Stratified random assignment using pandas
Project description
Stochatreat
| Build | |
| PyPI | |
| conda-forge | |
| Meta |
Introduction
This is a Python tool to employ stratified randomization or sampling with uneven numbers in some strata using pandas. Mainly thought with randomized controlled trials (RCTs) in mind, it also works for any other scenario in where you would like to randomly allocate treatment within blocks or strata. The tool also supports having multiple treatments with different probability of assignment within each block or stratum.
Installation
PyPI
You can install this package via pip:
pip install stochatreat
Conda
You can also install this package with conda:
conda install -c conda-forge stochatreat
Usage
Single cluster:
from stochatreat import stochatreat
import numpy as np
import pandas as pd
# make 1000 households in 5 different neighborhoods.
np.random.seed(42)
df = pd.DataFrame(
data={"id": list(range(1000)), "nhood": np.random.randint(1, 6, size=1000)}
)
# randomly assign treatments by neighborhoods.
treats = stochatreat(
data=df, # your dataframe
stratum_cols="nhood", # the blocking variable
treats=2, # including control
idx_col="id", # the unique id column
random_state=42, # random seed
misfit_strategy="stratum",
) # the misfit strategy to use
# merge back with original data
df = df.merge(treats, how="left", on="id")
# check for allocations
df.groupby("nhood")["treat"].value_counts().unstack()
# previous code should return this
treat 0 1
nhood
1 105 105
2 95 95
3 95 95
4 103 103
5 102 102
Multiple clusters and treatment probabilities:
from stochatreat import stochatreat
import numpy as np
import pandas as pd
# make 1000 households in 5 different neighborhoods, with a dummy indicator
np.random.seed(42)
df = pd.DataFrame(
data={
"id": list(range(1000)),
"nhood": np.random.randint(1, 6, size=1000),
"dummy": np.random.randint(0, 2, size=1000),
}
)
# randomly assign treatments by neighborhoods and dummy status.
treats = stochatreat(
data=df,
stratum_cols=["nhood", "dummy"],
treats=2,
probs=[1 / 3, 2 / 3],
idx_col="id",
random_state=42,
misfit_strategy="global",
)
# merge back with original data
df = df.merge(treats, how="left", on="id")
# check for allocations
df.groupby(["nhood", "dummy"])["treat"].value_counts().unstack()
# previous code should return this
treat 0 1
nhood dummy
1 0 37 75
1 33 65
2 0 35 69
1 29 57
3 0 30 58
1 34 68
4 0 36 72
1 32 66
5 0 33 68
1 35 68
Contributing
If you'd like to contribute to the package, make sure you read the contributing guide.
References
stochatreatis totally inspired by Alvaro Carril's fantastic STATA package:randtreat, which was published in The Stata Journal.- David McKenzie's fantastic post (and blog) about running RCTs for the World Bank.
- In Pursuit of Balance: Randomization in Practice in Development Field Experiments. Bruhn, McKenzie, 2009
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stochatreat-0.1.4.tar.gz.
File metadata
- Download URL: stochatreat-0.1.4.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d78482eba423cb7f131af48a08a90a87326639b7ff36ba08b519161b1d2cb6a
|
|
| MD5 |
b3a089027079714618435f91d32aa4b2
|
|
| BLAKE2b-256 |
81a2ce7aae562f0b3215fbebe199085df04f531861008ec5acf0f7ae5d9b00f0
|
Provenance
The following attestation bundles were made for stochatreat-0.1.4.tar.gz:
Publisher:
release-and-publish.yml on manmartgarc/stochatreat
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stochatreat-0.1.4.tar.gz -
Subject digest:
4d78482eba423cb7f131af48a08a90a87326639b7ff36ba08b519161b1d2cb6a - Sigstore transparency entry: 731149119
- Sigstore integration time:
-
Permalink:
manmartgarc/stochatreat@8b6b61ab48270f123b5f41fb13baf2f7f592cce6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/manmartgarc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-and-publish.yml@8b6b61ab48270f123b5f41fb13baf2f7f592cce6 -
Trigger Event:
pull_request
-
Statement type:
File details
Details for the file stochatreat-0.1.4-py3-none-any.whl.
File metadata
- Download URL: stochatreat-0.1.4-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8c257b236731a05e3991e6ff269fafe422a74ba8b04fa28dec5b8fd752ea178
|
|
| MD5 |
a5d918210119ae910fae92fa024a88cf
|
|
| BLAKE2b-256 |
49b5f42c1442033057b54d9718c10ad4ffaa870372c1fe808470bf9b75bfc561
|
Provenance
The following attestation bundles were made for stochatreat-0.1.4-py3-none-any.whl:
Publisher:
release-and-publish.yml on manmartgarc/stochatreat
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stochatreat-0.1.4-py3-none-any.whl -
Subject digest:
c8c257b236731a05e3991e6ff269fafe422a74ba8b04fa28dec5b8fd752ea178 - Sigstore transparency entry: 731149120
- Sigstore integration time:
-
Permalink:
manmartgarc/stochatreat@8b6b61ab48270f123b5f41fb13baf2f7f592cce6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/manmartgarc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-and-publish.yml@8b6b61ab48270f123b5f41fb13baf2f7f592cce6 -
Trigger Event:
pull_request
-
Statement type: