Skip to main content

Exercises 14.12 Exercises

1.

In this chapter we compared the weights of male and female penguins and computed a confidence interval for the difference. Now let’s do the same for flipper length. The observed difference is about 4.6 mm.
Listing 14.12.1. Python Code
$ grouped = adelie.groupby("Sex")

lengths_male = grouped.get_group("MALE")["Flipper Length (mm)"]
lengths_female = grouped.get_group("FEMALE")["Flipper Length (mm)"]
observed_diff = lengths_male.mean() - lengths_female.mean()
observed_diff
np.float64(4.616438356164366)
Use sampling_dist_mean to make Normal objects that represent sampling distributions for the mean flipper length in the two groups—noting that the groups are not the same size. Then compute the sampling distribution of the difference and a 90% confidence interval.

2.

Using the NSFG data, we computed the correlation between a baby’s birth weight and the mother’s age, and we used a \(t\) distribution to compute a p-value. Now let’s do the same with birth weight and father’s age, which is recorded in the hpagelb column.
Listing 14.12.2. Python Code
$ valid = live.dropna(subset=["hpagelb", "totalwgt_lb"])
n = len(valid)
n
8933
The observed correlation is about 0.065.
Listing 14.12.3. Python Code
$ data = valid["hpagelb"].values, valid["totalwgt_lb"].values
r_actual = correlation(data)
r_actual
np.float64(0.06468629895432174)
Compute the transformed correlation, t_actual. Use the CDF of the \(t\) distribution to compute a p-value—is this correlation statistically significant? Use the SciPy function pearsonr to check your results.

3.

In one of the exercises in Chapter 9 we considered the Trivers-Willard hypothesis, which suggests that for many mammals the sex ratio depends on “maternal condition”—that is, factors like the mother’s age, size, health, and social status. Some studies have shown this effect among humans, but results are mixed.
As an example, and a chance to practice a chi-squared test, let’s see if there’s a relationship between the sex of a baby and the mother’s marital status. The notebook for this chapter has instructions to help you get started.
First we’ll partition mothers of male and female babies.
Listing 14.12.4. Python Code
male = live.query("babysex == 1")
female = live.query("babysex == 2")
Now we’ll make a DataFrame with one column for each group and one row for each value of fmarital, which encodes marital status like this:
1  married
2  widowed
3  divorces
4  separated
5  never married
Listing 14.12.5. Python Code
$ observed = pd.DataFrame()
observed["male"] = male["fmarital"].value_counts().sort_index()
observed["female"] = female["fmarital"].value_counts().sort_index()
observed
          male  female
fmarital              
1         2576    2559
2           56      54
3          568     572
4          355     330
5         1086     985
The null hypothesis is that the distribution of marital status is the same for both groups, so we can use the whole dataset to compute it.
Listing 14.12.6. Python Code
$ from empiricaldist import Pmf

pmf_fmarital = Pmf.from_seq(live["fmarital"])
pmf_fmarital
fmarital
1    0.561653
2    0.012024
3    0.124617
4    0.075208
5    0.226498
Name: , dtype: float64
To compute the expected values, we multiply the probabilities in pmf_marital by the total number of cases in each column.
Listing 14.12.7. Python Code
$ expected = pd.DataFrame()
expected["male"] = pmf_fmarital * observed["male"].sum()
expected["female"] = pmf_fmarital * observed["female"].sum()
expected
                 male       female
fmarital                          
1         2606.630739  2527.437691
2           55.805641    54.110188
3          578.349366   560.778312
4          349.038916   338.434631
5         1051.175339  1019.239178
Use observed and expected to compute a chi-squared statistic. Then use the CDF of the chi-squared distribution to compute a p-value. The degrees of freedom should be n-1, where n is the number of values in the observed DataFrame. Then use the SciPy function chisquared to compute the chi-squared statistic and p-value. Hint: use the argument axis=None to treat the entire DataFrame as a single test rather than one test for each column.
Does this test provide support for the Trivers-Willard hypothesis?

4.

The method we used in this chapter to analyze differences between groups can be extended to analyze “differences in differences”, which is a common experimental design. As an example, we’ll use data from a 2014 paper that investigates the effects of an intervention intended to mitigate gender-stereotypical task allocation within student engineering teams.
Stein, L. A., Aragon, D., Moreno, D., Goodman, J. (2014, October). Evidence for the persistent effects of an intervention to mitigate gender-stereotypical task allocation within student engineering teams. In 2014 IEEE Frontiers in Education Conference (FIE) Proceedings (pp. 1-9). IEEE. Available from ieeexplore.ieee.org.
Before and after the intervention, students responded to a survey that asked them to rate their contribution to each aspect of class projects on a 7-point scale.
Before the intervention, male students reported higher scores for the programming aspect of the projects than female students: men reported an average score of 3.57 with standard error 0.28; women reported an average score of 1.91 with standard error 0.32.
After the intervention, the gender gap was smaller: the average score for men was 3.44 (SE 0.16); the average score for women was 3.18 (SE 0.16).
  1. Make four Normal objects to represent the sampling distributions of the estimated means before and after the intervention, for both male and female students. Because we have standard errors for the estimated means, we don’t need to know the sample size to get the parameters of the sampling distributions.
  2. Compute the sampling distributions of the gender gap—the difference in means—before and after the intervention.
  3. Then compute the sampling distribution of the difference in differences—that is, the change in the size of the gap. Compute a 95% confidence interval and a p-value.
Is there evidence that the size of the gender gap decreased after the intervention?