Our research question is: Can you weigh a perch with a ruler?
We will investigate whether perch length can be used to predict perch weight using a linear regression model. We will also examine residuals, the residual plot, R-squared, and the residual standard error to evaluate how accurate and trustworthy our predictions are.
First, we load the necessary packages and dataset.
library(Stat2Data)
library(dplyr)
library(ggplot2)
data(Perch)
my_data <- Perch
Next, we create a scatterplot to examine the relationship between perch length and weight.
ggplot(my_data, aes(x = Length, y = Weight)) +
geom_point() +
labs(
title = "Perch Length vs. Weight",
x = "Length (cm)",
y = "Weight (grams)"
)
We calculate the correlation using cor().
cor(my_data$Length, my_data$Weight)
## [1] 0.9595061
The correlation is 0.9595061, indicating a strong positive linear relationship between perch length and weight.
Next, we fit a linear regression model.
model <- lm(Weight ~ Length, data = my_data)
coef(model)
## (Intercept) Length
## -652.78714 35.00089
The regression equation is:
\[ \widehat{Weight} = -652.78714 + 35.00089(Length) \]
The intercept is -652.78714, and the slope is 35.00089. This means that for every additional centimeter of perch length, the predicted weight increases by approximately 35.00089 grams.
We add the regression line to our scatterplot.
ggplot(my_data, aes(x = Length, y = Weight)) +
geom_point() +
geom_abline(
intercept = -652.78714,
slope = 35.00089,
color = "steelblue"
) +
labs(
title = "Perch Length vs. Weight with Regression Line",
x = "Length (cm)",
y = "Weight (grams)"
)
A residual is calculated by subtracting the predicted value from the actual value.
\[ Residual = Actual - Predicted \]
We examine the first observation.
my_data %>%
select(Obs, Length, Weight) %>%
head(1)
## Obs Length Weight
## 1 104 8.8 5.9
The first observation has a length of 8.8 cm and an actual weight of 5.9 grams.
We calculate its residual by hand.
5.9 - (35.00089 * 8.8 + (-652.78714))
## [1] 350.6793
The residual is 350.6793 grams. Because the residual is positive, this observation is above the regression line. Its actual weight is approximately 350.6793 grams greater than the predicted weight.
Next, we add predicted values and residuals to the dataset.
my_data <- my_data %>%
mutate(
predicted = fitted(model),
residual = residuals(model)
)
my_data %>%
select(Length, Weight, predicted, residual) %>%
head()
## Length Weight predicted residual
## 1 8.8 5.9 -344.779274 350.67927
## 2 14.7 32.0 -138.274002 170.27400
## 3 16.0 40.0 -92.772841 132.77284
## 4 17.2 51.5 -50.771769 102.27177
## 5 18.5 70.0 -5.270607 75.27061
## 6 19.2 100.0 19.230019 80.76998
The first residual should match the hand calculation, apart from a small rounding difference.
We create a residual plot to determine whether a straight-line model is appropriate.
ggplot(my_data, aes(x = Length, y = residual)) +
geom_point(alpha = 0.6) +
geom_hline(yintercept = 0, color = "steelblue") +
labs(
title = "Residual Plot for Perch",
x = "Length (cm)",
y = "Residual (grams)"
)
The residual plot appears to have a boring cloud of points with no clear pattern. This suggests that a straight-line model is reasonable for these data.
We use summary() to examine how well our regression
model fits the data.
summary(model)
##
## Call:
## lm(formula = Weight ~ Length, data = my_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -146.25 -57.86 -23.99 45.00 350.68
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -652.787 43.407 -15.04 <2e-16 ***
## Length 35.001 1.398 25.03 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 98.82 on 54 degrees of freedom
## Multiple R-squared: 0.9207, Adjusted R-squared: 0.9192
## F-statistic: 626.5 on 1 and 54 DF, p-value: < 2.2e-16
The Multiple R-squared is 0.9207. This means that approximately 92.07% of the variation in perch weight is explained by perch length.
The residual standard error is 98.82 grams. This means that our predictions of perch weight are typically off by approximately 98.82 grams.
Our big question was: Can you weigh a perch with a ruler?
Yes, perch length can be used to predict perch weight. The high positive correlation of 0.9595061 and R-squared of 92.07% indicate a strong linear relationship between the two variables. The residual plot also showed a boring cloud of points, suggesting that a straight-line model is appropriate.
However, the residual standard error of 98.82 grams shows that predictions are not perfect and can be off by a noticeable amount. Overall, perch length is a useful predictor of perch weight, although predictions should not be expected to be exact.