Bootstrap Analysis Documentation¶
Overview¶
Bootstrap analysis is a statistical resampling technique used to estimate the variability (such as variance, standard deviation, and confidence intervals) of a statistic (e.g., prevalence) by repeatedly sampling from the data with replacement. In this project, bootstrapping is used to provide robust estimates of prevalence and associated uncertainty for survey data, accounting for complex survey design via bootstrap weights.
Process Steps¶
1. Data Preparation¶
- Load Main Data: The main survey data is loaded from a Parquet file.
- Load Bootstrap Weights: Bootstrap replicate weights are loaded from a separate Parquet file.
- Merge Data: The main data and bootstrap weights are merged on a unique identifier (e.g.,
ONT_ID). - Filtering: Data can be filtered by geographic or demographic criteria before analysis.
2. Running the Bootstrap Analysis¶
- Function:
run_bootstrap_analysis_for_all_values(merged_data, variable_col, weight_col) - Inputs:
merged_data: DataFrame containing both main data and bootstrap weights.variable_col: The categorical variable for which prevalence is calculated.weight_col: The main survey weight column (e.g.,WTS_S).- Process:
- Identify all bootstrap weight columns (e.g., columns starting with
BSW). - For each value in the selected variable:
- Calculate the weighted sum (numerator) for the main weight and each bootstrap weight.
- Calculate the total sum of weights (denominator) for the main and each bootstrap weight.
- Compute prevalence as (weighted sum / total weight) * 100 for both main and bootstrap weights.
- Calculate variance, standard deviation, and confidence intervals (typically 95%) using the distribution of bootstrap replicate prevalences.
- Calculate the coefficient of variation (CV) and error margin.
- Calculate the weighted population (sum of weights) for each group.
- Outputs: DataFrame with columns for Value, Prevalence, Weighted Population, Variance, Standard Deviation, CI Lower, CI Upper, CV (%), and Error.
3. Display and Interpretation¶
- Results Table: Shows prevalence, weighted population, and uncertainty metrics for each value of the variable.
- Crosstab Report: Allows comparison of prevalence and weighted population across multiple variables and values.
- Visualization: Bar charts with error bars visualize prevalence and uncertainty.
Key Functions¶
run_bootstrap_analysis_for_all_values: Core function for bootstrap analysis.display_results: Presents results in a styled table and chart.display_crosstab_report: Generates crosstab reports for prevalence and weighted population.
Formulas Used in Bootstrap Analysis¶
Let: - \(i\) index the records in the dataset - \(g\) index the groups (values) of the variable being analyzed - \(w_i\) be the main survey weight for record \(i\) - \(w_{i}^{(b)}\) be the \(b\)-th bootstrap weight for record \(i\) - \(y_i\) be an indicator variable (1 if record \(i\) is in group \(g\), 0 otherwise) - \(B\) be the number of bootstrap replicates
Weighted Population¶
For group \(g\):
Weighted Prevalence (Main Weight)¶
For group \(g\):
Weighted Prevalence (Bootstrap Replicates)¶
For each bootstrap replicate \(b\):
Variance (Bootstrap)¶
Standard Deviation¶
95% Confidence Interval¶
Coefficient of Variation (CV)¶
Error Margin (for error bars)¶
Notes¶
- Weighted Population: Represents the estimated population size for each group, calculated as the sum of survey weights.
- Confidence Intervals: Calculated using the CCHS reporting convention of estimate ± 2.0 × bootstrap standard error.
- Bootstrap Weights: Account for survey design and provide more accurate variance estimates than simple random sampling.
Example Usage¶
result_df = run_bootstrap_analysis_for_all_values(merged_data, 'SEX', 'WTS_S')
display_results(result_df, 'SEX')