[英]Loop glm for every column in R dataset
我有一个包含 100 位患者(此处显示 7 位)、2 个协变量和 50 种表型(此处显示 5 位)的数据集。 我想使用协变量 1 和协变量 2 作为协变量对每个表型执行多变量逻辑回归来预测结果,我想得到一个这样的表,其中我有每个表型的 p 值、OR 和置信区间 (CI)协变量。
我试过了:
for (i in df) {
print(i)
model <-glm(Outcome~ x[i] +Covariate1 +Covariate2, family = binomial(link = "logit"), data=df)
我也尝试了这个问题的解决方案。 但是在我的问题中 x 和 ya 颠倒了,所以它不起作用: R: automate table for results of several multivariable logistic regressions
非常感谢您的帮助!
这是一个示例数据集
df<-structure(list(ID = c(1, 2, 3, 4, 5, 6, 7), Outcome = c(0, 0,
1, 1, 0, 1, 0), Covariate1 = c(1, 2, 3, 4, 5, 6, 7), Covariate2 = c(0,
0, 0, 1, 1, 1, 1), P1 = c(1, 0, 0, 1, 1, 1, 2), P2 = c(0, 2,
0, 1, 1, 1, 1), P3 = c(0, 0, 0, 1, 1, 1, 1), P4 = c(0, 0, 0,
1, 2, 1, 1), P5 = c(0, 0, 0, 1, 1, 1, 2)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -7L))
如果我理解正确的话
df <- structure(
list(
ID = c(1, 2, 3, 4, 5, 6, 7),
Outcome = c(0, 0, 1, 1, 0, 1, 0),
Covariate1 = c(1, 2, 3, 4, 5, 6, 7),
Covariate2 = c(0, 0, 0, 1, 1, 1, 1),
P1 = c(1, 0, 0, 1, 1, 1, 2),
P2 = c(0, 2, 0, 1, 1, 1, 1),
P3 = c(0, 0, 0, 1, 1, 1, 1),
P4 = c(0, 0, 0, 1, 2, 1, 1),
P5 = c(0, 0, 0, 1, 1, 1, 2)
),
class = c("tbl_df",
"tbl", "data.frame"),
row.names = c(NA,-7L)
)
library(tidyverse)
first_tables <- map(
.x = select(df, starts_with("P")),
.f = ~ glm(
Outcome ~ .x + Covariate1 + Covariate2,
family = binomial(link = "logit"),
data = df
)
) %>%
map(broom::tidy)
map_df(
.x = first_tables,
.f = ~ .x %>% mutate(
p = p.value,
OR = exp(estimate),
CI5 = exp(estimate - 1.96 * std.error),
CI95 = exp(estimate + 1.96 * std.error),
.keep = "unused"
) %>%
select(-statistic),
.id = "phenotype"
) %>%
filter(term == ".x") %>%
select(-term)
#> # A tibble: 5 x 5
#> phenotype p OR CI5 CI95
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 P1 0.997 5.84e-10 0 Inf
#> 2 P2 0.996 1.53e- 4 0 Inf
#> 3 P3 0.824 2.00e+ 0 0.00442 904.
#> 4 P4 0.998 3.66e- 9 0 Inf
#> 5 P5 0.997 2.72e-10 0 Inf
创建于 2023-01-11,使用reprex v2.0.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.