r 中的回归模型输出表到单词

问题描述 投票:0回答:3

我一直在使用 sjplot 创建组合表。这将创建一个 HTML 表格。我想做一个可以导出到word的表格。

我已经审阅了这篇文章,其中讨论了复制和粘贴到 Word 中,但这会改变列和行的格式。 在 R 中将多个回归表输出到 Word 文档的多个页面中

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)

除了每组的观察总数之外,是否可以添加一行包含有结果的数字(即 N 的数量)?

有什么建议吗?愿意尝试其他套餐。

r ms-word logistic-regression sjplot gtsummary
3个回答
3
投票

由于

sjPlot
输出为html,因此很难直接将其导入Word文档。以下示例展示了如何使用
knitr
rmarkdown
jtools
huxtable
执行与您想要执行的操作类似的操作。我将 RStudio 与 rmarkdown 文档一起使用,我将其编织到 Word 文档中。

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

3
投票

我们可以使用 gtsummary 包来做到这一点。默认情况下,gtsummary 使用不支持 Word 输出的 gt 包打印表格。但我们可以将任何 gtsummary 对象转换为 Word 支持的类型。在下面的示例中,我们将转换为弹性表。

我为您的解决方案提供了两种格式:一张长桌子和一张(非常)宽的桌子。长桌子是这样的:

宽格式看起来像这样:

我们使用

add_nevent()
函数获取每个模型的事件数。以下是 R Markdown 文件的完整代码。注意:
as_flextable()
是新的,您需要安装 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()
```

快乐编码!


0
投票

可以调整宽格式以包含多个自变量吗?

© www.soinside.com 2019 - 2024. All rights reserved.