Hello all,
I am using the survey package to analyze the European Social Survey, 2018. After specifying the survey design object and running an analysis command, such as getting mean trust in parliament by age group and country, it works fine, apparently, via the console and script.
But running these lines through RMarkdown results in a lonely psu error. Since it works via the console, I don't think I've erred in specifying the complex survey design.
Is there an option for the RMarkdown code chunk that would resolve this problem? Any advice or an explanation of what is going on would be really appr. ciated. Thank you!
library(tidyverse)
library(survey)
# ESS data here: https://www.dropbox.com/s/ceq02i2rkyaqrwk/ESS9e03_1.sav
# brief codebook https://www.dropbox.com/s/d9jefeuht8bu6yo/appendix%20general%20codebook.pdf
ess9<-foreign::read.spss("ESS9e03_1.sav", to.data.frame=TRUE)
ess9<-ess9 %>%
mutate(age=agea)
ess9<-ess9 %>%
mutate(age = na_if(agea, 999))
ess9<-ess9 %>%
mutate(agecat= case_when(
age %in% 15:25 ~ "1. 15 to 25",
age %in% 26:36 ~ "2. 26 to 36",
age %in% 37:47 ~ "3. 37 to 47",
age %in% 48:58 ~ "4. 48 to 58",
age %in% 59:69 ~ "5. 59 to 69",
age %in% 70:90 ~ "6. 70 to 90"))
ess9<-ess9 %>%
mutate(agecat = fct_relevel(agecat, "1. 15 to 25","2. 26 to 36","3. 37 to 47","4. 48 to 58","5. 59 to 69","6. 70 to 90"))
ess9<-ess9 %>%
mutate(trust_parliament = trstprl) %>%
mutate(trust_parliament = fct_recode(trust_parliament,
NULL = "Refusal",
NULL = "Don't know",
NULL = "No answer"))
ess9$anweight <- ess9$pweight*ess9$pspwght
ess_design<-svydesign(weights=~anweight, ids=~psu, strata=~stratum, survey.lonely.psu = "remove", data = ess9, nest=TRUE)
svyby(~as.numeric(trust_parliament), ~ agecat, ess_design, na.rm=TRUE, svymean)
svyby(~as.numeric(trust_parliament), ~ agecat + cntry, ess_design, na.rm=TRUE, svymean)