Introduction & Package Loading

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)

Step 1: Exploratory Data Visualization

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)"
  )

Initial Observations

When viewing the data split by treatment group, most groups display a positive association with moderate strength.


Step 2: Filtering and Correlation Analysis

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

Correlation & Association Interpretation

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.


Step 3: Fitting the Simple Linear Regression Model

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

Model Coefficients & Interpretation

  • Intercept (\(\hat{\beta}_0\)): 8.669793
  • Slope (\(\hat{\beta}_1\)): 1.309223

Interpretation: 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.


Step 4: Visualizing the Regression Line

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)"
  )

Model Verification

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.