Our primary question is: Can you tell how tall a female elephant is from her age? We will also investigate how far off our linear predictions tend to be and how much we can trust this model.
We begin by loading the required libraries (Stat2Data,
dplyr, and ggplot2), filtering the dataset to
include only female elephants (Sex == "F"), creating an
initial scatterplot, and computing the correlation coefficient \(r\).
library(Stat2Data)
library(dplyr)
library(ggplot2)
data("ElephantsMF")
my_data <- ElephantsMF %>%
filter(Sex == "F")
ggplot(my_data, aes(x = Age, y = Height)) +
geom_point(alpha = 0.7) +
labs(
title = "Height vs. Age for Female Elephants",
x = "Age (years)",
y = "Height (cm)"
) +
theme_minimal()
cor(my_data$Age, my_data$Height)
## [1] 0.8385397
The correlation coefficient between Age and Height is \(r = 0.7\).
Next, we fit a linear model with Height as the response
variable (\(y\)) and Age
as the explanatory variable (\(x\)),
and retrieve the model coefficients.
model <- lm(Height ~ Age, data = my_data)
coef(model)
## (Intercept) Age
## 135.418943 4.254779
From the output: - Intercept: \(135.418943\) - Slope: \(4.254779\)
We overlay this fitted regression line on our original scatterplot:
ggplot(my_data, aes(x = Age, y = Height)) +
geom_point(alpha = 0.7) +
geom_abline(intercept = 135.418943, slope = 4.254779, color = "steelblue") +
labs(
title = "Height vs. Age for Female Elephants with Fitted Line",
x = "Age (years)",
y = "Height (cm)"
) +
theme_minimal()
A residual measures the vertical distance between an observed data point and the fitted regression line (\(\text{Residual} = \text{Actual } y - \text{Predicted } y\)).
We examine the first observation in the dataset:
my_data %>%
select(Age, Height) %>%
head(1)
## Age Height
## 1 11.08 217.87
For the first elephant: - Actual Age (\(x\)) = \(11.08\text{ years}\) - Actual Height (\(y\)) = \(217.87\text{ cm}\)
Calculating the residual by hand:
\[\text{Residual} = 217.87 - (4.254779 \times 11.08 + 135.418943) = 35.30811\text{ cm}\]
Because the residual is positive (\(+35.30811\text{ cm}\)), this data point sits above the regression line.
We append the fitted values and residuals for all observations to
my_data and inspect a residual plot.
my_data <- my_data %>%
mutate(
predicted = fitted(model),
residual = residuals(model)
)
my_data %>%
select(Age, Height, predicted, residual) %>%
head()
## Age Height predicted residual
## 1 11.08 217.87 182.5619 35.3081058
## 2 2.33 132.89 145.3326 -12.4425777
## 3 2.42 144.83 145.7155 -0.8855078
## 4 26.50 206.07 248.1706 -42.1005869
## 5 11.75 207.00 185.4126 21.5874038
## 6 0.33 85.59 136.8230 -51.2330197
ggplot(my_data, aes(x = Age, y = residual)) +
geom_point(alpha = 0.6) +
geom_hline(yintercept = 0, color = "steelblue") +
labs(
title = "Residual Plot for Female Elephants",
x = "Age (years)",
y = "Residual (cm)"
) +
theme_minimal()
Interpretation of Residual Plot: The residual plot exhibits a distinct downward curve rather than a random, “boring” cloud of points. This curvature indicates that a simple linear equation is not entirely appropriate for these data. Specifically, when female elephants are young, the line predicts too low (underestimating height), whereas when elephants get older, the line predicts too high (overestimating height).
Finally, we run summary(model) to extract \(R^2\) and the Residual Standard Error.
summary(model)
##
## Call:
## lm(formula = Height ~ Age, data = my_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -60.189 -20.377 4.449 18.496 54.354
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 135.4189 3.5559 38.08 <2e-16 ***
## Age 4.2548 0.2272 18.72 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 24.91 on 148 degrees of freedom
## Multiple R-squared: 0.7031, Adjusted R-squared: 0.7011
## F-statistic: 350.6 on 1 and 148 DF, p-value: < 2.2e-16
Key Summary Takeaways: - Multiple \(R^2 = 0.7031\): Approximately \(70.31\%\) of the variation in female elephant height is explained by age. - Residual Standard Error \(= 24.91\text{ cm}\): Predictions from this linear model are typically off by about \(24.91\text{ cm}\).
Final Conclusion: While age provides a strong general indication of height (accounting for about \(70.31\%\) of the variance), the persistent curvature in the residual plot shows that female elephant growth slows over time. A non-linear model would provide a more accurate depiction of how height changes with age.