regression models in r output table to word

寵の児 提交于 2021-01-27 06:33:01

问题


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.


回答1:


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
```



回答2:


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:

The wide format looks like this:

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!



来源:https://stackoverflow.com/questions/60997710/regression-models-in-r-output-table-to-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!