Skip to main content

Section D.1 Data

Rather than download the whole dataset, we can get just an excerpt.
Listing D.1.1. Python Code
download("https://github.com/AllenDowney/ThinkStats/raw/v3/data/LLCP2022.hdf5")
Here are the first few rows.
Listing D.1.2. Python Code
$ brfss = pd.read_hdf("LLCP2022.hdf5", key="brfss")
brfss.describe()
                _SEX           HTM4          WTKG3        _LLCPWT
count  445132.000000  416480.000000  403054.000000  445132.000000
mean        1.529942     170.269057    8307.447039     594.856344
std         0.499103      10.717750    2144.817270    1134.837415
min         1.000000      91.000000    2268.000000       0.020464
25%         1.000000     163.000000    6804.000000     115.885991
50%         2.000000     170.000000    8074.000000     274.632388
75%         2.000000     178.000000    9525.000000     627.913694
max         2.000000     241.000000   29257.000000   54390.520926
The dataset includes self-reported heights for almost 200,000 men and almost 220,000 women. Here are numbers of males and females with the shortest recorded heights.
Listing D.1.3. Python Code
$ xtab = pd.crosstab(brfss["HTM4"], brfss["_SEX"])
xtab.columns = ["Male", "Female"]
xtab.head()
      Male  Female
HTM4              
91.0     7      17
92.0     0       1
95.0     1       0
97.0     1       3
99.0     1       0
And the tallest recorded heights.
Listing D.1.4. Python Code
$ xtab.tail()
       Male  Female
HTM4               
226.0     9       2
229.0     4       1
234.0     4       0
236.0     1       0
241.0     4       1
According to the codebook, heights below 91 cm are set to 91 cm, and heights above 241 cm are set to 241 cm. Even so, some of the remaining values are likely to be errors. For example, the current tallest woman in the world is 234 cm, so the largest female height in the dataset, 241 cm, was probably reported or recorded incorrectly.
To draw a bootstrap sample from this DataFrame, we’ll use the following function.
Listing D.1.5. Python Code
def resample(df):
    """Draw a bootstrap sample.

    df: DataFrame

    returns: DataFrame
    """
    n = len(df)
    return df.sample(n, replace=True, weights="_LLCPWT")
Here’s a single sample we’ll use for testing.
Listing D.1.6. Python Code
sample = resample(brfss)