Problem using srvyr package

Help everyone. I am currently trying to analyse a complex survey dataset, and I need to create a survey object in order to carry out weighted statistics. I am trying to use the srvyr package since it is easier to subsequently make plots with ggplot, but I am facing an error which I don't really know how to solve. Below Is my code:

if (!require(c("devtools","srvyr","tidyr","xtable","survey","readr","dplyr","plyr","ggplot2","readxl","tidyverse","svyPVpack","furrr","ggthemes","wesanderson"))) install.packages(c("devtools","srvyr","tidyr","xtable","survey","readr","dplyr","plyr","ggplot2","readxl","tidyverse","svyPVpack","furrr","ggthemes","wesanderson"))
library(c("devtools","srvyr","tidyr","xtable","survey","readr","dplyr","plyr","ggplot2","readxl","tidyverse","svyPVpack","furrr","ggthemes","wesanderson"))
packages <- c("devtools","srvyr","tidyr","xtable","survey","readr","dplyr","plyr","ggplot2","readxl","tidyverse","svyPVpack","furrr","ggthemes","wesanderson")
lapply(packages, library, character.only = TRUE)

setwd("~/Documents/Tesi/RECS/2015")
recs2015_public_v4 <- read_excel("recs2015_public_v4.xlsx")
recs=as.data.frame(recs2015_public_v4)
rm(recs2015_public_v4)

recs_design1=recs %>%
as_survey_rep(type="Fay",rho=0.5,repweights=starts_with("brrwt"), weights=nweight,combined.weights=FALSE)

p1<- recs_design1 %>%
group_by(hhsex) %>%
summarise(race = survey_mean(householder_race, na.rm=TRUE))

Error: Survey context not set
Call rlang::last_error() to see a backtrace.

Could you please give me an hint on how to solve this problem? Thanks you all!

Hi, welcome!

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help. In particular, we don’t have access to the data on your local computer.

1 Like

Hey! Thank you for replying. Below is the code I hope will help enough:

setwd("~/Documents/Tesi/RECS/2015")
recs2015_public_v4 <- read_excel("recs2015_public_v4.xlsx")
recs=as.data.frame(recs2015_public_v4)
rm(recs2015_public_v4)
attach(recs)
names(recs)=tolower(names(recs)) # All variables in lowercase
recs_design1=recs %>%

  • as_survey_rep(type="Fay",rho=0.5,repweights=starts_with("brrwt"), weights=nweight,combined.weights=FALSE)

recs_design1
Call: Called via srvyr
Fay's variance method (rho= 0.5 ) with 96 replicates.
Sampling variables:

  • repweights: brrwt1 + brrwt2 + brrwt3 + brrwt4 + brrwt5 + brrwt6 + brrwt7 + brrwt8 + brrwt9 + brrwt10 + brrwt11 + brrwt12 + brrwt13 + brrwt14 + brrwt15 + brrwt16 + brrwt17 + brrwt18 + brrwt19 + brrwt20 + brrwt21 + brrwt22 + brrwt23 + brrwt24 + brrwt25 + brrwt26 + brrwt27 + brrwt28 + brrwt29 + brrwt30 + brrwt31 + brrwt32 + brrwt33 + brrwt34 + brrwt35 + brrwt36 + brrwt37 + brrwt38 + brrwt39 + brrwt40 + brrwt41 + brrwt42 + brrwt43 + brrwt44 + brrwt45 + brrwt46 + brrwt47 + brrwt48 + brrwt49 + brrwt50 + brrwt51 + \n brrwt52 + brrwt53 + brrwt54 + brrwt55 + brrwt56 + brrwt57 + brrwt58 + brrwt59 + brrwt60 + brrwt61 + brrwt62 + brrwt63 + brrwt64 + brrwt65 + brrwt66 + brrwt67 + brrwt68 + brrwt69 + brrwt70 + brrwt71 + brrwt72 + brrwt73 + brrwt74 + brrwt75 + brrwt76 + brrwt77 + brrwt78 + brrwt79 + brrwt80 + brrwt81 + brrwt82 + brrwt83 + brrwt84 + brrwt85 + brrwt86 + brrwt87 + brrwt88 + brrwt89 + brrwt90 + brrwt91 + brrwt92 + brrwt93 + brrwt94 + brrwt95 + brrwt96
  • weights: nweight

