In this analysis, we explore the relationship between sparrow weight
(in grams) and wing length (in millimeters) using the
Sparrows dataset from the Stat2Data
package.
Our main goal is to determine whether we can predict wing length from bird weight, specifically within the control treatment group.
# Load required libraries
library(Stat2Data)
library(dplyr)
library(ggplot2)
# Load the Sparrows dataset
data(Sparrows)
We begin by plotting all sparrows in the dataset to observe overall patterns and compare treatment groups.
# Plot 1: Plain scatterplot of ALL data
ggplot(Sparrows, aes(x = Weight, y = WingLength)) +
geom_point() +
labs(
title = "Sparrow Wing Length vs. Weight",
x = "Weight (grams)",
y = "Wing Length (mm)"
)
# Plot 2: Scatterplot grouped by Treatment (color and shape)
ggplot(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)"
)
When viewing the data split by treatment group, most groups display a positive association with moderate strength.
We now focus specifically on the control group to
evaluate direction, linearity, and strength.
# Filter dataset to keep only the control group
my_group <- Sparrows %>%
filter(Treatment == "control")
# Scatterplot of control group
ggplot(my_group, aes(x = Weight, y = WingLength)) +
geom_point(alpha = 0.6) +
labs(
title = "Control Sparrows: Wing Length vs. Weight",
x = "Weight (grams)",
y = "Wing Length (mm)"
)
# Calculate the correlation coefficient r
cor(my_group$Weight, my_group$WingLength, use = "complete.obs")
## [1] 0.8059326
With a correlation coefficient of approximately \(r = 0.806\), there is a strong, positive, and linear association between bird weight and wing length for control sparrows. This high positive correlation value fits the visually clear linear trend seen in the scatterplot.
Next, we model WingLength (response variable) as a
linear function of Weight (explanatory variable).
# Fit the linear regression model
model <- lm(WingLength ~ Weight, data = my_group)
# Display model coefficients
coef(model)
## (Intercept) Weight
## 8.669793 1.309223
8.6697931.309223Interpretation: For every 1-gram increase in a sparrow’s weight, we predict that its wing length will increase by approximately 1.309 millimeters on average.
Finally, we plot the control group scatterplot along with our fitted regression line.
# Scatterplot with fitted regression line
ggplot(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 Sparrows: Wing Length vs. Weight",
subtitle = "Line: WingLength = 8.669793 + 1.309223 * Weight (r = 0.806)",
x = "Weight (grams)",
y = "Wing Length (mm)"
)
The fitted line passes cleanly through the middle of the scatter of data points, confirming that our calculated intercept and slope accurately summarize the linear trend.