In this analysis, we investigate the question: Can you tell
how tall a female elephant is from her age? Using the
ElephantsMF dataset from the Stat2Data package
(filtered for female elephants where Sex == "F"), we
examine the relationship between Age (in years, our
explanatory variable) and Height (in cm, our response
variable), evaluate how far off our linear predictions are, and assess
how much we should trust a straight-line model.
First, we load the required packages, filter the dataset for female elephants, visualize the relationship with a scatterplot, compute the correlation coefficient \(r\), and fit our linear regression model.
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 = "Female Elephant Height vs. Age",
x = "Age (years)",
y = "Height (cm)"
) +
theme_minimal()
cor(my_data$Age, my_data$Height)
## [1] 0.8385397
model <- lm(Height ~ Age, data = my_data)
coef(model)
## (Intercept) Age
## 135.418943 4.254779
From the output above: * Correlation (\(r\)): 0.8385397,
indicating a strong positive linear relationship between age and height.
* Intercept: 135.418943 cm *
Slope: 4.254779 cm per year
Next, we overlay the fitted regression line onto our scatterplot and examine the first female elephant in our dataset to compute her residual by hand.
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 = "Female Elephant Height vs. Age with Line of Best Fit",
x = "Age (years)",
y = "Height (cm)"
) +
theme_minimal()
my_data %>%
select(Age, Height) %>%
head(1)
## Age Height
## 1 11.08 217.87
217.87 - (4.254779 * 11.08 + 135.418943)
## [1] 35.30811
For the first elephant in the dataset (Age = 11.08
years, actual Height = 217.87 cm), the hand calculation
gives a residual of -35.30810568 cm (about
-35.31 cm). Because this residual is negative, the data
point sits below the regression line by about 35.31 cm,
meaning this elephant is shorter than what our linear model predicted
for her age.
Now we calculate the predicted heights and residuals for all female elephants in the dataset and construct a residual plot to check whether a straight line is appropriate across all ages.
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: Female Elephant Height vs. Age",
x = "Age (years)",
y = "Residual (cm)"
) +
theme_minimal()
Rather than looking like a random, “boring” cloud around zero, the residual plot shows a curved pattern: * Between ages 5 and 20, points tend to fall above the zero line, meaning the linear model predicts too low (actual heights are taller than predicted). * Between ages 20 and 30, points fall below the zero line, meaning the linear model predicts too high (actual heights are shorter than predicted).
This curvature tells us that elephant growth slows down as they age, so a straight line does not capture the true relationship across all life stages.
Finally, we inspect the model summary to quantify how much variation our line explains and how large our typical prediction errors are.
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
0.7031): About
70.31% of the variation in female elephant height is
explained by the linear relationship with age.24.91): When
using this straight line to predict a female elephant’s height from her
age, our predictions are typically off by about 24.91
cm.Age provides a useful rough estimate of a female elephant’s height, explaining about 70.31% of the variation in height. However, because our predictions are typically off by about 24.91 cm and the curved residual plot reveals systematic over- and under-prediction across different age ranges, we should be cautious about trusting a simple straight-line model for precise height predictions.