循环列表R中的多个GLM对象的变量列表

发布于 2025-01-23 14:07:13 字数 655 浏览 1 评论 0原文

我的一生无法理解为什么在R :(

## Unadjusted model 
noadj_var <- list("a",
                  "b",
                  "c",
                  "d",
                  )

for (i in 1:length(noadj_var)) {
  paste0("unadjusted_model_",i) <- glm(
    df$dependent_variable ~ i,
    data = df[df$present_2018 == "Yes" , ],
    family = binomial(link=logit),
    na.action = na.exclude
  )
}

我有错误:

Error in model.frame.default(formula = df$dependent_variable ~ i, data = df[df$present_2018 ==  : 
 variable lengths differ (found for 'i')

我不明白为什么我有na.Action = Na.exclude时

会出现 是搞砸了吗?

此错误那 。

I can not for the life of me see why this isn't working for me in R:(

## Unadjusted model 
noadj_var <- list("a",
                  "b",
                  "c",
                  "d",
                  )

for (i in 1:length(noadj_var)) {
  paste0("unadjusted_model_",i) <- glm(
    df$dependent_variable ~ i,
    data = df[df$present_2018 == "Yes" , ],
    family = binomial(link=logit),
    na.action = na.exclude
  )
}

I get the error:

Error in model.frame.default(formula = df$dependent_variable ~ i, data = df[df$present_2018 ==  : 
 variable lengths differ (found for 'i')

I don't understand why I get this error when I have na.action = na.exclude

Is there something else that is being messed up?

Just to preface this the variable names "a", "b", "c" are not the real names, and I can't just change them to a sequence, it needs to be a list of strings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2025-01-30 14:07:13

purrr软件包的另一种可能性:

 list("a",
       "b",
       "c",
       "d",
  ) %>% 
    purrr::set_names(paste0("unadjusted_model_", .)) %>% 
    purrr::map(~glm(reformulate(.x, 'dependent_variable'),data = df),
               family = binomial(link=logit),
               na.action = na.exclude ) %>% 
    purrr::map(broom::tidy)

Another possibility with the purrr package:

 list("a",
       "b",
       "c",
       "d",
  ) %>% 
    purrr::set_names(paste0("unadjusted_model_", .)) %>% 
    purrr::map(~glm(reformulate(.x, 'dependent_variable'),data = df),
               family = binomial(link=logit),
               na.action = na.exclude ) %>% 
    purrr::map(broom::tidy)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文