Search code examples
rlmbroom

broom::tidy() calculate confidence intervals at 0.84 and 0.95 levels


Is there a nifty work around to calculate the confidence intervals at two levels using broom::tidy()?

Specifically, I want to calculate the confidence intervals at two levels rather than one. I am not entirely sure what the best approach might be. So I'd like something like this:

model %>%
   tidy(conf.int = TRUE, level = c(0.84, 0.95)

Any ideas?


Solution

  • If you want to use multiple different confidence intervals for the same model, you could try the following. Using map_dfr from purrr, you can indicate multiple different confidence levels and then use in the tidy function. Here is an example using mtcars data. An additional column has been added to indicate the level for that particular model result.

    library(purrr)
    library(broom)
    
    mod <- lm(mpg ~ wt, mtcars)
      
    map_dfr(
      c(.84, .95),
      ~cbind(tidy(x = mod, conf.int = T, conf.level = .x), level = .x)
    )
    

    Output

             term  estimate std.error statistic      p.value  conf.low conf.high level
    1 (Intercept) 37.285126  1.877627 19.857575 8.241799e-19 34.579856 39.990396  0.84
    2          wt -5.344472  0.559101 -9.559044 1.293959e-10 -6.150020 -4.538923  0.84
    3 (Intercept) 37.285126  1.877627 19.857575 8.241799e-19 33.450500 41.119753  0.95
    4          wt -5.344472  0.559101 -9.559044 1.293959e-10 -6.486308 -4.202635  0.95