Skip to content

Survey Tools

Claude edited this page Mar 15, 2026 · 1 revision

Survey Tools Module

The survey_tools/ directory contains R scripts for survey design calculations, sampling weights, and complex survey analysis.


Scripts

sample_size_calculator.R

Calculates required sample sizes for three common designs:

Function Design
simple_sample_size(p, e, z, N) Simple random sampling with finite population correction
stratified_sample_size(strata_df, e, z) Neyman allocation across strata
cluster_sample_size(n_simple, deff) Cluster sampling with design effect adjustment
estimate_deff(icc, m) Design effect estimation from ICC and cluster size
source("survey_tools/sample_size_calculator.R")

n <- simple_sample_size(p = 0.5, e = 0.05, z = 1.96, N = 10000)
deff <- estimate_deff(icc = 0.05, m = 20)
n_cluster <- cluster_sample_size(n, deff)

sampling_weights.R

Computes and adjusts sampling weights:

Function Purpose
compute_base_weights(df, strata_col, pop_col, sample_col) Base weights from sampling fractions
trim_weights(weights, lower, upper) Percentile-based weight trimming
weighted_summary(df, var, weight_col) Weighted mean, SE, and confidence intervals
source("survey_tools/sampling_weights.R")

df <- compute_base_weights(df, "district", "pop_size", "sample_size")
df$weight_trimmed <- trim_weights(df$base_weight, lower = 0.05, upper = 0.95)
weighted_summary(df, "income", "weight_trimmed")

survey_summary.R

Creates complex survey design objects and produces weighted statistics using the survey package:

Function Purpose
create_survey_design(df, strata, cluster, weights) Survey design object with strata and clusters
survey_descriptives(design, vars) Weighted means and totals with SEs
survey_proportions(design, formula) Weighted proportions with CIs
source("survey_tools/survey_summary.R")

design <- create_survey_design(df, strata = ~district, cluster = ~psu, weights = ~weight)
survey_descriptives(design, vars = c("income", "age", "education_years"))
survey_proportions(design, ~factor(employed))

Prerequisites

  • R 4.0+
  • survey package (for survey_summary.R)
  • Base R sufficient for sample_size_calculator.R and sampling_weights.R

Clone this wiki locally