2

我有一个数据集,其中年龄作为连续因素和一个因素,性别作为一个因素和 4 个组。

structure(list(Age = c(9, 12, 16, 57), Age_1 = structure(c(2L, 
3L, 3L, 7L), .Label = c("8", "1", "2", "3", "4", "5", "6", "7"
), class = "factor"), Sex = structure(c(2L, 1L, 2L, 1L), .Label = c("M", 
"F", "U"), class = "factor"), N = structure(c(2L, 2L, 2L, 
2L), .Label = c("0", "1"), class = "factor"), G = structure(c(1L, 
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), L_1 = 
structure(c(1L, 
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), C_1 = 
structure(c(1L, 
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), G_1 = 
structure(c(1L, 
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), m = structure(c(1L, 
1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), A = c(1, 
1, 1, 1)), row.names = c(NA, 4L), class = "data.frame")

我想对每个组(N、G、L_1、C_1、G_1、m)的每个变量(Age、Age_1 和性别)进行逻辑回归。例如。

mylogit <- glm(N  ~ Sex, data = logistic_s, family = "binomial")
mylogit <- glm(N  ~ Age, data = logistic_s, family = "binomial")

我正在使用 gtsummary 来组合表格中的变量。

library(gtsummary) 

tbl_n <-
      tbl_uvregression(
        logistic_s[c("N", "Age", "sex", "Age_1")],
        method = glm,
        y = N,
        method.args = list(family = binomial),
        exponentiate = TRUE
      )

tbl_n  

这会产生一组(例如 N)的输出,其中包含变量 Age、Age_1、Sex。

我想对每个组(例如 N、G、L_1 等)重复此操作,然后将这些表组合成一个组合表。

如果有其他更适合这个的选项,我愿意使用不同的包。我想做一个可以用word导出的表格。

4

1 回答 1

2

我同意一些可重现的代码会有所帮助。我不是 100% 确定你想要得到什么样的输出。您想为 2 个或更多组分别构建一些单变量逻辑回归模型?

如果这是正确的,这里有一种方法:我将使用包中的trial数据集gtsummary作为示例。我将分组变量处理 ( trt)。

library(gtsummary)
library(tidyverse)

trial_subset <-
  trial %>%
  select(trt, response, age, marker, grade) 

我们将从trt使用 package.json 中的tbl_uvregression()函数构建分层的单变量回归表开始gtsummary。它们将存储在数据框中的一个新列中,称为tbl_uv.

df_uv <-
  trial_subset %>%
  # group by trt, and nest data within group
  group_by(trt) %>%
  nest() %>%
  # build univariate logistic regression models separately within grouping variable
  mutate(
    tbl_uv = map(
      data,
      ~tbl_uvregression(
        data = .x, 
        y = response,
        method = glm, 
        method.args = list(family = binomial),
        exponentiate = TRUE
      )
    )
  )
#> # A tibble: 2 x 3
#> # Groups:   trt [2]
#>   trt    data               tbl_uv    
#>   <chr>  <list>             <list>    
#> 1 Drug A <tibble [98 x 4]>  <tbl_vrgr>
#> 2 Drug B <tibble [102 x 4]> <tbl_vrgr>

我们现在可以使用保存的表格,使用该函数df_uv将它们合并到一个表格中。tbl_merge()

tbl_merge(
  tbls = df_uv$tbl_uv, # list of the univariate logistic regression tables
  tab_spanner = paste0("**", df_uv$trt, "**") # adding stars to bold the header
)

这将产生下表。我希望这是有帮助的!

在此处输入图像描述

于 2020-03-31T21:04:17.473 回答