Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract confidence intervals from multiple regression models?

I am extracting the regression results for two different groups as shown in this example below. In the temp data.frame i get the estimate, std.error, statistic and p-value. However, i don't get the confidence intervals. Is there a simple way to extract them as well?

 df <- tibble(
  a = rnorm(1000),
  b = rnorm(1000),
  c = rnorm(1000),
  d = rnorm(1000),
  group = rbinom(n=1000, size=1, prob=0.5)
)

df$group = as.factor(df$group)

temp <- df %>%
  group_by(group) %>%
  do(model1 = tidy(lm(a ~ b + c + d, data = .))) %>%   
  gather(model_name, model, -group) %>%                        
  unnest() 
like image 474
Jad Avatar asked Mar 01 '23 21:03

Jad


1 Answers

You are doing tidy on a lm object. If you check the help page, there is an option to include the confidence interval, conf.int=TRUE:

temp <- df %>%
  group_by(group) %>%
  do(model1 = tidy(lm(a ~ b + c + d, data = . ), conf.int=TRUE)) %>%   
  gather(model_name, model, -group) %>%                        
  unnest()

# A tibble: 8 x 9
  group model_name term  estimate std.error statistic p.value conf.low conf.high
  <fct> <chr>      <chr>    <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
1 0     model1     (Int…  0.0616     0.0423    1.46    0.146   -0.0215    0.145 
2 0     model1     b      0.00178    0.0421    0.0424  0.966   -0.0808    0.0844
3 0     model1     c     -0.00339    0.0431   -0.0787  0.937   -0.0881    0.0813
4 0     model1     d     -0.0537     0.0445   -1.21    0.228   -0.141     0.0337
5 1     model1     (Int… -0.0185     0.0454   -0.408   0.683   -0.108     0.0707
6 1     model1     b      0.00128    0.0435    0.0295  0.976   -0.0842    0.0868
7 1     model1     c     -0.0972     0.0430   -2.26    0.0244  -0.182    -0.0126
8 1     model1     d      0.0734     0.0457    1.60    0.109   -0.0165    0.163 
like image 117
StupidWolf Avatar answered Mar 04 '23 23:03

StupidWolf