使用 srvyr 包按组划分的比例

Dan*_*elG 1 r survey dplyr

您好,我有一个带有权重列的数据框,如示例所示:

df <- tibble::tribble(
  ~id, ~edu, ~q_d1, ~q_d2_1, ~weight,
   1L,   1L,    1L,      0L,    1740,
   2L,   1L,    1L,      0L,    1428,
   3L,   2L,    1L,      2L,     496,
   4L,   2L,    1L,      2L,     550,
   5L,   3L,    1L,      1L,    1762,
   6L,   4L,    1L,      0L,    1004,
   7L,   5L,    1L,      0L,     522,
   8L,   3L,    2L,      0L,    1099,
   9L,   4L,    2L,      2L,    1295
  )
Run Code Online (Sandbox Code Playgroud)

我使用 srvyr 包来计算组的汇总统计数据。我的脚本:

sv_design_test <- df %>%
  srvyr::as_survey_design(weights = weight)

sv_design_test %>% 
  dplyr::mutate(smartphone = case_when(
    q_d1 == 2 ~ "No Internet",
    q_d2_1 > 0 ~ "smartphone" ,
    q_d2_1 == 0 ~ "No smartphone" ,
    TRUE ~ NA_character_)) %>% 
  group_by(smartphone) %>% 
  summarize(proportion = srvyr::survey_mean(),
            total = srvyr::survey_total(),
            total_unweighted = srvyr::unweighted(n())) %>% 
  select(-proportion_se, -total_se )
Run Code Online (Sandbox Code Playgroud)

输出:

# A tibble: 3 x 4
  smartphone    proportion total total_unweighted
  <chr>              <dbl> <dbl>            <int>
1 No Internet        0.242  2394                2
2 No smartphone      0.474  4694                4
3 smartphone         0.284  2808                3
Run Code Online (Sandbox Code Playgroud)

但是当我将教育 (edu) 添加到 group_by 时出现错误:

sv_design_test %>% 
  dplyr::mutate(smartphone = case_when(
    q_d1 == 2 ~ "No Internet",
    q_d2_1 > 0 ~ "smartphone" ,
    q_d2_1 == 0 ~ "No smartphone" ,
    TRUE ~ NA_character_)) %>% 
  group_by(edu, smartphone) %>% 
  summarize(proportion = srvyr::survey_mean(),
            total = srvyr::survey_total(),
            total_unweighted = srvyr::unweighted(n())) %>% 
  select(-proportion_se, -total_se )
Run Code Online (Sandbox Code Playgroud)

错误信息是:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels
Run Code Online (Sandbox Code Playgroud)

cam*_*lle 5

问题

您的错误消息(有关对比的错误消息)表明您需要使用因素作为分组变量。在原始数据框中,edu是数字,因此您可以在创建调查设计之前将其转换为因子。

library(tidyverse)
library(srvyr)

# ...

sv_design_test <- df %>%
  mutate(edu = as.factor(edu)) %>%
  srvyr::as_survey_design(weights = weight)
Run Code Online (Sandbox Code Playgroud)

然后在创建 后smartphone,将其也转换为一个因子:

sv_design_test %>% 
  dplyr::mutate(smartphone = case_when(
    q_d1 == 2 ~ "No Internet",
    q_d2_1 > 0 ~ "smartphone" ,
    q_d2_1 == 0 ~ "No smartphone" ,
    TRUE ~ NA_character_)) %>% 
  mutate(smartphone = as.factor(smartphone))
Run Code Online (Sandbox Code Playgroud)

在第二条错误消息(关于长度的错误消息)中,这是因为您的函数中summarise返回不同的行数。您可以通过单独调用这些函数来验证这一点(错误消息显示它是参数 3,意思是n = unweighted(n())问题所在)。

这将返回 15 行:

sv_design_test %>% 
  dplyr::mutate(smartphone = case_when(
    q_d1 == 2 ~ "No Internet",
    q_d2_1 > 0 ~ "smartphone",
    q_d2_1 == 0 ~ "No smartphone",
    TRUE ~ NA_character_)) %>% 
  mutate(smartphone = as.factor(smartphone)) %>%
  group_by(edu, smartphone) %>% 
  summarise(prop = survey_mean(), 
            total = survey_total())
#> # A tibble: 15 x 6
#>    edu   smartphone     prop prop_se total total_se
#>    <fct> <fct>         <dbl>   <dbl> <dbl>    <dbl>
#>  1 1     No Internet   0       0         0       0 
#>  2 1     No smartphone 1       0      3168    2108.
#>  3 1     smartphone    0       0         0       0 
#>  4 2     No Internet   0       0         0       0 
#>  5 2     No smartphone 0       0         0       0 
#>  6 2     smartphone    1       0      1046     693.
#>  7 3     No Internet   0.384   0.355  1099    1099.
#>  8 3     No smartphone 0       0         0       0 
#>  9 3     smartphone    0.616   0.355  1762    1762.
#> 10 4     No Internet   0.563   0.369  1295    1295.
#> 11 4     No smartphone 0.437   0.369  1004    1004 
#> 12 4     smartphone    0       0         0       0 
#> 13 5     No Internet   0       0         0       0 
#> 14 5     No smartphone 1       0       522     522 
#> 15 5     smartphone    0       0         0       0
Run Code Online (Sandbox Code Playgroud)

