2

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.

bison2178
  • 747
  • 1
  • 8
  • 22

1 Answers1

2

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.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • i just checked the `p values` and `C.I `from regular `tidy(lm(` vs `tidy.ols(ols` both appear similar, no difference, so looks good to me. Thanks Ben. Tidy including a wrapper for rms objects is my wishlist. – bison2178 Jul 11 '23 at 19:01