1. Load Packages and Data

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

data("Sparrows")

2. Explore the Full Dataset

First, examine the relationship between Weight and WingLength for all sparrows.

ggplot(Sparrows, aes(x = Weight, y = WingLength)) +
  geom_point() +
  labs(
    title = "Sparrow Weight vs. Wing Length",
    x = "Weight (grams)",
    y = "Wing Length (mm)"
  )

Next, identify the different Treatment groups using color and shape.

ggplot(Sparrows, aes(x = Weight, y = WingLength,
                     color = Treatment, shape = Treatment)) +
  geom_point() +
  labs(
    title = "Sparrow Weight vs. Wing Length by Treatment",
    x = "Weight (grams)",
    y = "Wing Length (mm)"
  )

The different Treatment groups overlap in the scatterplot.

3. Focus on the Control Group

We will now focus on observations where Treatment is “control”.

my_group <- Sparrows %>%
  filter(Treatment == "control")

Create a scatterplot of the control group.

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

Calculate the correlation coefficient.

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

The association is positive, linear, and moderately strong. The correlation coefficient is 0.806. This matches the positive direction observed in the scatterplot.

4. Fit the Regression Model

The response variable is WingLength, and the explanatory variable is Weight.

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

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

The intercept is 8.669793, and the slope is 1.309223.

The slope means that for every 1-gram increase in Weight, the predicted WingLength increases by approximately 1.3 mm.

5. Graph the 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 = "Sparrow Weight vs. Wing Length",
    subtitle = "WingLength = 8.669793 + 1.309223(Weight), r = 0.806",
    x = "Weight (grams)",
    y = "Wing Length (mm)"
  )

The regression line passes through the middle of the points.

6. Make Predictions

The regression equation is:

WingLength = 8.669793 + 1.309223(Weight)

We will examine predictions at Weight = 13, nearby values, and a far value.

# Prediction at Weight = 13
8.669793 + 1.309223 * 13
## [1] 25.68969
# Predictions for nearby values
8.669793 + 1.309223 * 12.5
## [1] 25.03508
8.669793 + 1.309223 * 13.5
## [1] 26.3443
# Prediction for a far value
8.669793 + 1.309223 * 30
## [1] 47.94648

STUDENT: Record and interpret the predicted WingLength at Weight = 13. 8.669793 + 1.309223 * 13 = 25.68969 STUDENT: Explain how the nearby predictions compare to the prediction at Weight = 13. The predictions at Weight = 12.5 and Weight = 13.5 should be close to the prediction at Weight = 13 because these values are within a similar range. Since they are nearby values, the predictions are more reliable. STUDENT: Explain why predicting at Weight = 30 may be less trustworthy than predicting at Weight = 13. Predicting at Weight = 30 may be less trustworthy because it is much farther outside the range of the observed data. ## 7. Conclusion

The goal of this analysis was to determine whether Weight can be used to predict WingLength for sparrows in the control group.

The correlation coefficient of 0.806 indicates a positive, moderately strong linear association.

The regression equation shows that predicted WingLength increases as Weight increases.

STUDENT: Write your final conclusion. Explain whether Weight can be used to predict WingLength and how much you should trust predictions at nearby and far values. Weight can be used to predict WingLength in sparrows in the control group because there is a positive, moderately strong linear association between the two variables, with a correlation coefficient of 0.806. As Weight increases, predicted WingLength also increases. Predictions for nearby values are more trustworthy because they are close to the prediction value of 13 grams. Predictions at far values are less trustworthy because they consist of predicting beyond the range of the observed data. In conclusion, Weight is useful for predicting WingLength, but predictions are most reliable when they are within the range of the original data.