Section 13.4 Weighted Bootstrap
The NSFG dataset includes a column called
finalwgt that contains each respondentโs sampling weight, which is the number of people in the population they represent. We can use these weights during the resampling process to correct for stratified sampling. The following function takes a DataFrame and the name of the column that contains the sampling weights. It resamples the rows of the DataFrame, taking the sampling weights into account, and returns a new DataFrame.
def resample_rows_weighted(df, column="finalwgt"):
n = len(df)
weights = df[column]
return df.sample(n, weights=weights, replace=True)
The current dataset includes respondents from several iterations of the survey, called cycles, so in order to resample, we have to group the respondents by cycle, resample each group, and then put the groups back together. Thatโs what the following function does.
def resample_cycles(resp):
grouped = resp.groupby("cycle")
samples = [resample_rows_weighted(group) for _, group in grouped]
return pd.concat(samples)
To get started, weโll resample the data once.
# Seed the random number generator so we get the same results every time
np.random.seed(1)
sample = resample_cycles(resp)
Later weโll resample the data several times, so we can see how much variation there is due to random sampling.
The following figure shows the results with resampling, compared to the results from the previous section without resampling, shown as dotted lines.
for label, surv in surv_map.items():
surv.plot(ls=":", color="gray", alpha=0.6)
survs_resampled = make_survival_map(sample, cohorts)
for label, surv in survs_resampled.items():
surv.plot(label=label)
decorate(xlabel="Age", ylabel="Prob never married", ylim=ylim)
plt.rc("axes", prop_cycle=plt.rcParamsDefault["axes.prop_cycle"])

plt.rc("axes", prop_cycle=plt.rcParamsDefault["axes.prop_cycle"])
The difference, with and without resampling, is substantial, which shows that we need to correct for stratified sampling to get accurate results.
Now letโs get to the second problem, dealing with incomplete data.
