使用“tbl_regression”对嵌套数据框进行多变量回归
Using `tbl_regression` for multivariable regression on nested data frames
我正在对具有一组一致的自变量的结果变量列表进行多变量回归。对于单变量回归,我遵循 this example 在嵌套数据框上使用 gtsummary
中的 tl_uvregression
,但我试图在嵌套数据框上使用 tbl_regression
将其推广到多变量回归数据框,当我尝试 unnest
tables 时,出现“输入必须是向量列表”的错误。以下是我尝试过的 - 我假设我缺少一些小但关键的步骤,但我无法弄清楚它是什么。我想要的输出是 table 多变量回归输出,每个模型作为一列,所有协变量作为行(类似于在每个多变量模型 运行 的列表上执行 tbl_merge
分别在 tbl_regression
).
library(tidyverse)
library(magrittr)
library(gtsummary)
library(broom)
id <- 1:2000
gender <- sample(0:1, 2000, replace = T)
age <- sample(17:64, 2000, replace = T)
race <- sample(0:1, 2000, replace = T)
health_score <- sample(0:25, 2000, replace = T)
cond_a <- sample(0:1, 2000, replace = T)
cond_b <- sample(0:1, 2000, replace = T)
cond_c <- sample(0:1, 2000, replace = T)
cond_d <- sample(0:1, 2000, replace = T)
df <- data.frame(id, gender, age, race, health_score, cond_a, cond_b, cond_c, cond_d)
regression_tables <- df %>% select(-id) %>%
gather(c(cond_a, cond_b, cond_c, cond_d), key = "condition", value = "case") %>%
group_by(condition) %>% nest() %>%
mutate(model = map(data, ~glm(case ~ gender + age + race + health_score, family = "binomial", data = .)),
table = map(model, ~tbl_regression, exponentiate = T, conf.level = 0.99)) %>%
select(table) %>% unnest(table)
问题似乎是使用了 lambda 表达式 (~
) 而没有使用它,即指定参数。此外,没有可用的 tidy
方法(来自 broom
)从 tbl_regression
中提取为 tibble 格式
library(dplyr)
library(tidyr)
library(broom)
library(gtsummary)
out <- df %>%
select(-id) %>%
gather(c(cond_a, cond_b, cond_c, cond_d), key = "condition",
value = "case") %>%
group_by(condition) %>%
nest() %>%
mutate(model = map(data,
~glm(case ~ gender + age + race + health_score,
family = "binomial", data = .)),
table = map(model, tbl_regression, exponentiate = T, conf.level = 0.99)) %>%
select(table)
out$table[[1]]
除了OP使用map
循环的方法外,其实我们可以简单地应用模型,tbl_regression
在nest_by
之后(替换gather
pivot_longer
因为 gather
会被弃用,pivot_longer
是通用版本)
out <- df %>%
select(-id) %>%
pivot_longer(cols = starts_with('cond'),
names_to = 'condition', values_to = 'case') %>%
nest_by(condition) %>%
mutate(model = list(glm(case ~ gender + age +
race + health_score,
family = "binomial", data = data)),
table_out = list(tbl_regression(model, exponentiate = TRUE, conf.level = 0.99)))
out
# A tibble: 4 x 4
# Rowwise: condition
# condition data model table_out
# <chr> <list<tbl_df[,5]>> <list> <list>
#1 cond_a [2,000 × 5] <glm> <tbl_rgrs>
#2 cond_b [2,000 × 5] <glm> <tbl_rgrs>
#3 cond_c [2,000 × 5] <glm> <tbl_rgrs>
#4 cond_d [2,000 × 5] <glm> <tbl_rgrs>
如果我们需要合并 table,请在 tbl_regression
的 list
上应用 tbl_merge
tbl_merge(out$table_out)
-输出
我正在对具有一组一致的自变量的结果变量列表进行多变量回归。对于单变量回归,我遵循 this example 在嵌套数据框上使用 gtsummary
中的 tl_uvregression
,但我试图在嵌套数据框上使用 tbl_regression
将其推广到多变量回归数据框,当我尝试 unnest
tables 时,出现“输入必须是向量列表”的错误。以下是我尝试过的 - 我假设我缺少一些小但关键的步骤,但我无法弄清楚它是什么。我想要的输出是 table 多变量回归输出,每个模型作为一列,所有协变量作为行(类似于在每个多变量模型 运行 的列表上执行 tbl_merge
分别在 tbl_regression
).
library(tidyverse)
library(magrittr)
library(gtsummary)
library(broom)
id <- 1:2000
gender <- sample(0:1, 2000, replace = T)
age <- sample(17:64, 2000, replace = T)
race <- sample(0:1, 2000, replace = T)
health_score <- sample(0:25, 2000, replace = T)
cond_a <- sample(0:1, 2000, replace = T)
cond_b <- sample(0:1, 2000, replace = T)
cond_c <- sample(0:1, 2000, replace = T)
cond_d <- sample(0:1, 2000, replace = T)
df <- data.frame(id, gender, age, race, health_score, cond_a, cond_b, cond_c, cond_d)
regression_tables <- df %>% select(-id) %>%
gather(c(cond_a, cond_b, cond_c, cond_d), key = "condition", value = "case") %>%
group_by(condition) %>% nest() %>%
mutate(model = map(data, ~glm(case ~ gender + age + race + health_score, family = "binomial", data = .)),
table = map(model, ~tbl_regression, exponentiate = T, conf.level = 0.99)) %>%
select(table) %>% unnest(table)
问题似乎是使用了 lambda 表达式 (~
) 而没有使用它,即指定参数。此外,没有可用的 tidy
方法(来自 broom
)从 tbl_regression
library(dplyr)
library(tidyr)
library(broom)
library(gtsummary)
out <- df %>%
select(-id) %>%
gather(c(cond_a, cond_b, cond_c, cond_d), key = "condition",
value = "case") %>%
group_by(condition) %>%
nest() %>%
mutate(model = map(data,
~glm(case ~ gender + age + race + health_score,
family = "binomial", data = .)),
table = map(model, tbl_regression, exponentiate = T, conf.level = 0.99)) %>%
select(table)
out$table[[1]]
除了OP使用map
循环的方法外,其实我们可以简单地应用模型,tbl_regression
在nest_by
之后(替换gather
pivot_longer
因为 gather
会被弃用,pivot_longer
是通用版本)
out <- df %>%
select(-id) %>%
pivot_longer(cols = starts_with('cond'),
names_to = 'condition', values_to = 'case') %>%
nest_by(condition) %>%
mutate(model = list(glm(case ~ gender + age +
race + health_score,
family = "binomial", data = data)),
table_out = list(tbl_regression(model, exponentiate = TRUE, conf.level = 0.99)))
out
# A tibble: 4 x 4
# Rowwise: condition
# condition data model table_out
# <chr> <list<tbl_df[,5]>> <list> <list>
#1 cond_a [2,000 × 5] <glm> <tbl_rgrs>
#2 cond_b [2,000 × 5] <glm> <tbl_rgrs>
#3 cond_c [2,000 × 5] <glm> <tbl_rgrs>
#4 cond_d [2,000 × 5] <glm> <tbl_rgrs>
如果我们需要合并 table,请在 tbl_regression
list
上应用 tbl_merge
tbl_merge(out$table_out)
-输出