#Only to be run locally to link to vignette from GitHub README rmarkdown::render("vignettes/survey-functions.Rmd", output_file="doc/survey-functions.html")
knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Large-scale social surveys often do not aim to recruit random samples, but rather oversample small groups that are of interest for sub-group analyses (e.g., ethnic minorities). In addition, they never quite succeed in recruiting representative samples. To ensure that the results of statistical models approximate population parameters, survey weights need to be used to give each participant a specific weight in analyses. You can find out more in this brief YouTube video or explore the more extensive guide to weights in the European Social Survey as an example.
Many functions in the timesaveR
package have alternatives that accept survey objects instead of dataframes. These functions are designated by the svy_
prefix. To use them, you will first need to use the srvyr
package to create a survey object based on the dataframe.
Below, I show some analyses of the European Social Survey 2014 data using the correct survey weights.
library(timesaveR) library(srvyr) #Create weights (consists of two variables in ESS) ess_health$svy_weight <- ess_health$pspwght * ess_health$pweight ess_survey <- as_survey(ess_health, weights = svy_weight)
Let's create scales for health behaviours and depressive symptoms, each including some reverse coding. For that, we need to pass the survey items (as well as any to be reversed) to the svy_make_scale()
function
depression_items = c("fltdpr", "flteeff", "slprl", "wrhpp", "fltlnl", "enjlf", "fltsd", "cldgng") healthy_eating_items = c("etfruit", "eatveg") depression_reversed = c("wrhpp", "enjlf")
Then, the svy_make_scale()
command can be used to calculate scale scores and display descriptives. It returns the survey object with the new variable added, so that you can use a pipeline to create multiple scales.
ess_survey <- svy_make_scale(ess_survey, depression_items, "depression", reversed = depression_reversed) %>% svy_make_scale(healthy_eating_items, "healthy_eating", r_key = -1)
You can use svy_cor_matrix()
to calculate weighted correlations, and then use report_cor_table()
to present them nicely. To improve the calculation of confidence
intervals, n
should be specified as the weighted number of survey responses in that call.
ess_survey %>% select(agea, health, depression, healthy_eating) %>% svy_cor_matrix() %>% report_cor_table(n = survey_tally(ess_survey)[[1]])
It is often helpful to use different variable names for display. For this, functions in the package typically accept rename tibbles that contain an old
and a new
column. By using the tibble::tribble
notation, they can be entered more cleanly than named character vectors, and the get_rename_tribble()
helper function creates most of the code.
#Call # get_rename_tribbles(ess_health, agea, health, depression, healthy_eating, which = "vars") # to get most of this code var_renames <- tibble::tribble( ~old, ~new, "agea", "Age", "health", "Poor health", "depression", "Depression", "healthy_eating", "Healthy eating" ) # If var_names are provided, only variables included in that argument # are included in the correlation table ess_survey %>% svy_cor_matrix(var_names = var_renames) %>% report_cor_table(n = survey_tally(ess_survey)[[1]])
For now, we cannot add_distributions
to the correlation table while using survey weights.
Running a t-test on survey data is easy with survey::svyttest()
. However, getting
Cohen's d as a measure of effect size, or subsetting to compare just two levels if
there are more in the data is less trivial, and is where svy_cohen_d_pair()
comes in.
svy_cohen_d_pair(ess_survey, "health", "gndr") svy_cohen_d_pair(ess_survey, "health", "cntry", pair = c("DE", "GB"))
svy_pairwise_t_test()
allows to run pairwise t-tests, e.g., as post-hoc comparisons,
between multiple groups in the survey. p-values are adjusted for multiple comparisons,
by default using the Holm-Bonferroni method.
svy_pairwise_t_test(ess_survey, "health", "cntry", cats = c("DE", "GB", "FR"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.