r-使用扫帚::整洁为嵌套数据帧中的线性模型添加置信区间



我一直在尝试遵循Hadley Wickham提出的方法,根据https://r4ds.had.co.nz/many-models.html

我已经设法在下面写下了这段代码来创建多个线性模型:

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

然而,我需要在回归估计周围添加置信区间。看起来我应该能够根据帮助页面使用broom::tidy添加它们,但我无法在上面的代码中正确指定这一点?

您必须在map中指定相关参数。有两种可能性:

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)

两者等价,只是语法不同

最新更新