Step 1: Load Data & Scatterplot

We begin by loading the required packages and the AccordPrice dataset from Stat2Data. We create a scatterplot to explore the relationship between Mileage (explanatory) and Price (response) and compute the correlation coefficient \(r\).

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

data("AccordPrice")
my_data <- AccordPrice

# Scatterplot of Price vs Mileage
ggplot(my_data, aes(x = Mileage, y = Price)) +
  geom_point() +
  labs(
    title = "Used Honda Accord Price vs. Mileage",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

# Correlation coefficient
cor(my_data$Mileage, my_data$Price)
## [1] -0.8489441

Next, we fit the linear model using lm() and inspect the model coefficients.

model <- lm(Price ~ Mileage, data = my_data)
coef(model)
## (Intercept)     Mileage 
##  20.8096129  -0.1198119

The negative correlation (\(r = -0.8489441\)) and negative slope (\(\hat{\beta}_1 = -0.1198119\)) indicate an inverse relationship: as an Accord’s mileage increases, its resale price tends to decrease.


Step 2: Fitted Line & Hand Residual Calculation

We overlay our regression line using geom_abline() with our estimated slope and intercept.

ggplot(my_data, aes(x = Mileage, y = Price)) +
  geom_point() +
  geom_abline(intercept = 20.8096129, slope = -0.1198119, color = "steelblue") +
  labs(
    title = "Used Honda Accord Price vs. Mileage with Linear Fit",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

We view the first row of our data to calculate its residual by hand:

my_data %>% 
  select(Mileage, Price) %>% 
  head(1)
##   Mileage Price
## 1    74.9    12

We calculate the residual (\(\text{Actual } y - \text{Predicted } y\)):

12 - (-0.1198119 * 74.9 + 20.8096129)
## [1] 0.1642984

Interpretation: Because the residual is positive (+0.164 thousand dollars), this specific Honda Accord is priced about $163 above what the linear model predicts.


Step 3: All Residuals & Residual Plot

We add the predicted values and residuals to our dataset using mutate(), fitted(), and residuals().

my_data <- my_data %>% 
  mutate(predicted = fitted(model), residual = residuals(model))

my_data %>% 
  select(Mileage, Price, predicted, residual) %>% 
  head()
##   Mileage Price predicted   residual
## 1    74.9  12.0  11.83570  0.1643021
## 2    53.0  17.9  14.45958  3.4404204
## 3    79.1  15.7  11.33249  4.3675123
## 4    50.1  12.5  14.80703 -2.3070342
## 5    62.0   9.5  13.38127 -3.8812720
## 6     4.8  21.5  20.23452  1.2654845

Next, we plot the residuals against Mileage to assess whether our linear model assumptions hold.

ggplot(my_data, aes(x = Mileage, y = residual)) +
  geom_point(alpha = 0.6) +
  geom_hline(yintercept = 0, color = "steelblue") +
  labs(
    title = "Residual Plot: Price vs. Mileage",
    x = "Mileage (thousands of miles)",
    y = "Residual (thousands of dollars)"
  )

Interpretation: The residual plot appears boring—a random cloud of points around \(y = 0\) with no clear curve or fan shape. This confirms that fitting a straight line is an appropriate model for this dataset.


Step 4: Model Assessment (\(R^2\) & Residual Standard Error)

Finally, we evaluate the summary output of our fitted linear model.

summary(model)
## 
## Call:
## lm(formula = Price ~ Mileage, data = my_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.5984 -1.8169 -0.4148  1.4502  6.5655 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  20.8096     0.9529   21.84  < 2e-16 ***
## Mileage      -0.1198     0.0141   -8.50 3.06e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.085 on 28 degrees of freedom
## Multiple R-squared:  0.7207, Adjusted R-squared:  0.7107 
## F-statistic: 72.25 on 1 and 28 DF,  p-value: 3.055e-09

Interpretations: 1. \(R^2\): About 72.07% of the variation in used Honda Accord prices is explained by the car’s mileage. 2. Residual Standard Error: The model’s predictions typically deviate from the actual price by about $3,085 (3.085 thousand dollars).