Home > Blockchain >  Store coefficients, confidence intervalls and odds ratios in one dataframe after univariate logistic
Store coefficients, confidence intervalls and odds ratios in one dataframe after univariate logistic

Time:11-15

The dataframe looks like this (dput at the end):

 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

I perform univariate logistic regression where A is the dependent variable and the others B:K are the independent variables. All are factor.

This code works:

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)
       })

However there is a bunch of information in the output(each of them useful) but I want to know if this kind of output is possible in R:

Desired output:

            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 ***

The data:

df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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"))

CodePudding user response:

@TarJae. Maybe this might be helpful.

You can use map_df from purrr to go through all of your independent variables.

If you use tidy from broom you can get the output desired. If you add conf.int to TRUE you will get confidence intervals (default level is 0.95).

You can also obtain relative odds ratios with exp of your estimate. An exponentiate option for tidy would only show exponentiated coefficients.

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))

Output

   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.  

CodePudding user response:

I don't know for any function to do this automatically, but you can also do it yourself. Here is a code to get all the coefficients you want:

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])

You can include this inside function and append each interaction to a data frame outside the function.

  • Related