Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regression models in r output table to word

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.

like image 432
sar Avatar asked Apr 02 '20 17:04

sar


People also ask

How to interpret regression output in R?

How to Interpret Regression Output in R To fit a linear regression model in R, we can use the lm () command. To view the output of the regression model, we can then use the summary () command. This tutorial explains how to interpret every value in the regression output in R.

Can R create a regression table with multiple models?

Feb , 2021 11 min read This post describes how R can be used to create regression tables that combine multiple models or steps (e.g., stepwise regressions, hierarchical regressions) for several dependent (or outcome) variables.

How do you make a table from regression results?

A simple HTML table from regression results First, we fit two linear models to demonstrate the tab_model()-function. m1 <-lm(barthtot ~c160age +c12hour +c161sex +c172code, data =efc)m2 <-lm(neg_c_7 ~c160age +c12hour +c161sex +e17age, data =efc) The simplest way of producing the table output is by passing the fitted model as parameter.

How to export table from R to Microsoft Word?

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.


2 Answers

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
```
like image 111
Sam Dickson Avatar answered Sep 28 '22 07:09

Sam Dickson


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!

like image 35
Daniel D. Sjoberg Avatar answered Sep 28 '22 06:09

Daniel D. Sjoberg