regression models in r output table to word
Asked Answered
B

2

6

I have been using sjplot to create a combined table. This creates a HTML table. I would like to make a table that can be exported to word.

I have reviewed this post which discusses copy and pasting into word, but this alters the formatting of the columns and lines. Output several regression tables into multiple pages of a Word document in R

n1 <- glm(N  ~ Age_2 , data = n_data, family = "binomial")
g1 <- glm(G  ~ Age_2 , data = g1_data, family = "binomial")
ga1 <- glm(G_1  ~ Age_2 , data = ga1_data, family = "binomial")
l1 <- glm(L_1  ~ Age_2 , data = l1_data, family = "binomial")
c1 <- glm(C_1  ~ Age_2 , data = c1_data, family = "binomial")
m1 <- glm(m  ~ Age_2 , data = m1_data, family = "binomial")

tab_model (n1,g1,ga1,l1,c1,m1)

Also is it possible to add a line with the number that had the outcome (ie. number of N), in addition to the total number of observations per group?

Any suggestions? Willing to try other packages.

Brooke answered 2/4, 2020 at 17:38 Comment(3)
flextable outputs to word nicely. I use a combination of huxtable and flextable when outputing to word (huxtable to build the table, flextable to output to word)Sacerdotalism
the gtsummary package might also help here with output to work. You can change the output to knitr instead of gt to render it to word. It also has a way to compare regression models to each other: danieldsjoberg.com/gtsummaryJueta
Have you considered rmarkdown and knit to a word doc with knitr::kable for the tables?Evanevander
G
3

Since sjPlot outputs to html, it's very hard to get it into a Word document directly. Here's an example of how to do something similar to what you want to do using knitr, rmarkdown, jtools, and huxtable. I'm using RStudio with an rmarkdown document, which I knit to a Word document.

---
title: "jtools to Output Logistic Regression Models"
author: "sar"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: word_document
---

```{r setup, include=FALSE}
library(knitr)
library(jtools)
library(huxtable)

knitr::opts_chunk$set(echo=FALSE, warning = FALSE)

```

# Introduction

This is a test document to demonstrate how knitr and rmarkdown can be used to put output from jtools
into a Word Document

```{r OutputTable}
set.seed(1234)
logistic_s <- data.frame(N=rbinom(200,1,0.5),
                         G=rbinom(200,1,0.5),
                         G_1=rbinom(200,1,0.5),
                         L_1=rbinom(200,1,0.5),
                         C_1=rbinom(200,1,0.5),
                         m=rbinom(200,1,0.5),
                         Age_2=round(rnorm(200,40,6)))

n1 <- glm(N  ~ Age_2 , data = logistic_s, family = "binomial")
g1 <- glm(G  ~ Age_2 , data = logistic_s, family = "binomial")
ga1 <- glm(G_1  ~ Age_2 , data = logistic_s, family = "binomial")
l1 <- glm(L_1  ~ Age_2 , data = logistic_s, family = "binomial")
c1 <- glm(C_1  ~ Age_2 , data = logistic_s, family = "binomial")
m1 <- glm(m  ~ Age_2 , data = logistic_s, family = "binomial")

model_summs <- export_summs(n1,g1,ga1,l1,c1,m1,
                            error_format = "({conf.low}, {conf.high})",
                            model.names = c("N","G","G_1","L_1","C_1","m"))

col_width(model_summs) = c(0.84,rep(0.95,6))

model_summs
```
Goodkin answered 9/4, 2020 at 18:45 Comment(1)
thanks @Sam Dickson. I get this error when displaying the table: Error: pandoc document conversion failed with error 1 In addition: Warning messages: 1: In readLines(con, warn = FALSE) : invalid input found on input connection 'model_summs.doc.Rmd' 2: In knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet) : The file "model_summs.doc.Rmd" must be encoded in UTF-8. Please see yihui.org/en/2018/11/biggest-regret-knitr for more info. 3: In readLines(con, warn = FALSE) : invalid input found on input connection 'model_summs.doc.knit.md'Brooke
P
3

We can do this with the gtsummary package. By default, gtsummary prints tables with the gt packages that does not support Word output. But we can convert any gtsummary object to a type supported by Word. In the example below, we'll convert to a flextable.

I provided 2 formats for your solution: one long table, and one (very) wide table. Here's what the long table looks like: enter image description here

The wide format looks like this:

enter image description here We get the number of events for each model using the add_nevent() function. Here's the full code for the R Markdown file. NOTE: The as_flextable() is new, and you'll need to install the dev version of gtsummary remotes::install_github("ddsjoberg/gtsummary") http://www.danieldsjoberg.com/gtsummary/index.html

---
title: "Regression Tables with gtsummary"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Data 

```{r}
set.seed(324524)
logistic_s <- data.frame(N=rbinom(200,1,0.5),
                         G=rbinom(200,1,0.5),
                         G_1=rbinom(200,1,0.5),
                         L_1=rbinom(200,1,0.5),
                         C_1=rbinom(200,1,0.5),
                         m=rbinom(200,1,0.5),
                         Age_2=round(rnorm(200,40,6)))
```

## Long Table

Create a table that is one line per model

```{r}
library(gtsummary)
library(tidyverse)

# build models
tbl_uvregression(
  data = logistic_s,
  x = Age_2,
  method = glm,
  method.args = list(family = binomial),
  exponentiate = TRUE
) %>%
  modify_header(label = "**Model Outcome**") %>%
  # add the number of evenets
  add_nevent() %>%
  # export as flextable instead of gt table
  as_flextable()
```

## Wide Table

Create a table that wide

```{r cars}
# list all outcomes
outcomes <- c("N", "G", "G_1", "L_1", "C_1", "m") 

# map over each outcome to make a model and table
list_regression_tables <- 
  map(outcomes,
      # make a model for each outcome
      ~glm(
        formula = as.formula(paste(.x, "Age_2", sep = "~")),
        data = logistic_s,
        family = binomial
      ) %>%
        # putting model in table with tbl_regression
        tbl_regression(exponentiate = TRUE) %>%
        # add the number of evenets
        add_nevent() 
  )

# merging all tables together
tbl_merge(tbls = list_regression_tables,
          tab_spanner = outcomes) %>%
  # export as flextable instead of gt table
  as_flextable()
```

Happy Coding!

Perseverance answered 9/4, 2020 at 23:56 Comment(4)
thanks @Daniel Sjoberg. I am looking for one model per column (or one model for 3 subcolumns (OR,CI,p). For the rows I am looking to have the model variables.Brooke
Great, that is what the wide table does. Is that not what you're looking for? (Code in the R markdown chunk) @BrookePerseverance
I am getting this error message Error in map(outcomes, ~glm(formula = as.formula(paste(.x, "Age_2", sep = "~")), : could not find function "map"Brooke
I have since altered the models and used individual datasets for each model. I have updated my code in my question. Is there a way to do this with multiple datasets?Brooke

© 2022 - 2024 — McMap. All rights reserved.