Skip to main content

Exercises 9.7 Exercises

1. Exercise 9.1.

Let’s try hypothesis testing with the penguin data from Chapter 8. Instructions for downloading the data are in the notebook for this chapter.
The following cell downloads the data from a repository created by Allison Horst.
Horst AM, Hill AP, Gorman KB (2020). palmerpenguins: Palmer Archipelago (Antarctica) penguin data. R package version 0.1.0. https://allisonhorst.github.io/palmerpenguins/. doi: 10.5281/zenodo.3960218.
The data was collected as part of the research that led to this paper: Gorman KB, Williams TD, Fraser WR (2014). Ecological sexual dimorphism and environmental variability within a community of Antarctic penguins (genus Pygoscelis). PLoS ONE 9(3):e90081. https://doi.org/10.1371/journal.pone.0090081
Listing 9.7.1. Python Code
$ download(
    "https://raw.githubusercontent.com/allisonhorst/palmerpenguins/c19a904462482430170bfe2c718775ddb7dbb885/inst/extdata/penguins_raw.csv"
)
Downloaded penguins_raw.csv
Here’s how we read the data and select the Chinstrap penguins.
Listing 9.7.2. Python Code
$ penguins = pd.read_csv("penguins_raw.csv").dropna(subset=["Body Mass (g)"])
chinstrap = penguins.query('Species.str.startswith("Chinstrap")')
chinstrap.shape
(68, 17)
And here’s how we can extract the weights for male and female penguins in kilograms.
Listing 9.7.3. Python Code
$ male = chinstrap.query("Sex == 'MALE'")
weights_male = male["Body Mass (g)"] / 1000
weights_male.mean()
np.float64(3.9389705882352937)
Listing 9.7.4. Python Code
$ female = chinstrap.query("Sex == 'FEMALE'")
weights_female = female["Body Mass (g)"] / 1000
weights_female.mean()
np.float64(3.5272058823529413)
Use abs_diff_means and simulate_groups to generate a large number of simulated datasets under the null hypothesis that the two groups have the same distribution of weights, and compute the difference in means for each one. Compare the simulation results to the observed difference and compute a p-value. Is it plausible that the apparent difference between the groups is due to chance?

2. Exercise 9.2.

Using the penguin data from the previous exercise, we can extract the culmen depths and lengths for the female penguins (the culmen is the top ridge of the bill).
Listing 9.7.5. Python Code
data = female["Culmen Depth (mm)"], female["Culmen Length (mm)"]
The correlation between these variables is about 0.26.
Listing 9.7.6. Python Code
$ observed_corr = abs_correlation(data)
observed_corr
np.float64(0.2563170802728449)
Let’s see whether this correlation could happen by chance, even if there is actually no correlation between the measurements. Use permute to generate many permutations of this data and abs_correlation to compute the correlation for each one. Plot the distribution of the correlations under the null hypothesis and compute a p-value for the observed correlation. How do you interpret the result?