Step 1: Initial Exploration of the Data

We load the required packages and the Sparrows dataset, then examine scatterplots of all data to look for potential grouping patterns.

library(Stat2Data)
library(dplyr)
library(ggplot2)

data("Sparrows")

# Plain scatterplot of all data
ggplot(Sparrows, aes(x = Weight, y = WingLength)) +
  geom_point() +
  labs(
    title = "Sparrow Wing Length vs. Weight (All Data)",
    x = "Weight (grams)",
    y = "Wing Length (mm)"
  )

# Scatterplot grouped by Treatment
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 scatterplot grouped by Treatment, the different treatment groups are mostly all bunched up around the same area rather than forming separate, distinct clusters.

Step 2: Filtering and Correlation Analysis

We filter the dataset to isolate the control group (my_group) and evaluate the linear association between weight and wing length for this specific treatment group.

# Filter dataset to keep only the control group
my_group <- Sparrows %>%
  filter(Treatment == "control")

# Scatterplot for the control group
ggplot(my_group, aes(x = Weight, y = WingLength)) +
  geom_point(alpha = 0.6) +
  labs(
    title = "Sparrow Wing Length vs. Weight (Control Group)",
    x = "Weight (grams)",
    y = "Wing Length (mm)"
  )

# Calculate correlation coefficient (r)
cor(my_group$Weight, my_group$WingLength, use = "complete.obs")
## [1] 0.8059326

The correlation coefficient between Weight and WingLength for the control group is \(r = 0.8059326\). The scatterplot displays a positive, straight linear trend with a strong association, which aligns well with an \(r\) value near \(0.81\).

Step 3: Fitting the Linear Model

We fit a simple linear regression model with WingLength as the response variable (\(y\)) and Weight as the explanatory variable (\(x\)).

# Fit linear model
model <- lm(WingLength ~ Weight, data = my_group)

# View model coefficients
coef(model)
## (Intercept)      Weight 
##    8.669793    1.309223

From the model output: - Intercept: \(8.669793\) - Slope: \(1.309223\)

Slope Interpretation: For every 1-gram increase in Weight, the predicted WingLength increases by approximately 1.309223 mm, on average.

Step 4: Visualizing the Regression Line

We superimpose our fitted regression line onto the scatterplot using our calculated intercept (\(8.669793\)) and slope (\(1.309223\)).

# Scatterplot with 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", size = 1) +
  labs(
    title = "Sparrow Wing Length vs. Weight (Control Group)",
    subtitle = "WingLength = 8.669793 + 1.309223(Weight) | r = 0.8059326",
    x = "Weight (grams)",
    y = "Wing Length (mm)"
  )

Visual verification confirms that the fitted regression line passes right through the middle of the data points as expected.