Randomized block assignment using pandas
Project description
Stochatreat
Introduction
This is a Python module to employ block randomization using pandas. Mainly thought with RCTs in mind, it also works for any other scenario in where you would like to randomly allocate treatment within blocks or strata.
Installation
For now the easiest way (for me) to use this is to copy stochatreat.py
into wherever you'd like to use it, and then import it using:
from stochatreat import stochatreat
Usage
Single cluster:
import numpy as np
import pandas as pd
from stochatreat import stochatreat
# 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
block_cols='nhood', # the blocking variable
treats=2, # including control
idx_col='id', # the unique id column
random_state=42)
# 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.0 1.0
nhood
1 105 105
2 95 95
3 95 95
4 103 103
5 102 102
Multiple clusters and treatment probabilities:
import numpy as np
import pandas as pd
from stochatreat import stochatreat
# 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,
block_cols=['nhood', 'dummy'],
treats=2,
probs=[1/3, 2/3],
idx_col='id',
random_state=42)
# 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.0 1.0
nhood dummy
1 0 38 74
1 33 65
2 0 35 69
1 29 57
3 0 30 58
1 34 68
4 0 36 72
1 33 65
5 0 34 67
1 35 68
Acknowledgments
stochatreat
is totally inspired by Alvaro Carril's fantastic Stata package:randtreat
, which was published in The Stata Journal :trumpet:.- 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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for stochatreat-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c42851949dd4d3680a4ee46f2a763627220c4604d3faa2858fc2ee40028577ea |
|
MD5 | 71381c470a83352d436c5a4ac4948c65 |
|
BLAKE2b-256 | 0c502d4ac5650ab2331895874d589f1e2f3e63eb8a7da91dc8975b1fd8a23d63 |