pip install samplics
Getting Started
Samplics
is a python package for selecting, weighting and analyzing sample obtained from complex sampling design.
Installation
if both Python 2.x and python 3.x are installed on your computer, you may have to use:
pip3 install samplics
Dependencies
Python versions 3.10.x or newer and the following packages:
Usage
To select a sample of primary sampling units using PPS method, we can use a code similar to:
import samplics
from samplics.sampling import SampleSelection
= pd.read_csv("psu_frame.csv")
psu_frame = {"East":3, "West": 2, "North": 2, "South": 3}
psu_sample_size
= SampleSelection(
pps_design ="pps-sys", stratification=True, with_replacement=False
method
)
"psu_prob"] = pps_design.inclusion_probs(
frame["cluster"],
psu_frame[
psu_sample_size,"region"],
psu_frame["number_households_census"]
psu_frame[ )
To adjust the design sample weight for nonresponse, we can use a code similar to:
import samplics
from samplics.weighting import SampleWeight
= {
status_mapping "in": "ineligible",
"rr": "respondent",
"nr": "non-respondent",
"uk":"unknown"
}
"nr_weight"] = SampleWeight().adjust(
full_sample[=full_sample["design_weight"],
samp_weight=full_sample["region"],
adjust_class=full_sample["response_status"],
resp_status=status_mapping
resp_dict )
import samplics
from samplics.estimation import TaylorEstimator, ReplicateEstimator
= TaylorEstimator("mean").estimate(
zinc_mean_str =nhanes2f["zinc"],
y=nhanes2f["finalwgt"],
samp_weight=nhanes2f["stratid"],
stratum=nhanes2f["psuid"],
psu=True,
remove_nan
)
= ReplicateEstimator("brr", "ratio").estimate(
ratio_wgt_hgt =nhanes2brr["weight"],
y=nhanes2brr["finalwgt"],
samp_weight=nhanes2brr["height"],
x=nhanes2brr.loc[:, "brr_1":"brr_32"],
rep_weights=True,
remove_nan )
Contributing
Support the project
License
Open source MIT