Here are basic commands to calculate averages with standard errors and plot trends over time using both survey and srvyr packages:
r Copy code
# Load required packages
library(survey)
library(srvyr)
library(dplyr)
library(ggplot2)
# Assuming your data has:
# - year: time variable
# - income, education, health: variables of interest
# - wt: survey weights
# - psu: primary sampling unit
# - strata: stratification variable
# ---- APPROACH 1: Using 'survey' package ----
# Create survey design object
svy_design <- svydesign(
id = ~psu,
strata = ~strata,
weights = ~wt,
data = mydata,
nest = TRUE
)
# Calculate means by year for multiple variables
results_income <- svyby(~income, ~year, svy_design, svymean)
results_education <- svyby(~education, ~year, svy_design, svymean)
results_health <- svyby(~health, ~year, svy_design, svymean)
# Combine results for plotting
plot_data <- data.frame(
year = results_income$year,
income_mean = results_income$income,
income_se = sqrt(results_income$`se.income`),
education_mean = results_education$education,
education_se = sqrt(results_education$`se.education`),
health_mean = results_health$health,
health_se = sqrt(results_health$`se.health`)
)
# Plot trends with error bars
ggplot(plot_data, aes(x = year, y = income_mean)) +
geom_line() +
geom_ribbon(aes(ymin = income_mean - 1.96*income_se,
ymax = income_mean + 1.96*income_se), alpha = 0.2) +
labs(title = "Income Trends Over Time",
y = "Mean Income",
x = "Year") +
theme_minimal()
# ---- APPROACH 2: Using 'srvyr' package (tidyverse style) ----
# Create srvyr design object
svy_tidy <- mydata %>%
as_survey_design(
ids = psu,
strata = strata,
weights = wt
)
# Calculate means and SEs for all variables by year
results_tidy <- svy_tidy %>%
group_by(year) %>%
summarize(
income_mean = survey_mean(income, vartype = "se"),
income_se = survey_mean(income, vartype = "se")[[2]],
education_mean = survey_mean(education, vartype = "se"),
education_se = survey_mean(education, vartype = "se")[[2]],
health_mean = survey_mean(health, vartype = "se"),
health_se = survey_mean(health, vartype = "se")[[2]]
)
# Plot all variables on separate panels
results_tidy %>%
# Reshape to long format for faceting
tidyr::pivot_longer(
cols = -year,
names_to = c("variable", "stat"),
names_pattern = "(.*)_(.*)"
) %>%
tidyr::pivot_wider(
names_from = stat,
values_from = value
) %>%
# Create plot
ggplot(aes(x = year, y = mean)) +
geom_line() +
geom_ribbon(aes(ymin = mean - 1.96*se,
ymax = mean + 1.96*se), alpha = 0.2) +
facet_wrap(~variable, scales = "free_y") +
labs(title = "Trends Over Time",
y = "Weighted Mean",
x = "Year") +
theme_minimal()
# Alternative: Plot multiple variables on same graph with different colors
results_tidy %>%
ggplot() +
geom_line(aes(x = year, y = income_mean, color = "Income")) +
geom_line(aes(x = year, y = education_mean, color = "Education")) +
geom_line(aes(x = year, y = health_mean, color = "Health")) +
labs(title = "Trends Over Time",
y = "Standardized Value",
x = "Year",
color = "Variable") +
scale_color_manual(values = c("Income" = "blue",
"Education" = "red",
"Health" = "green")) +
theme_minimal()