In this analysis, we explore whether we can predict a sparrow’s
response variable (WingLength in mm) from its explanatory
variable (Weight in grams), and how much trust we should
put into that prediction. We focus specifically on the
control group from the Sparrows dataset in
the Stat2Data package.
First, we load the required packages and examine scatterplots of the full dataset—both plain and differentiated by treatment group.
# Load libraries
library(Stat2Data)
library(dplyr)
library(ggplot2)
# Load dataset
data("Sparrows")
# Plain scatterplot of all data
ggplot(data = Sparrows, aes(x = Weight, y = WingLength)) +
geom_point() +
labs(
title = "Sparrow Wing Length vs. Weight",
x = "Weight (grams)",
y = "Wing Length (mm)"
)
# Scatterplot with groups colored and shaped by Treatment
ggplot(data = Sparrows, aes(x = Weight, y = WingLength, color = Treatment, shape = Treatment)) +
geom_point() +
labs(
title = "Sparrow Wing Length vs. Weight by Treatment Group",
x = "Weight (grams)",
y = "Wing Length (mm)"
)
The enlarged group is scattered among the graph, with more in the middle of the graph between 10 and 15 grams. The reduce treatment is more towards 15 grams and more with a few in between 10 and 15. The control treatment points cluster together around the lower-to-middle weight range.
Next, we filter the dataset to isolate the control group, plot the relationship, and calculate the correlation coefficient \(r\).
# Filter to keep only the control group
my_group <- Sparrows %>%
filter(Treatment == "control")
# Scatterplot for the control group
ggplot(data = my_group, aes(x = Weight, y = WingLength)) +
geom_point(alpha = 0.6) +
labs(
title = "Control Group: Wing Length vs. Weight",
x = "Weight (grams)",
y = "Wing Length (mm)"
)
# Calculate correlation coefficient r
cor(my_group$Weight, my_group$WingLength, use = "complete.obs")
## [1] 0.8059326
With \(r = 0.8059326\), there is a strong positive linear association between sparrow weight and wing length. As sparrow weight increases, wing length tends to increase as well, following a roughly straight-line pattern.
We fit a simple linear regression model to predict
WingLength from Weight and extract the
regression coefficients.
# Fit the linear regression model
model <- lm(WingLength ~ Weight, data = my_group)
# Print coefficients
coef(model)
## (Intercept) Weight
## 8.669793 1.309223
The estimated intercept is 8.669793 mm and the estimated slope is 1.309223 mm per gram. In context, the slope indicates that for every 1-gram increase in a sparrow’s weight, its wing length is predicted to increase by approximately 1.31 mm.
Finally, we overlay the fitted regression line onto the control
group’s scatterplot using geom_abline().
# Scatterplot with fitted regression line
ggplot(data = my_group, aes(x = Weight, y = WingLength)) +
geom_point(alpha = 0.6) +
geom_abline(intercept = 8.669793, slope = 1.309223, color = "steelblue", linewidth = 1) +
labs(
title = "Control Group: Wing Length vs. Weight with Fitted Line",
subtitle = "WingLength = 8.669793 + 1.309223 * Weight (r = 0.8059)",
x = "Weight (grams)",
y = "Wing Length (mm)"
)
The regression line runs through the center of the data points, confirming a strong, reliable linear fit. Because \(r \approx 0.81\), we can place a high degree of trust in using weight to predict wing length within the range of observed weights for this control group.