虽然这仅返回 7,因为edu和只smartphone出现了 7 种组合,因此只计算了 7 个。

sv_design_test %>% 
  dplyr::mutate(smartphone = case_when(
    q_d1 == 2 ~ "No Internet",
    q_d2_1 > 0 ~ "smartphone",
    q_d2_1 == 0 ~ "No smartphone",
    TRUE ~ NA_character_)) %>% 
  mutate(smartphone = as.factor(smartphone)) %>%
  group_by(edu, smartphone) %>%
  summarise(n = unweighted(n()))
#> # A tibble: 7 x 3
#>   edu   smartphone        n
#>   <fct> <fct>         <int>
#> 1 1     No smartphone     2
#> 2 2     smartphone        2
#> 3 3     No Internet       1
#> 4 3     smartphone        1
#> 5 4     No Internet       1
#> 6 4     No smartphone     1
#> 7 5     No smartphone     1
Run Code Online (Sandbox Code Playgroud)

解决方案1:使用.drop = FALSE内部group_by()

通过使用函数的参数,您summarize()甚至可以强制生成数据中未出现的因子水平组合的结果。.dropgroup_by()

sv_design_test %>% 
      dplyr::mutate(smartphone = case_when(
        q_d1 == 2 ~ "No Internet",
        q_d2_1 > 0 ~ "smartphone",
        q_d2_1 == 0 ~ "No smartphone",
        TRUE ~ NA_character_)) %>% 
      mutate(smartphone = as.factor(smartphone)) %>%
      group_by(edu, smartphone,
               .drop = FALSE) %>%
      summarize(prop= srvyr::survey_mean(),
                total = srvyr::survey_total(),
                total_unweighted = srvyr::unweighted(n()))

#> # A tibble: 15 x 7
#>    edu   smartphone     prop prop_se total total_se total_unweighted
#>    <fct> <fct>         <dbl>   <dbl> <dbl>    <dbl> <dbl>
#>  1 1     No Internet   0       0         0       0      0
#>  2 1     No smartphone 1       0      3168    2108.     2
#>  3 1     smartphone    0       0         0       0      0
#>  4 2     No Internet   0       0         0       0      0
#>  5 2     No smartphone 0       0         0       0      0
#>  6 2     smartphone    1       0      1046     693.     2
#>  7 3     No Internet   0.384   0.355  1099    1099.     1
#>  8 3     No smartphone 0       0         0       0      0
#>  9 3     smartphone    0.616   0.355  1762    1762.     1
#> 10 4     No Internet   0.563   0.369  1295    1295.     1
#> 11 4     No smartphone 0.437   0.369  1004    1004      1
#> 12 4     smartphone    0       0         0       0      0
#> 13 5     No Internet   0       0         0       0      0
#> 14 5     No smartphone 1       0       522     522      1
#> 15 5     smartphone    0       0         0       0      0
Run Code Online (Sandbox Code Playgroud)

解决方案2:加入

您可以创建 2 个不同的汇总数据框,然后将它们连接起来。

complete我正在添加对after 的调用n()来填充缺失的级别。制作两个数据框并将它们连接起来会得到以下结果:

props <- sv_design_test %>% 
  dplyr::mutate(smartphone = case_when(
    q_d1 == 2 ~ "No Internet",
    q_d2_1 > 0 ~ "smartphone",
    q_d2_1 == 0 ~ "No smartphone",
    TRUE ~ NA_character_)) %>% 
  mutate(smartphone = as.factor(smartphone)) %>%
  group_by(edu, smartphone) %>% 
  summarise(prop = survey_mean(), 
            total = survey_total())

counts <- sv_design_test %>% 
  dplyr::mutate(smartphone = case_when(
    q_d1 == 2 ~ "No Internet",
    q_d2_1 > 0 ~ "smartphone",
    q_d2_1 == 0 ~ "No smartphone",
    TRUE ~ NA_character_)) %>% 
  mutate(smartphone = as.factor(smartphone)) %>%
  group_by(edu, smartphone) %>%
  summarise(n = unweighted(n())) %>%
  complete(edu, smartphone, fill = list(n = 0))

left_join(props, counts, by = c("edu", "smartphone"))
#> # A tibble: 15 x 7
#>    edu   smartphone     prop prop_se total total_se     n
#>    <fct> <fct>         <dbl>   <dbl> <dbl>    <dbl> <dbl>
#>  1 1     No Internet   0       0         0       0      0
#>  2 1     No smartphone 1       0      3168    2108.     2
#>  3 1     smartphone    0       0         0       0      0
#>  4 2     No Internet   0       0         0       0      0
#>  5 2     No smartphone 0       0         0       0      0
#>  6 2     smartphone    1       0      1046     693.     2
#>  7 3     No Internet   0.384   0.355  1099    1099.     1
#>  8 3     No smartphone 0       0         0       0      0
#>  9 3     smartphone    0.616   0.355  1762    1762.     1
#> 10 4     No Internet   0.563   0.369  1295    1295.     1
#> 11 4     No smartphone 0.437   0.369  1004    1004      1
#> 12 4     smartphone    0       0         0       0      0
#> 13 5     No Internet   0       0         0       0      0
#> 14 5     No smartphone 1       0       522     522      1
#> 15 5     smartphone    0       0         0       0      0
Run Code Online (Sandbox Code Playgroud)