结合 tidyverse + survey [R]:如何在 Nest-Map-Unnest-Chain 中使用 svyglm?

Combing tidyverse + survey [R]: How to use svyglm in Nest-Map-Unnest-Chain?

我目前正在努力 运行 R 中多个变量的加权回归模型。

在使用(非加权)glm 时,我通过 运行以下操作获得成功:

mtcars_1 <- mtcars %>%
  nest(-gear)%>%
  mutate(model_0      = map(data, ~ glm(vs ~ drat, family = "binomial", data = .)))%>%
  mutate(model_0_tidy = map(model_0, tidy))%>%
  select(gear, model_0_tidy)%>%
  ungroup()%>%
  unnest(model_0_tidy)

那是我收到以下信息:

# A tibble: 6 x 6
   gear term        estimate std.error statistic p.value
  <dbl> <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1     4 (Intercept)  -15.3       22.6     -0.677   0.499
2     4 drat           4.26       5.76     0.740   0.459
3     3 (Intercept)   -3.91       7.39    -0.529   0.597
4     3 drat           0.801      2.32     0.345   0.730
5     5 (Intercept)    5.20      14.4      0.362   0.718
6     5 drat          -1.71       3.77    -0.453   0.651

但是,当我想对我的观察进行加权并因此使用调查包中的 svyglm 时,嵌套不起作用。

这是我的方法:

design_0 <- svydesign(ids=~0, data = mtcars, weights = mtaars$wt)

mtcars_2 <- mtcars%>%
  nest(-gear)%>%
  mutate(model_1 = map(data, ~ svyglm(vs ~ drat, family = quasibinomial(logit), design = design_0, data = .)))%>%
  mutate(model_1_tidy = map(model_1, tidy))%>%
  select(gear, model_1_tidy)%>%
  ungroup()%>%
  unnest(model_1_tidy)

# If suggested that wt serves as frequency weight

# Outcome

   gear term        estimate std.error statistic p.value
  <dbl> <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1     4 (Intercept)    -8.12      3.88     -2.09  0.0451
2     4 drat            2.12      1.07      1.99  0.0554
3     3 (Intercept)    -8.12      3.88     -2.09  0.0451
4     3 drat            2.12      1.07      1.99  0.0554
5     5 (Intercept)    -8.12      3.88     -2.09  0.0451
6     5 drat            2.12      1.07      1.99  0.0554

每种齿轮(即 3、4、5)的估计结果是相同的。

这里似乎基本上忽略了嵌套。

有没有结合svyglm和nest-map-unnest的解决方案?还是我必须寻找其他不太舒服的方式?

谢谢!

尝试这样做

  mtcars%>%
  nest(-gear) %>% 
  mutate(design = map(data, ~ svydesign(ids=~0, data = .x, weights = ~ wt)),
         model = map(.x = design,
                     .f = ~ svyglm(vs ~ drat,
                                   family = quasibinomial(logit),
                                   design = .x))) %>% 
  mutate(model_tidy = map(model, tidy)) %>% 
  select(gear, model_tidy)%>%
  ungroup()%>%
  unnest(model_tidy)