library(Stat2Data)
library(ggplot2)

data(Perch)

Step 1: Fit the line

ggplot(Perch, aes(x = Length, y = Weight)) +
  geom_point() +
  labs(
    title = "Length vs. Weight for Perch",
    x = "Length (cm)",
    y = "Weight (grams)"
  )

cor(Perch$Length, Perch$Weight)
## [1] 0.9595061
model <- lm(Weight ~ Length, data = Perch)

coef(model)
## (Intercept)      Length 
##  -652.78714    35.00089

Question 1: What are r, the slope, and the intercept? Does the sign of the slope match the sign of r?

The correlation is 0.9595061, the slope is 35.00089, and the intercept is -652.78714. The slope and correlation are both positive, meaning that longer perch tend to weigh more.

Step 2: One residual by hand

ggplot(Perch, aes(x = Length, y = Weight)) +
  geom_point() +
  geom_abline(
    intercept = -652.78714,
    slope = 35.00089,
    color = "steelblue"
  ) +
  labs(
    title = "Length vs. Weight for Perch",
    x = "Length (cm)",
    y = "Weight (grams)"
  )

head(Perch[c("Obs", "Length", "Weight")], 1)
##   Obs Length Weight
## 1 104    8.8    5.9
5.9 - (35.00089 * 8.8 + (-652.78714))
## [1] 350.6793

Question 2: Is this fish above or below the line, and by how much? What weight does the line predict for this fish, and does that prediction make sense?

The fish is about 350.68 grams above the line because the residual is positive. The predicted weight is -344.78 grams, which doesn’t make sense because a fish cannot have a negative weight.

Step 3: Every residual and the residual plot

Perch$predicted <- as.numeric(stats::predict(model))
Perch$residual <- as.numeric(stats::residuals(model))

head(Perch[c("Length", "Weight", "predicted", "residual")])
##   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
ggplot(Perch, aes(x = Length, y = residual)) +
  geom_point(alpha = 0.6) +
  geom_hline(yintercept = 0, color = "steelblue") +
  labs(
    title = "Residual Plot: Length Model for Perch",
    x = "Length (cm)",
    y = "Residual (grams)"
  )

Question 3: Is the residual plot boring, curved, or fanning? For which lengths does the line predict too high, and for which does it predict too low? Is a straight line a good model for these fish?

The residual plot has a curved pattern. The model predicts too low for shorter and longer fish and too high for medium-sized fish. Since there’s a clear pattern, a straight line isn’t a very good model for predicting their weight.

Step 4: R-squared and the residual standard error

summary(model)
## 
## Call:
## lm(formula = Weight ~ Length, data = Perch)
## 
## 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
cor(Perch$Length, Perch$Weight)^2
## [1] 0.9206519

Question 4: What are R-squared and the residual standard error? Interpret each in context. Square your r from Step 1: does it equal R-squared?

The R-squared is 0.9207, meaning about 92.07% of the variation in perch weight is explained by length. The residual standard error is 98.82 grams, meaning predictions are typically off by about 98.82 grams. Squaring r gives approximately 0.9207, which matches R-squared.

Step 5: The biggest surprises

sorted_perch <- Perch[order(-abs(Perch$residual)), ]

head(
  sorted_perch[c("Obs", "Length", "Weight", "predicted", "residual")],
  3
)
##    Obs Length Weight predicted residual
## 1  104    8.8    5.9 -344.7793 350.6793
## 52 155   44.6 1100.0  908.2527 191.7473
## 40 143   37.3  840.0  652.7462 187.2538
max(abs(Perch$residual)) / summary(model)$sigma
## [1] 3.548587

Question 5: How many residual standard errors is the largest residual from the line? Is it surprising? Then answer the big question: How far off is a fish ruler, and how much should you trust it?

The largest residual is about 3.55 residual standard errors from the line, which is surprising because it’s much larger than a typical residual. The R-squared of 0.9207 shows that length explains about 92% of the variation in weight, but the residual standard error is 98.82 grams. Since the residual plot also shows a curved pattern, I wouldn’t completely trust the straight-line model to predict a fish’s weight.