user11841097
user11841097

Reputation: 79

Error when grouping data with dplyr and survey

I use the R-packages survey and srvyr in combination with dplyr to analyse survey data. However, when I try to calculate confidence intervals for groups`(see code below), I get the error 'group_by_drop_default' is not an exported object from 'namespace:dplyr'

Thanks for any help regarding this error or calculating confidence intervals of groups in the survey framework in general.

  as_survey_design(strata = strata, weight  = weight_pers, id= hh_id, nest=TRUE)

out <- strat_design %>%
  group_by(sex, year) %>%
  summarize( var_mean= survey_mean(var1, vartype = "ci"),
            n = unweighted(n()))```

Upvotes: 0

Views: 590

Answers (2)

GregF
GregF

Reputation: 1392

Unfortunately it looks like there is an incompatibility between your version of dplyr and srvyr.

dplyr introduced that function in 0.8.1, and srvyr started to depend on it in version 0.3.6.

The easiest solution is to update dplyr with install.packages("dplyr"), but if you can’t update it then you could try to install an older version of srvyr (stackoverflow post on how that would work)

Upvotes: 0

Edward
Edward

Reputation: 18653

Your commands have some errors. I don't have your data, so I'll use the apistrat dataset from the survey package as an example. You can use this to modify your own commands accordingly.

library(survey)
library(srvyr)

data(api)

out <- apistrat %>%
  as_survey_design(strata = stype, weights = pw) %>%
  group_by(awards) %>%
  summarize( var_mean = survey_mean(api00, vartype = "ci"),
             n = unweighted(n()) )

out

# A tibble: 2 x 5
  awards var_mean var_mean_low var_mean_upp     n
  <fct>     <dbl>        <dbl>        <dbl> <int>
1 No         634.         603.         664.    87
2 Yes        678.         655.         702.   113

Upvotes: 1

Related Questions