对R数据集中的每列循环glm

lh80um4z  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(133)

我有一个包含100例患者(此处显示了7例)、2个协变量和50个表型(此处显示了5例)的数据集。我想使用协变量1和协变量2作为协变量对每个表型进行多变量logistic回归,以预测结局。我想得到一个如下表格,其中包含每个协变量的p值、OR和置信区间(CI)。

我试过:

for (i in df) {
  print(i)
  model <-glm(Outcome~ x[i] +Covariate1 +Covariate2, family = binomial(link = "logit"), data=df)

我也试过这个问题的解答,但是x和y在我的问题中颠倒了,所以它不起作用: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))
u5rb5r59

u5rb5r591#

如果我理解正确话

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年1月11日,使用reprex v2.0.2

相关问题