srvyr套餐的按组比例

时间:2018-08-09 17:07:36

标签: r dplyr survey

嗨,我有一个带有权重列的数据框,例如:

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
  )

我使用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 )

输出:

# 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

但是当我将教育(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 )

错误消息是:

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

2 个答案:

答案 0 :(得分:3)

问题

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

library(tidyverse)
library(srvyr)

# ...

sv_design_test <- df %>%
  mutate(edu = as.factor(edu)) %>%
  srvyr::as_survey_design(weights = weight)

然后在创建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))

在第二条错误消息(有关长度的错误消息)中,这是因为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

虽然它仅返回7,因为仅出现edusmartphone的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

解决方案1:在.drop = FALSE中使用group_by()

使用summarize()函数的.drop自变量,即使对于数据中未显示的因子水平组合,也可以强制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_)) %>% 
      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

解决方案2:加入

您可以制作2个不同的摘要数据框架,然后将它们合并。

我在complete之后向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

答案 1 :(得分:1)

似乎您实际上并不需要df %>% 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) %>% summarise(total = sum(weight), total_unweighted = n()) %>% mutate(proportion = prop.table(total)) # A tibble: 3 x 4 smartphone total total_unweighted proportion <chr> <dbl> <int> <dbl> 1 No Internet 2394 2 0.242 2 No smartphone 4694 4 0.474 3 smartphone 2808 3 0.284 df %>% 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) %>% summarise(total = sum(weight), total_unweighted = n()) %>% mutate(proportion = prop.table(total)) # A tibble: 7 x 5 # Groups: edu [5] edu smartphone total total_unweighted proportion <int> <chr> <dbl> <int> <dbl> 1 1 No smartphone 3168 2 1 2 2 smartphone 1046 2 1 3 3 No Internet 1099 1 0.384 4 3 smartphone 1762 1 0.616 5 4 No Internet 1295 1 0.563 6 4 No smartphone 1004 1 0.437 7 5 No smartphone 522 1 1

{{1}}