Search code examples
rdataframematrixdatasetlogistic-regression

Save results of glm into matrix in R


I performed multivariable logistic regression using glm.

example <- glm(Death ~ age + sex + time, db, family = binomial)

I just want to save the results into a simple matrix in R just like this:

enter image description here

Where columns A= odds ratio; B= lower 95%CI; C= upper 95%CI. Each row corresponds to the results of one variable (so if age is the first variable, OR: 1.4, 95% CI: 0.9 - 1.6)

How can I do it? The problem is that the results of glm are automatically saved into a list so I don't know how to code it.

Also, is there a way maybe to put automatically in a separate column the variable to which the OR and 95%CI is referred to? I just need this table to do plots and I don't want to confound myself.

enter image description here


Solution

  • I found the solution! Hope it might be useful for someone

    example <- glm(Death ~ age + sex + time, db, family = binomial)
    CI <- exp(confint.default(example))
    aOR <- exp(coefficients(example))
    
    df<-as.data.frame(CI) %>%
                      mutate(aOR=aOR) %>%
                      filter(!row_number() %in% c(1)) 
    
    df <- df[, c(3,1,2)]
    rm(CI, example, aOR)