在具有多个自变量的单变量逻辑回归后,将系数、置信区间和优势比存储在一个数据框中

Store coefficients, confidence intervalls and odds ratios in one dataframe after univariate logistic regression with multiple independent variables

数据框看起来像这样(最后是dput):

 A      B     C     D     E     F     G     H     I     J     K    
   <fct>  <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct>
 1 No ev~ fema~ >=60  <30   B     1     >=150 M     0     >=30  No   
 2 No ev~ fema~ <60   <30   A     0     <150  B     0     <30   No   
 3 No ev~ fema~ <60   >=30  A     1     >=150 M     0     <30   No   
 4 No ev~ fema~ >=60  <30   A     0     >=150 M     0     <30   No   
 5 No ev~ male  <60   >=30  B     1     >=150 B     0     <30   No   
 6 No ev~ male  >=60  <30   A     1     >=150 M     1     >=30  No   
 7 event  fema~ >=60  >=30  A     1     >=150 B     0     <30   Yes  
 8 No ev~ fema~ <60   <30   A     0     >=150 M     0     >=30  No   
 9 No ev~ male  >=60  <30   A     0     >=150 B     1     <30   No   
10 No ev~ male  >=60  <30   B     1     >=150 M     0     <30   No   
# ... with 140 more rows

我执行单变量逻辑回归,其中 A 是因变量,其他 B:K 是自变量。都是因素。

此代码有效:

lapply(c("B","C","D","E","F","G","H","I","J", "K"),
       
       function(var) {
         
         formula    <- as.formula(paste("A ~", var))
         res.logist <- glm(formula, data = df_fake, family = binomial)
         
         summary(res.logist)
       })

但是输出中有一堆信息(每一个都有用)但我想知道这种输出在 R 中是否可行:

期望输出:

            Estimate Std. Error z value   Pr(>|z|)      OR lowerlimit upperlimit
Bmale         0.2941     0.6917   0.425   0.671          ?   ?          ?       
C>=60         0.5653     0.7269   0.778   0.437 
D>=30         1.7579     0.7061   2.489   0.0128 *
EB            0.7302     0.6929   1.054   0.292  
F1            1.4508     0.7298   1.988   0.0468 *
G>=150        0.1238     0.6917   0.179   0.858 
HM            1.0223     0.7274   1.405   0.16
I1            0.5325     0.7351   0.724   0.469
J>=30         0.6581     0.7372   0.893   0.372   
KYes          5.0814     0.9917   5.124   0.00000029909 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L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("<30", ">=30"), class = "factor"), 
K = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor")), row.names = c(NA, 
-150L), class = c("tbl_df", "tbl", "data.frame"))

我不知道有什么功能可以自动执行此操作,但您也可以自己执行。这是获取所需所有系数的代码:

res.logist <- glm(A ~ B, data = df, family = binomial)
res <- tibble(Independant = names(res.logist[[1]])[2],
              Estimate = res.logist[[1]][2],
              'Std. Error' = summary(res.logist)$coefficients[2,2],
              'z value' = summary(res.logist)$coefficients[2,3],
              'Pr(>|z|)' = summary(res.logist)$coefficients[2,4],
              OR = exp(coef(res.logist))[2],
              lowerlimit = confint(res.logist)[2,1],
              upperlimit = confint(res.logist)[2,2])

您可以包含此内部函数并将每个交互附加到函数外部的数据框。

@TarJae。也许这可能会有所帮助。

您可以使用 purrr 中的 map_df 遍历所有自变量。

如果您使用 broom 中的 tidy,您可以获得所需的输出。如果将 conf.int 添加到 TRUE,您将获得置信区间(默认水平为 0.95)。

您还可以通过 estimateexp 获得相对优势比。 tidyexponentiate 选项只会显示指数系数。

library(tidyverse)
library(broom)

map_df(set_names(names(df)[names(df) != "A"]),
       ~glm(formula(paste("A ~ ", .x)), data = df, family = binomial) %>%
         tidy(conf.int = TRUE)) %>%
  filter(term != "(Intercept)") %>%
  mutate(OR = exp(estimate))

输出

   term   estimate std.error statistic     p.value conf.low conf.high     OR
   <chr>     <dbl>     <dbl>     <dbl>       <dbl>    <dbl>     <dbl>  <dbl>
 1 Bmale     0.294     0.692     0.425 0.671        -1.07        1.73   1.34
 2 C>=60     0.565     0.727     0.778 0.437        -0.808       2.15   1.76
 3 D>=30     1.76      0.706     2.49  0.0128        0.364       3.22   5.80
 4 EB        0.730     0.693     1.05  0.292        -0.640       2.16   2.08
 5 F1        1.45      0.730     1.99  0.0468        0.0724      3.04   4.27
 6 G>=150    0.124     0.692     0.179 0.858        -1.24        1.56   1.13
 7 HM        1.02      0.727     1.41  0.160        -0.351       2.61   2.78
 8 I1        0.532     0.735     0.724 0.469        -1.07        1.92   1.70
 9 J>=30     0.658     0.737     0.893 0.372        -0.943       2.05   1.93
10 KYes      5.08      0.992     5.12  0.000000299   3.31        7.31 161.