svytotal(~hhsex,recs_design1)
total SE
hhsexFemale 66697844 895315
hhsexMale 51510406 895315

svytotal(~householder_race,recs_design1)
total SE
householder_raceWhite 95409173 1147801
householder_raceBlack or African/American 13606140 1095251
householder_raceAmerican Indian or Alaska Native 1297035 264046
householder_raceAsian 4945446 410667
householder_raceHawaiian or Pacific Island 435490 108994
householder_raceOther Race 0 0
householder_raceOr more race 2514967 252289

p1<- recs_design1 %>%

  • group_by(hhsex) %>%
  • summarise(race = survey_mean(householder_race, na.rm=TRUE))
    Error: Survey context not set
    Call rlang::last_error() to see a backtrace.

I don't understand why when I use commands from the "survey" package, I do not get errors; this suggests me that the survey design object has been correctly created. However, when I try to make statistics using the srvyr package, I get this error!

@mfherman is dead on about reprex but I can offer some insight.

When you see Error, look for the function immediately above it, in this case

summarise(race = survey_mean(householder_race, na.rm=TRUE))

and then open help(summarise) and run the example

library(srvyr)
#> 
#> Attaching package: 'srvyr'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(survey)
#> Loading required package: grid
#> Loading required package: Matrix
#> Loading required package: survival
#> 
#> Attaching package: 'survey'
#> The following object is masked from 'package:graphics':
#> 
#>     dotchart
data(api)

dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

dstrata %>%
  summarise(api99 = survey_mean(api99),
            api00 = survey_mean(api00),
            api_diff = survey_mean(api00 - api99))
#> # A tibble: 1 x 6
#>   api99 api99_se api00 api00_se api_diff api_diff_se
#>   <dbl>    <dbl> <dbl>    <dbl>    <dbl>       <dbl>
#> 1  629.     10.1  662.     9.54     32.9        2.08

Created on 2019-11-10 by the reprex package (v0.3.0)

The next question is why the difference? For that, look at the top of help(summarise) and help(survey_mean`) for the required arguments to those functions.

Finally, you have namespace conflicts between dplyr and srvyr for at least summarise, so use srvyr::sumarise as needed.

@technocrat Thank you! This is what I am getting after running the example you suggested me:

library(srvyr)
library(survey)
help(summarise)
data(api)
dstrata <- apistrat %>%
as_survey_design(strata = stype, weights = pw)
dstrata %>%
summarise(api99 = survey_mean(api99),
api00 = survey_mean(api00),
api_diff = survey_mean(api00 - api99))
Error: Survey context not set
Call rlang::last_error() to see a backtrace.

I still get the error even if using the example. Am I missing some packages or what? I don't manage to figure it out.

1 Like

Try restarting your R session and cut and pasting just the example code . If it still throws the error please post your sessionInfo()

sessionInfo()
#> R version 3.6.1 (2019-07-05)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Mojave 10.14.6
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] compiler_3.6.1  magrittr_1.5    tools_3.6.1     htmltools_0.4.0
#>  [5] yaml_2.2.0      Rcpp_1.0.3      stringi_1.4.3   rmarkdown_1.16 
#>  [9] highr_0.8       knitr_1.25      stringr_1.4.0   xfun_0.10      
#> [13] digest_0.6.22   rlang_0.4.1     evaluate_0.14

Created on 2019-11-10 by the reprex package (v0.3.0)

I restarted the session and only loaded those two packages along with the example, It worked.
I then tried to load packages one by one and I found that the problem is with the package plyr, which hides some functions, among which "summarise". That's why it wasn't working! Thank you a lot for suggesting me restarting R, that gave me the hint for trying that other way.

1 Like

Great. Please mark as solution for the benefit of anyone else who has the problem. Whenever there's a problem with namespace conflict among packages that are loaded, you can always call the one you want with srvyr::summarize

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.