Apply Census weighting to survey data
Project description
Surveyweights
Apply Census weighting to survey data.
Example Usage
from surveyweights import run_weighting_scheme, run_weighting_iteration
# Define what to weigh on
weigh_on = ['age', 'education', 'gender', 'income', 'race', 'urban_rural', 'vote2016']
# Run weighting
output = run_weighting_scheme(survey_data, iters=25, weigh_on=weigh_on)
# Get data back with weight column
survey_data = output['final_df']
# See balance of weights
run_weighting_iteration(survey_data, weigh_on=weigh_on)
# Look at unweighted outcome data
print(survey_data['outcome'].value_counts(normalize=True) * 100)
# Look at weighted outcome data
print(survey_data['outcome'].value_counts(normalize=True) * survey_data.groupby('outcome')['weight'].mean() * 100)
Debugging
Help: the percentages don't sum to 100%!
If you subset the dataset, you subset the weights too and they will no longer work for the subsetted dataset. To fix this, use nomalize_weights
:
# Subset df
subset_df = df[df[var] == subset]
# Look at weighted data (will be wrong and will not sum to 100%!)
print(subset_df[var].value_counts(normalize=True) * subset_df.groupby(var)['weight'].mean() * 100)
# Normalize weights
df['weight'] = nomalize_weights(df['weight'])
# Look at weighted data (it is now fixed and still representative!)
print(subset_df[var].value_counts(normalize=True) * subset_df.groupby(var)['weight'].mean() * 100)
Installation
pip3 install surveyweights
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
surveyweights-0.3.tar.gz
(7.0 kB
view hashes)
Built Distribution
Close
Hashes for surveyweights-0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d113f088d70be40c788f05b184d58e66129eabc561a5348464136103c2e81440 |
|
MD5 | 602d95e54e266fb0ddca2f142959bc4a |
|
BLAKE2b-256 | be39e08ece338d24a91ad3c489a7aec541f6dc29fedbe639f1046d8efb37e80e |