相关文章推荐
独立的皮带  ·  修改 input ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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)
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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.