Skip to main content

Exercises 2.8 Exercises

For the exercises in this chapter, weโ€™ll load the NSFG female respondent file, which contains one row for each female respondent. Instructions for downloading the data and the codebook are in the notebook for this chapter.
Listing 2.8.1. Python Code
download("https://github.com/AllenDowney/ThinkStats/raw/v3/data/2002FemResp.dct")
download("https://github.com/AllenDowney/ThinkStats/raw/v3/data/2002FemResp.dat.gz")
The nsfg.py module provides a function that reads the female respondent file, cleans some of the variables, and returns a DataFrame.
Listing 2.8.2. Python Code
$ from nsfg import read_fem_resp

resp = read_fem_resp()
resp.shape
(7643, 3092)
This DataFrame contains 3092 columns, but weโ€™ll use just a few of them.

1. Exercise 2.1.

Weโ€™ll start with totincr, which records the total income for the respondentโ€™s family, encoded with a value from 1 to 14. You can read the codebook for the respondent file to see what income level each value represents.
Make a FreqTab object to represent the distribution of this variable and plot it as a bar chart.

2. Exercise 2.2.

Make a frequency table of the parity column, which records the number of children each respondent has borne. How would you describe the shape of this distribution?
Use the largest function to find the largest values of parity. Are there any values you think are errors?

3. Exercise 2.3.

Letโ€™s investigate whether women with higher or lower income bear more children. Use the query method to select the respondents with the highest income (level 14). Plot the frequency table of parity for just the high income respondents.
Compare the mean parity for high income respondents and others.
Compute Cohenโ€™s effect size for this difference. How does it compare with the difference in pregnancy length for first babies and others?
Do these results show that people with higher income have fewer children, or can you think of another explanation for the apparent difference?