Our big question is: Can we predict WingLength from
Weight, and how much should we trust that prediction?
We will use the Sparrows dataset from the
Stat2Data package. We will focus on sparrows in the
"control" treatment group.
library(Stat2Data)
library(dplyr)
library(ggplot2)
data("Sparrows")
We begin by looking at the relationship between Weight
and WingLength for all sparrows.
ggplot(Sparrows, aes(x = Weight, y = WingLength)) +
geom_point() +
labs(
title = "Sparrow Weight and Wing Length",
x = "Weight (grams)",
y = "WingLength (mm)"
)
We then use color and shape to distinguish the treatment groups.
ggplot(Sparrows, aes(x = Weight, y = WingLength,
color = Treatment, shape = Treatment)) +
geom_point() +
labs(
title = "Sparrow Weight and Wing Length by Treatment",
x = "Weight (grams)",
y = "WingLength (mm)",
color = "Treatment",
shape = "Treatment"
)
Interpretation:
The control group appears higher than the other treatment groups on the scatterplot.
We use filter() to keep only the control group.
my_group <- Sparrows %>%
filter(Treatment == "control")
We create a scatterplot of the control group.
ggplot(my_group, aes(x = Weight, y = WingLength)) +
geom_point(alpha = 0.6) +
labs(
title = "Sparrow Weight and Wing Length (Control Group)",
x = "Weight (grams)",
y = "WingLength (mm)"
)
We calculate the correlation coefficient.
cor(my_group$Weight, my_group$WingLength,
use = "complete.obs")
## [1] 0.8059326
The argument use = "complete.obs" tells R to skip any
rows with missing values.
Interpretation:
The association between Weight and
WingLength is positive, roughly linear, and strong.
The correlation coefficient is 0.806. This supports the description of a strong positive association.
The response variable is WingLength, and the explanatory
variable is Weight.
We use the syntax:
lm(response ~ explanatory, data = my_group)
model <- lm(WingLength ~ Weight, data = my_group)
coef(model)
## (Intercept) Weight
## 8.669793 1.309223
The number under (Intercept) is the intercept, and the
number under the explanatory variable’s name is the slope.
Interpretation:
The slope is 1.309223.
For every 1-gram increase in Weight, the predicted
WingLength increases by 1.309223 mm, on average.
The positive slope matches the positive correlation.
We add the regression line to the scatterplot using the slope and
intercept obtained from coef(model).
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 and Wing Length (Control Group)",
subtitle = "WingLength = 8.669793 + 1.309223(Weight)",
x = "Weight (grams)",
y = "WingLength (mm)"
)
Interpretation:
The regression line passes through the middle of the points, which supports the visual fit of the line.
We will use the regression equation to estimate
WingLength for a sparrow with a Weight of 13
grams.
The prediction value is 13 grams.
# STUDENT: Use the slope, intercept, and x value
# to calculate the predicted WingLength.
8.669793 + 1.309223 * 13
## [1] 25.68969
STUDENT: Write your interpretation of this predicted WingLength here. Include the units and explain what the prediction means in context.
We will compare the prediction at 13 grams with predictions at nearby and far-away values.
# Prediction at the nearby lower value
8.669793 + 1.309223 * 12.5
## [1] 25.03508
# Prediction at the nearby upper value
8.669793 + 1.309223 * 13.5
## [1] 26.3443
# Prediction at the far value
8.669793 + 1.309223 * 30
## [1] 47.94648
The nearby range is 12.5 to 13.5 grams.
The far value is 30 grams.
STUDENT: Write your interpretation here. Discuss whether the nearby predictions are reasonable and why the prediction at the far value may be less trustworthy. Yes, there is a strong correlation coefficient which points toward a strong linear correlation. The farther may be less trustworthy since them seem like outliers.
STUDENT: Write your final conclusion here. Answer the big
question: Can you predict WingLength from
Weight, and how much should you trust that
prediction? Yes. The correlation coefficient is strong which is
trustworthy. We can predict that every time weight increases by 1, wing
length increases by 1.309.