Search code examples
rtidyrbroom

Using the broom::tidy to add confidence intervals to linear models in nested data frame


I've been trying follow the approach set out by Hadley Wickham for running multiple models in a nested data frame as per https://r4ds.had.co.nz/many-models.html

I've managed to write this code below to create the multiple linear models:

#create nested data frame
by_clin_grp <- df_long %>%
  group_by(clin_risk) %>%
  nest()

#create a function to run the linear models
model <- function(df){
  lm(outcome ~ age + sex + clin_sub_grp, data =df)
}

#run the models
by_clin_grp <- by_clin_grp %>%
  mutate(model = map(data,model))


#unnest the models using the broom package 
by_clin_grp_unnest <- by_clin_grp %>%
  mutate(tidy = map(model, broom::tidy))%>%
  unnest(tidy)

However, I need to add confidence intervals around my regression estimates. It seems like I should be able to add them using broom::tidy as per the help page but I'm unable to work out how to specify this correctly in the code above?


Solution

  • You have to specify the relevant arguments inside map. There are two possibilities:

    by_clin_grp_unnest <- by_clin_grp %>%
      mutate(tidy = map(model, ~broom::tidy(.x, conf.int = TRUE, conf.level = 0.99)))%>%
      unnest(tidy)
    

    or

    by_clin_grp_unnest <- by_clin_grp %>%
      mutate(tidy = map(model, broom::tidy, conf.int = TRUE, conf.level = 0.99)) %>%
      unnest(tidy)
    

    Both are equivalent just differ in syntax