Search code examples
rdplyrconfidence-intervaltidyrms

r use of rms ols function with multiple objects and confidence intervals


This example below uses the lm function and tidy(..., conf.int=TRUE) easily generates summary estimates and C.I for multiple model objects.

library(tidyverse)
library(broom)

mtcars %>%
  gather(predictor, measure, -mpg) %>%
  group_by(predictor) %>%
  do(tidy(lm(mpg ~ measure, .),conf.int=TRUE))

How do I generate similar output using rms::ols function ? tidy does not work on ols so is there a way to tweak the output from ols function to include C.I from multiple model objects ? Thanks in advance.


Solution

  • Here is a bare-bones/stripped-down version of tidy.ols. It has many limitations (see below), but should do what you want ...

    tidy.ols <- function(x, ...) {
       se <- sqrt(diag(x$var)) 
       rdf <- x$stats["n"] - x$stats["d.f."] - 1
       cc <- coef(x)
       ci <- confint(x)
       tibble(term = names(cc),
              estimate = cc,
              std.error = se,
              statistic = cc/se,
              df = rdf,
              p.value = 2*pt(-abs(statistic), df = rdf),
              conf.low = ci[,1],
              conf.high = ci[,2])
    }
    mtcars %>%
      gather(predictor, measure, -mpg) %>%
      group_by(predictor) %>%
      do(tidy(ols(mpg ~ measure, .)))
    

    limitations

    • ignores additional args without checking
    • haven't checked p-value calcs etc.
    • doesn't allow choice of confint or not, or setting conf level
    • doesn't consider other possible bells and whistles of ols ...

    It seems like support for rms objects has been discussed a lot on the tidymodels/broom GitHub site but everything there is stale ... I opened an issue if anyone wants to chime in.