Step 1: Data Setup & Initial Exploration

We load the necessary packages and the Perch dataset from Stat2Data to explore whether we can predict a perch’s weight using its length.

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

data("Perch")
my_data <- Perch

# Scatterplot of Weight vs. Length
ggplot(my_data, aes(x = Length, y = Weight)) +
  geom_point(alpha = 0.7) +
  labs(
    title = "Perch Weight vs. Length",
    x = "Length (cm)",
    y = "Weight (grams)"
  )

# Compute correlation coefficient r
cor(my_data$Length, my_data$Weight)
## [1] 0.9595061

The correlation coefficient between Length and Weight is \(r = 0.9595061\), showing a strong positive linear relationship.

Next, we fit a linear model with Weight as the response variable and Length as the explanatory variable.

model <- lm(Weight ~ Length, data = my_data)
coef(model)
## (Intercept)      Length 
##  -652.78714    35.00089

From R output, the estimated coefficients are: * Intercept: -6787.2806 * Slope: 54.6225

Step 2: Fitting the Line and Manual Residual Calculation

We overlay our fitted regression line onto the scatterplot using our calculated slope and intercept.

ggplot(my_data, aes(x = Length, y = Weight)) +
  geom_point(alpha = 0.7) +
  geom_abline(intercept = -6787.2806, slope = 54.6225, color = "steelblue") +
  labs(
    title = "Perch Weight vs. Length with Fitted Line",
    x = "Length (cm)",
    y = "Weight (grams)"
  )

Let’s look at the first observation (row 1) in the dataset to calculate its residual by hand (\(\text{Residual} = \text{Actual } y - \text{Predicted } y\)).

my_data %>% 
  select(Obs, Length, Weight) %>% 
  head(1)
##   Obs Length Weight
## 1 104    8.8    5.9

Calculating the residual for Row 1:

5.9 - (54.6225 * 8.8 + (-6787.2806))
## [1] 6312.503

Interpretation: The residual for the first observation is \(6312.503\). Because this residual is positive, the data point lies above the fitted regression line by \(6312.503\) grams.

Step 3: Residual Analysis & Diagnostic Plot

Now we compute fitted values and residuals for all observations, then construct a residual plot to evaluate model assumptions.

# Add fitted values and residuals to the data
my_data <- my_data %>% 
  mutate(predicted = fitted(model), residual = residuals(model))

# View first few rows
my_data %>% 
  select(Obs, Length, Weight, predicted, residual) %>% 
  head()
##   Obs Length Weight   predicted  residual
## 1 104    8.8    5.9 -344.779274 350.67927
## 2 105   14.7   32.0 -138.274002 170.27400
## 3 106   16.0   40.0  -92.772841 132.77284
## 4 107   17.2   51.5  -50.771769 102.27177
## 5 108   18.5   70.0   -5.270607  75.27061
## 6 109   19.2  100.0   19.230019  80.76998
# Residual Plot
ggplot(my_data, aes(x = Length, y = residual)) +
  geom_point(alpha = 0.6) +
  geom_hline(yintercept = 0, color = "steelblue") +
  labs(
    title = "Residual Plot: Perch Weight vs. Length",
    x = "Length (cm)",
    y = "Residual (grams)"
  )

Interpretation: The residual plot displays a random “boring” scatter around zero with no apparent curvature or clear pattern. This suggests that using a straight-line model is appropriate for this data.

Step 4: Model Assessment (\(R^2\) and RSE)

We examine the model summary to evaluate how well the line accounts for variation in weight and assess the magnitude of typical prediction errors.

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

Interpretations: * \(R^2 = 0.4937\): \(49.37\%\) of the variation in perch weight is explained by the linear relationship with length. * Residual Standard Error (\(360.2\)): On average, predicted weights from the model differ from actual perch weights by about \(360.2\) grams.