|
| 1 | +# Sample Size Calculator for Survey Design |
| 2 | +# Covers simple random, stratified, and cluster sampling designs |
| 3 | + |
| 4 | +#' Calculate sample size for simple random sampling |
| 5 | +#' @param p Expected proportion (default 0.5 for maximum variance) |
| 6 | +#' @param margin Margin of error (default 0.05) |
| 7 | +#' @param confidence Confidence level (default 0.95) |
| 8 | +#' @param population Population size (NULL for infinite) |
| 9 | +#' @return Required sample size |
| 10 | +sample_size_simple <- function(p = 0.5, margin = 0.05, confidence = 0.95, population = NULL) { |
| 11 | + z <- qnorm(1 - (1 - confidence) / 2) |
| 12 | + n <- (z^2 * p * (1 - p)) / margin^2 |
| 13 | + |
| 14 | + # Finite population correction |
| 15 | + if (!is.null(population)) { |
| 16 | + n <- n / (1 + (n - 1) / population) |
| 17 | + } |
| 18 | + ceiling(n) |
| 19 | +} |
| 20 | + |
| 21 | +#' Calculate sample size for stratified sampling |
| 22 | +#' @param strata_sizes Vector of stratum population sizes |
| 23 | +#' @param strata_proportions Expected proportion per stratum |
| 24 | +#' @param margin Margin of error |
| 25 | +#' @param confidence Confidence level |
| 26 | +#' @param allocation Allocation method: "proportional" or "equal" |
| 27 | +#' @return Data frame with stratum-wise sample sizes |
| 28 | +sample_size_stratified <- function(strata_sizes, strata_proportions = NULL, |
| 29 | + margin = 0.05, confidence = 0.95, |
| 30 | + allocation = "proportional") { |
| 31 | + k <- length(strata_sizes) |
| 32 | + if (is.null(strata_proportions)) strata_proportions <- rep(0.5, k) |
| 33 | + |
| 34 | + N <- sum(strata_sizes) |
| 35 | + z <- qnorm(1 - (1 - confidence) / 2) |
| 36 | + |
| 37 | + # Total sample size |
| 38 | + n_total <- sample_size_simple(p = 0.5, margin = margin, confidence = confidence, population = N) |
| 39 | + |
| 40 | + # Allocate across strata |
| 41 | + if (allocation == "proportional") { |
| 42 | + weights <- strata_sizes / N |
| 43 | + } else { |
| 44 | + weights <- rep(1 / k, k) |
| 45 | + } |
| 46 | + |
| 47 | + n_strata <- ceiling(n_total * weights) |
| 48 | + |
| 49 | + data.frame( |
| 50 | + stratum = seq_len(k), |
| 51 | + population = strata_sizes, |
| 52 | + proportion = strata_proportions, |
| 53 | + sample_size = n_strata |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +#' Calculate sample size for cluster randomized designs |
| 58 | +#' @param icc Intra-cluster correlation coefficient |
| 59 | +#' @param cluster_size Average number of units per cluster |
| 60 | +#' @param p Expected proportion |
| 61 | +#' @param margin Margin of error |
| 62 | +#' @param confidence Confidence level |
| 63 | +#' @return List with design effect, effective sample size, and clusters needed |
| 64 | +sample_size_cluster <- function(icc = 0.05, cluster_size = 30, |
| 65 | + p = 0.5, margin = 0.05, confidence = 0.95) { |
| 66 | + # Design effect |
| 67 | + deff <- 1 + (cluster_size - 1) * icc |
| 68 | + |
| 69 | + # Simple sample size |
| 70 | + n_simple <- sample_size_simple(p = p, margin = margin, confidence = confidence) |
| 71 | + |
| 72 | + # Adjusted for clustering |
| 73 | + n_effective <- ceiling(n_simple * deff) |
| 74 | + n_clusters <- ceiling(n_effective / cluster_size) |
| 75 | + |
| 76 | + list( |
| 77 | + design_effect = round(deff, 2), |
| 78 | + simple_sample_size = n_simple, |
| 79 | + effective_sample_size = n_effective, |
| 80 | + clusters_needed = n_clusters, |
| 81 | + total_sample = n_clusters * cluster_size, |
| 82 | + icc = icc, |
| 83 | + cluster_size = cluster_size |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +# Example usage |
| 88 | +if (sys.nframe() == 0) { |
| 89 | + cat("=== Simple Random Sampling ===\n") |
| 90 | + cat(sprintf("50%% proportion, 5%% margin: n = %d\n", sample_size_simple())) |
| 91 | + cat(sprintf("30%% proportion, 3%% margin: n = %d\n", sample_size_simple(p = 0.3, margin = 0.03))) |
| 92 | + cat(sprintf("With population 10000: n = %d\n", sample_size_simple(population = 10000))) |
| 93 | + |
| 94 | + cat("\n=== Stratified Sampling ===\n") |
| 95 | + strat <- sample_size_stratified( |
| 96 | + strata_sizes = c(5000, 3000, 2000), |
| 97 | + allocation = "proportional" |
| 98 | + ) |
| 99 | + print(strat) |
| 100 | + |
| 101 | + cat("\n=== Cluster Sampling ===\n") |
| 102 | + cluster <- sample_size_cluster(icc = 0.05, cluster_size = 30) |
| 103 | + cat(sprintf("Design effect: %.2f\n", cluster$design_effect)) |
| 104 | + cat(sprintf("Clusters needed: %d\n", cluster$clusters_needed)) |
| 105 | + cat(sprintf("Total sample: %d (vs %d simple)\n", cluster$total_sample, cluster$simple_sample_size)) |
| 106 | +} |
0 commit comments