Weight Factors
Calculate weight factors for survey data to approximate a representative sample
Installation
pip install weightfactors
or clone and install from source
git clone https://github.com/markteffect/weightfactors
cd weightfactors
poetry install
Usage
Currently, the package implements a generalized raking algorithm.
If you'd like to see support for other algorithms, please open an issue or submit a pull request.
Let's use the following dataset as an example:
sample = pd.DataFrame(
{
"Gender": [
"Male",
"Male",
"Female",
"Female",
"Female",
"Male",
"Female",
"Female",
"Male",
"Female",
],
"Score": [7.0, 6.0, 8.5, 7.5, 8.0, 5.0, 9.5, 8.0, 4.5, 8.5],
}
)
Suppose our sample comprises 40% males and 60% females.
If we were to calculate the average score, we would get:
np.average(sample["Score"])
# 7.25
Now, assuming a 50/50 gender distribution in the population,
let's calculate weight factors to approximate the population distribution:
from weightfactors import GeneralizedRaker
raker = GeneralizedRaker({"Gender": {"Male": 0.5, "Female": 0.5}})
weights = raker.rake(sample)
# [1.25000008 1.25000008 0.83333334 0.83333334 0.83333334 1.25000008
# 0.83333334 0.83333334 1.25000008 0.83333334]
Let's calculate the average score again, this time applying the weight factors:
np.average(sample["Score"], weights=weights)
# 6.9791666284520835
For more detailed information and customization options, please refer to the docstrings.
Metadata
Release files for weightfactors 0.0.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| weightfactors-0.0.11.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| weightfactors-0.0.11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.9 kB
Release files / weightfactors-0.0.11.tar.gz
| Download URL | weightfactors-0.0.11.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7e1ddaf6f8f2de95c9925f3bb88c0fcc31e3b67d65cfb22367958958ee941cec
|
|
BLAKE2b-256 checksum How to use checksums |
0f7af7d402d874350747d3922a5955218dbf2920650db30a834a9cc4db058cbb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.3 Windows/11
|
Release files / weightfactors-0.0.11-py3-none-any.whl
| Download URL | weightfactors-0.0.11-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
383fb02aaca722910e13bb5085b01e9980ccdd6780c69004b72e9d14f042f0a6
|
|
BLAKE2b-256 checksum How to use checksums |
57db49907d35dc9aa45d1e5eb9f5cd082761fb5bf4c89383b68cdae8ef20ac27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.3 Windows/11
|