首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在嵌套数据框架中使用扫描::整齐将置信区间添加到线性模型中

在嵌套数据框架中使用扫描::整齐将置信区间添加到线性模型中
EN

Stack Overflow用户
提问于 2022-02-09 10:10:58
回答 1查看 302关注 0票数 1

我一直在尝试按照Hadley提出的方法在嵌套数据框架中按照https://r4ds.had.co.nz/many-models.html运行多个模型

我在下面编写了下面的代码来创建多个线性模型:

代码语言:javascript
复制
#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按照帮助页添加它们,但是我无法在上面的代码中正确地指定这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-09 10:25:09

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

代码语言:javascript
复制
by_clin_grp_unnest <- by_clin_grp %>%
  mutate(tidy = map(model, ~broom::tidy(.x, conf.int = TRUE, conf.level = 0.99)))%>%
  unnest(tidy)

代码语言:javascript
复制
by_clin_grp_unnest <- by_clin_grp %>%
  mutate(tidy = map(model, broom::tidy, conf.int = TRUE, conf.level = 0.99)) %>%
  unnest(tidy)

两者都是等价的,只是语法不同。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71047639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档