Survey Data Analysis: Basic Commands for Weighted Averages and Trend Plots
Here are commands to calculate weighted averages, standard errors, and plot trends over time using both survey and srvyr packages:
Setup and Basic Statistics
Using survey package
library(survey)
library(ggplot2)
library(dplyr)
# Create survey design object
svy_design <- svydesign(
ids = ~cluster_var, # Clustering variable
strata = ~strata_var, # Stratification variable
weights = ~weight_var, # Weight variable
data = survey_data,
nest = TRUE
)
# Calculate weighted mean and SE for a single variable
svymean(~income, svy_design)
# Calculate for multiple variables at once
svymean(~income + education + health_score, svy_design)
# Calculate by time periods (e.g., years)
yearly_stats <- svyby(
~income + education + health_score, # Variables to analyze
~year, # Time variable
svy_design, # Survey design
svymean, # Function to apply
keep.names = FALSE # Cleaner output names
)
Using srvyr package (tidyverse-friendly)
library(srvyr)
library(ggplot2)
library(dplyr)
# Create srvyr survey object
svy_data <- survey_data %>%
as_survey_design(
ids = cluster_var,
strata = strata_var,
weights = weight_var
)
# Calculate weighted mean and SE for variables
svy_data %>%
summarize(
income_mean = survey_mean(income),
education_mean = survey_mean(education),
health_mean = survey_mean(health_score)
)
# Calculate by time periods
yearly_stats_tidy <- svy_data %>%
group_by(year) %>%
summarize(
income_mean = survey_mean(income),
education_mean = survey_mean(education),
health_mean = survey_mean(health_score)
)
Plotting Trends Over Time
Using survey + ggplot2
# Convert survey results to data frame for plotting
plot_data <- data.frame(yearly_stats)
# Basic trend plot with error bars
ggplot(plot_data, aes(x = year, y = income)) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymin = income - 1.96*se.income,
ymax = income + 1.96*se.income), width = 0.2) +
labs(title = "Average Income Over Time",
y = "Weighted Mean Income",
x = "Year") +
theme_minimal()
# Multiple variables on separate plots
library(tidyr)
# Reshape data for easier plotting of multiple variables
plot_data_long <- plot_data %>%
pivot_longer(
cols = c(income, education, health_score),
names_to = "variable",
values_to = "mean"
) %>%
mutate(
se = case_when(
variable == "income" ~ se.income,
variable == "education" ~ se.education,
variable == "health_score" ~ se.health_score
)
)
# Create faceted plot for all variables
ggplot(plot_data_long, aes(x = year, y = mean, group = variable)) +
geom_line() +
geom_point() +
geom_errorbar(aes(ymin = mean - 1.96*se, ymax = mean + 1.96*se), width = 0.2) +
facet_wrap(~variable, scales = "free_y") +
labs(title = "Trends Over Time",
y = "Weighted Mean",
x = "Year") +
theme_minimal()
Using srvyr + ggplot2
# The srvyr output is already tidy and ready for plotting
ggplot(yearly_stats_tidy, aes(x = year, y = income_mean)) +
geom_line() +
geom_point() +
geom_ribbon(aes(ymin = income_mean - 1.96*income_mean_se,
ymax = income_mean + 1.96*income_mean_se), alpha = 0.2) +
labs(title = "Average Income Over Time",
y = "Weighted Mean Income",
x = "Year") +
theme_minimal()
# For multiple variables with srvyr
yearly_stats_tidy %>%
pivot_longer(
cols = c(income_mean, education_mean, health_mean),
names_to = "variable",
values_to = "mean"
) %>%
mutate(
variable = gsub("_mean$", "", variable),
se = case_when(
variable == "income" ~ income_mean_se,
variable == "education" ~ education_mean_se,
variable == "health" ~ health_mean_se
)
) %>%
ggplot(aes(x = year, y = mean, color = variable)) +
geom_line() +
geom_point() +
geom_ribbon(aes(ymin = mean - 1.96*se,
ymax = mean + 1.96*se,
fill = variable), alpha = 0.2, color = NA) +
labs(title = "Trends Over Time",
y = "Weighted Mean",
x = "Year") +
theme_minimal()
Bonus: Comparing Subgroups Over Time
# Calculate stats by year AND another grouping variable (e.g., gender)
subgroup_trends <- svy_data %>%
group_by(year, gender) %>%
summarize(
income_mean = survey_mean(income)
)
# Plot subgroup comparison
ggplot(subgroup_trends, aes(x = year, y = income_mean, color = gender)) +
geom_line() +
geom_point() +
geom_ribbon(aes(ymin = income_mean - 1.96*income_mean_se,
ymax = income_mean + 1.96*income_mean_se,
fill = gender), alpha = 0.2, color = NA) +
labs(title = "Income Trends by Gender",
y = "Weighted Mean Income",
x = "Year") +
theme_minimal()
These commands provide a solid foundation for calculating weighted statistics and visualizing trends over time with survey data. You can easily adapt them to your specific variables and research questions.
Would you like me to explain or break down any specific part of this code?