In this post, I will present a simple way how to export your regression results (or output) from R into Microsoft Word. Previously, I have written a tutorial how to create Table 1 with study characteristics and to export into Microsoft Word. These posts are especially useful for researchers who prepare their manuscript for publication in peer-reviewed journals.

Get the results from Cox Regression Analysis

As an example to illustrate this post, I will compute a survival analysis. Survival analysis is statistical methods for analyzing data where the outcome variable is the time until the occurrence of an event. The event can be an occurrence of a disease or death, etc. In R we compute the survival analysis with the survival package. The function for Cox regression analysis is coxph(). I will use the veteran data which come with survival package.

## Load survival package
library(survival)
# Load veteran data
data(veteran)
# Data description
help(veteran, package="survival")
# Show first 6 rows
head(veteran) 
  trt celltype time status karno diagtime age prior
1   1 squamous   72      1    60        7  69     0
2   1 squamous  411      1    70        5  64    10
3   1 squamous  228      1    60        3  38     0
4   1 squamous  126      1    60        9  63    10
5   1 squamous  118      1    70       11  65    10
6   1 squamous   10      1    20        5  49     0

trt:	1=standard 2=test
celltype:	1=squamous, 2=smallcell, 3=adeno, 4=large
time:	survival time
status:	censoring status
karno:	Karnofsky performance score (100=good)
diagtime:	months from diagnosis to randomisation
age:	in years
prior:	prior therapy 0=no, 1=yes 

Now let say that we are interested to know the risk of dying (status) from different cell type (celltype) and treatment (trt) when we adjust for other variables (karno, age prior, diagtime).

This is the model:

# Fit the COX model
fit = coxph(Surv(time, status) ~ age + celltype + prior + karno + diagtime + trt, data=veteran)

And the output:

summary(fit)
Call:
coxph(formula = Surv(time, status) ~ age + celltype + prior + 
    karno + diagtime + trt, data = veteran)

  n= 137, number of events= 128 

                        coef  exp(coef)   se(coef)      z Pr(>|z|)    
age               -8.706e-03  9.913e-01  9.300e-03 -0.936  0.34920    
celltypesmallcell  8.616e-01  2.367e+00  2.753e-01  3.130  0.00175 ** 
celltypeadeno      1.196e+00  3.307e+00  3.009e-01  3.975 7.05e-05 ***
celltypelarge      4.013e-01  1.494e+00  2.827e-01  1.420  0.15574    
prior              7.159e-03  1.007e+00  2.323e-02  0.308  0.75794    
karno             -3.282e-02  9.677e-01  5.508e-03 -5.958 2.55e-09 ***
diagtime           8.132e-05  1.000e+00  9.136e-03  0.009  0.99290    
trt                2.946e-01  1.343e+00  2.075e-01  1.419  0.15577    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

                  exp(coef) exp(-coef) lower .95 upper .95
age                  0.9913     1.0087    0.9734    1.0096
celltypesmallcell    2.3669     0.4225    1.3799    4.0597
celltypeadeno        3.3071     0.3024    1.8336    5.9647
celltypelarge        1.4938     0.6695    0.8583    2.5996
prior                1.0072     0.9929    0.9624    1.0541
karno                0.9677     1.0334    0.9573    0.9782
diagtime             1.0001     0.9999    0.9823    1.0182
trt                  1.3426     0.7448    0.8939    2.0166

Concordance= 0.736  (se = 0.03 )
Rsquare= 0.364   (max possible= 0.999 )
Likelihood ratio test= 62.1  on 8 df,   p=1.799e-10
Wald test            = 62.37  on 8 df,   p=1.596e-10
Score (logrank) test = 66.74  on 8 df,   p=2.186e-11

As we see there are “a lot” of results. In the manuscript, we often report only the Hazard ratio and 95% Confidence interval and only for the variables of interest. For example in this case I am interested for the cell types and treatment. Note: I will not comment for the regression coefficients since is not the aim of this post.

Prepare the table by creating the columns

# Prepare the columns
HR <- round(exp(coef(fit)), 2)
CI <- round(exp(confint(fit)), 2)
P <- round(coef(summary(fit))[,5], 3)
# Names the columns of CI
colnames(CI) <- c("Lower", "Higher")
# Bind columns together as dataset
table2 <- as.data.frame(cbind(HR, CI, P))
table2
                 HR Lower Higher     P
age               0.99  0.97   1.01 0.349
celltypesmallcell 2.37  1.38   4.06 0.002
celltypeadeno     3.31  1.83   5.96 0.000
celltypelarge     1.49  0.86   2.60 0.156
prior             1.01  0.96   1.05 0.758
karno             0.97  0.96   0.98 0.000
diagtime          1.00  0.98   1.02 0.993
trt               1.34  0.89   2.02 0.156

Select variables of interest from the table

As I mentioned earlier, I am interested only for 2 variables (cell type and treatment). With the code below I will select those variables.

# select variables you want to present in table
table2 <- table2[c("celltypesmallcell","celltypeadeno","celltypelarge","trt"),]
table2
                HR Lower Higher     P
celltypesmallcell 2.37  1.38   4.06 0.002
celltypeadeno     3.31  1.83   5.96 0.000
celltypelarge     1.49  0.86   2.60 0.156
trt               1.34  0.89   2.02 0.156

Format the table

In the manuscript we present the confidence intervals within brackets. Therefore, with the code below I will add the brackets.

# add brackes and line for later use in table
table2$a <- "("; table2$b <- "-"; table2$c <- ")"

# order the columns
table2 <- table2[,c("HR","a","Lower","b","Higher","c", "P")]
table2
                 HR a Lower b Higher c     P
celltypesmallcell 2.37 (  1.38 -   4.06 ) 0.002
celltypeadeno     3.31 (  1.83 -   5.96 ) 0.000
celltypelarge     1.49 (  0.86 -   2.60 ) 0.156
trt               1.34 (  0.89 -   2.02 ) 0.156

Finalize the table and make it ready for Microsoft Word

The table is almost ready, now I will merge in one column by using package tidyr with function unite().

# Merge all columns in one
library(tidyr)
table2 = unite(table2, "HR (95%CI)", c(HR, a, Lower, b, Higher, c), sep = "", remove=T)
# add space between the estimates of HR and CI
table2[,1] <- gsub("\\(", " (", table2[,1])
table2
                HR (95%CI)     P
celltypesmallcell 2.37 (1.38-4.06) 0.002
celltypeadeno     3.31 (1.83-5.96) 0.000
celltypelarge      1.49 (0.86-2.6) 0.156
trt               1.34 (0.89-2.02) 0.156

Export Table from R to Microsoft Word

To export table from R to Microsoft Word I will use the function FlexTable() from the package ReporteRs. I found a very good script in StackOverflow to achieve this task. I am sharing the code below. (Credits to the author in StackOverflow).

# Load the packages
library(ReporteRs)
library(magrittr)
# The script
docx( ) %>% 
     addFlexTable(table2 %>%
               FlexTable(header.cell.props = cellProperties( background.color = "#003366"),
                    header.text.props = textBold(color = "white"),
                    add.rownames = TRUE ) %>%
               setZebraStyle(odd = "#DDDDDD", even = "#FFFFFF")) %>%
     writeDoc(file = "table2.docx")

This is the table in Microsoft Word:
table2-HR

If you have any comment or feedback feel free to post a comment below.