Skip to main content

No project description provided

Project description

Weightipy

Weightipy is a cut down version of Quantipy3 for weighting people data using the RIM (iterative raking) algorithm.

Planned features

  • Support for multithreaded weighting
  • Support for more weighting algorithms
  • Rewrite of the API to be less oriented towards how Quantipy worked and more in line with simple weighting needs

Origins

Contributors

Installation

pip install weightipy

or

python3 -m pip install weightipy

Note that the package is called weightipy on pip.

Create a virtual envirionment

If you want to create a virtual environment when using Weightipy:

conda

conda create -n envwp python=3

with venv

python -m venv [your_env_name]

5-minutes to Weightipy

Get started

Weighting

If your data hasn't been weighted yet, you can use Weightipy's RIM weighting algorithm.

Assuming we have the variables gender and agecat we can weight the dataset with these two variables:

import weightipy as wp

targets = {
    "agecat": {"18-24": 5.0, "25-34": 30.0, "35-49": 26.0, "50-64": 19.0, "65+": 20.0},
    "gender": {"Male": 49, "Female": 51}
}
scheme = wp.scheme_from_dict(targets)

df_weighted = wp.weight_dataframe(
    df=my_df,
    scheme=scheme,
    weight_column="weights"
)
efficiency = wp.weighting_efficiency(df_weighted["weights"])

Or if we want more control of the raking process, we can use the Rim class directly:

import weightipy as wp

age_targets = {'agecat':{1:5.0, 2:30.0, 3:26.0, 4:19.0, 5:20.0}}
gender_targets = {'gender':{0:49, 1:51}}
scheme = wp.Rim('gender_and_age')
scheme.set_targets(targets=[age_targets, gender_targets])

df_weighted = wp.weight_dataframe(
    df=my_df,
    scheme=scheme,
    weight_column="weights"
)
efficiency = wp.weighting_efficiency(df_weighted["weights"])

Or by using the underlying functions that will give more access to reports etc:

...

my_df["identity"] = range(len(my_df))
engine = wp.WeightEngine(data=df)
engine.add_scheme(scheme=scheme, key="identity", verbose=False)
engine.run()
df_weighted = engine.dataframe()
col_weights = f"weights_{scheme.name}"

efficiency = wp.weighting_efficiency(df_weighted[col_weights])

print(engine.get_report())

Weight variable       weights_gender_and_age
Weight group                  _default_name_
Weight filter                           None
Total: unweighted                 582.000000
Total: weighted                   582.000000
Weighting efficiency               60.009826
Iterations required                14.000000
Mean weight factor                  1.000000
Minimum weight factor               0.465818
Maximum weight factor               6.187700
Weight factor ratio                13.283522

Contributing

The test suite for Weightipy can be run with the command

python3 -m pytest tests

But when developing a specific aspect of Weightipy, it might be quicker to run (e.g. for the DataSet)

python3 -m unittest tests.test_rim

Tests for unsupported features are skipped, see here for what tests are supported.

We welcome volunteers and supporters. Please include a test case with any pull request, especially those that run calculations.

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

weightipy-0.2.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

weightipy-0.2.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file weightipy-0.2.0.tar.gz.

File metadata

  • Download URL: weightipy-0.2.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for weightipy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bf0402facdabc58c7f24db8c8324faf77417fdba2117a0121d25a8c657ddca92
MD5 189c03e8a9a66901700bf5f3f5b53136
BLAKE2b-256 c96d61885c0ab6913aefb883227e88a05ba0e82f7188d5c1ba71bb217675ac78

See more details on using hashes here.

File details

Details for the file weightipy-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: weightipy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for weightipy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eff31637cc91f220885cab5780d0f58dad2fb892c5ef7d2c696a2f3478324933
MD5 92f64c0f10152a0ef1161f1c8db51f69
BLAKE2b-256 0fd0c5877e9fa7915d123d858da780d26a1a8c23954bfa86940c98c28f8b0df5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page