How to get the OR and 95%CI from lrm()?

Viewed 395

I want use rms package to build the logistic model, but how can I get the 95%CI of OR from lrm()? I only know how to get value of OR.

My code as below:

n <- 1000    # define sample size
set.seed(17) # so can reproduce the results
y              <- rep(0:1, 500)
age            <- rnorm(n, 50, 10)
sex            <- factor(sample(c('female','male'), n,TRUE))
label(age)            <- 'Age'
label(sex)            <- 'Sex'

f <- lrm(y ~ age + sex, x=TRUE, y=TRUE)
f

# OR
exp(coef(f))

1 Answers

To get the lower 95% confidence interval of the log odds ratio for each variable, you subtract 1.96 times the standard error from the estimate. The upper confidence interval for the log odds ratio is 1.96 times the standard error added to the estimate. When you exponentiate these results, you have your confidence interval for the odds ratio.

The reason for using the number 1.96 is that a standard normal distribution's 2.5% quantile is at -1.96 and its 97.5% quantile is at +1.96. If you want to avoid using 1.96 as a "magic number", you can use the qnorm function instead (see below).

The difficulty with lrm objects is that the standard errors are not directly stored in the model object, and are worked out each time you print to the console. However, you can easily get them by taking the square root of the diagonal of the matrix var that is a member of f:

sqrt(diag(f$var))
#>   Intercept         age    sex=male 
#> 0.326635446 0.006222249 0.126599055

You can check that this is the same as the standard error column in your model summary.

So to get a data frame of the odds ratios and their confidence intervals, you could do:

df <- data.frame(variable  = colnames(f$var),
                 OR        = exp(f$coefficients),
                 OR_lower  = exp(f$coefficients + qnorm(0.025) * sqrt(diag(f$var))),
                 OR_upper  = exp(f$coefficients + qnorm(0.975) * sqrt(diag(f$var))),
                 row.names = NULL)

df
#>    variable        OR  OR_lower OR_upper
#> 1 Intercept 1.1107651 0.5855846 2.106953
#> 2       age 0.9990378 0.9869282 1.011296
#> 3  sex=male 0.8935087 0.6971681 1.145144
Related