Introduction

Can you price a used Honda Accord just by reading its odometer? In this analysis, we explore the relationship between a used Honda Accord’s mileage (in thousands of miles) and its price (in thousands of dollars).

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

data(AccordPrice)
my_data <- AccordPrice

Step 1: Initial Visualization & Correlation

We fit a linear model where Mileage is the explanatory variable (\(x\)) and Price is the response variable (\(y\)).

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

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

The strong negative correlation (\(r = -0.8489\)) indicates that as mileage increases, the price of a used Honda Accord tends to decrease.

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

Step 2: Linear Model & Hand Residual Calculation

Below is the scatterplot with our regression line added:

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

We examined the first observation in the dataset:

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

For the first row (Mileage = 74.9 thousand miles, Price = 12 thousand dollars), we computed the residual by hand:

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

Interpretation: The residual is \(+0.1643\) (thousands of dollars), meaning this car’s actual price was $164.30 higher than predicted by the regression line.

Step 3: Residual Analysis

We added predicted values and residuals to our dataset and generated a residual plot to check model assumptions:

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
ggplot(my_data, aes(x = Mileage, y = residual)) +
  geom_point(alpha = 0.6) +
  geom_hline(yintercept = 0, color = "steelblue") +
  labs(
    title = "Residual Plot for Honda Accord Price Model",
    x = "Mileage (thousands of miles)",
    y = "Residual (thousands of dollars)"
  )

Interpretation: The residual plot is “boring”—a random scatter of points around zero with no clear curve or fanning pattern. This confirms that a straight-line model is appropriate for this dataset.

Step 4: Model Assessment (R-squared & Residual Standard Error)

Finally, we evaluated the summary of the 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

Interpretation: 1. \(R^2\): \(72.07\%\) of the variation in Honda Accord prices is explained by the linear relationship with mileage.