Skip to main content

Exercises 10.8 Exercises

1. Exercise 10.1.

In this chapter we computed a least squares fit for penguin weights as a function of flipper length. There are two other measurements in the dataset we can also consider: culmen length and culmen depth (the culmen is the top ridge of the bill).
Compute the least squares fit for weight as a function of culmen length. Make a scatter plot of these variables and plot the fitted line.
Based on the rvalue attribute of the RegressionResult object, what is the correlation of these variables? What is the coefficient of determination? Which is a better predictor of weight, culmen length or flipper length?
Listing 10.8.1. Python Code
xvar = "Culmen Length (mm)"
yvar = "Body Mass (g)"

culmen_length = adelie[xvar]
body_mass = adelie[yvar]

2. Exercise 10.2.

In this chapter we used resampling to approximate the sampling distribution for the slope of a fitted line. We can approximate the sampling distribution of the intercept the same way:
  1. Write a function called estimate_intercept that takes a resampled DataFrame as an argument, computes the least squares fit of penguin weight as a function of flipper length, and returns the intercept.
  2. Call the function with many resampled versions of adelie and collect the intercepts.
  3. Use plot_kde to plot the sampling distribution of the intercept.
  4. Compute the standard error and a 90% confidence interval.
  5. Check that the standard error you get from resampling is consistent with the intercept_stderr attribute in the RegressionResult object -- it might be a little smaller.

3. Exercise 10.3.

A person’s Body Mass Index (BMI) is their weight in kilograms divided by their height in meters raised to the second power. In the BRFSS dataset, we can compute BMI like this, after converting heights from centimeters to meters.
Listing 10.8.2. Python Code
heights_m = heights / 100
bmis = weights / heights_m**2
In this definition, heights are squared, rather than raised to some other exponent, because of the observation -- early in the history of statistics -- that average weight increases roughly in proportion to height squared.
To see whether that’s true, we can use data from the BRFSS, a least squares fit, and a little bit of math. Suppose weight is proportional to height raised to an unknown exponent, \(a\text{.}\) In that case, we can write:
\begin{equation*} w = b h^a \end{equation*}
where \(w\) is weight, \(h\) is height, and \(b\) is an unknown constant of proportionality. Taking logarithms of both sides:
\begin{equation*} \log w = \log b + a \log h \end{equation*}
So, if we compute a least squares fit for log-transformed weights as a function of log-transformed heights, the slope of the fitted line estimates the unknown exponent \(a\text{.}\)
Compute the logarithms of height and weight. You can use any base for the logarithms, as long as it’s the same for both transformations. Compute a least squares fit. Is the slope close to 2?