In this analysis, we explore the relationship between a used Honda
Accord’s mileage and its price using the AccordPrice
dataset from the Stat2Data package.
Our main research question is: Can you price a used Honda Accord just by reading its odometer? Additionally, we want to know how far off our regression line’s predictions are and how much we should trust them.
We begin by loading the required packages, preparing our dataset, exploring the scatterplot, and calculating 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)"
)
# Calculate correlation coefficient r
cor(my_data$Mileage, my_data$Price)
## [1] -0.8489441
Next, we fit the linear regression model with Price as
the response variable (\(y\)) and
Mileage as the explanatory variable (\(x\)).
model <- lm(Price ~ Mileage, data = my_data)
coef(model)
## (Intercept) Mileage
## 20.8096129 -0.1198119
The negative correlation (\(r = -0.849\)) and negative slope indicate a strong inverse relationship: as a car’s mileage increases, its price tends to decrease.
Using the fitted slope and intercept, we superimpose the regression line onto our scatterplot.
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 Fitted Line",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
We inspect the first row of our dataset:
my_data %>%
select(Mileage, Price) %>%
head(1)
## Mileage Price
## 1 74.9 12
For the first car in the dataset: * Actual \(x\) (Mileage): \(74.9\) thousand miles * Actual \(y\) (Price): \(12.0\) thousand dollars
Using our regression equation \(\hat{y} = -0.1198119 \cdot x + 20.8096129\): \[\text{Predicted } y = -0.1198119(74.9) + 20.8096129 = 11.835694 \text{ thousand dollars}\]
\[\text{Residual} = \text{Actual } y - \text{Predicted } y = 12 - 11.835694 = 0.164306 \text{ thousand dollars}\]
Interpretation: This data point sits above the regression line by \(0.1643\) thousand dollars (about \(\$164.30\)). In context, for a Honda Accord with \(74.9\) thousand miles, the actual price is \(12\) thousand dollars, which is slightly higher than our model’s prediction.
We add fitted values and residuals to our dataset, then generate 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 Used Honda Accord Model",
x = "Mileage (thousands of miles)",
y = "Residual (thousands of dollars)"
)
Residual Plot Evaluation: The residual plot exhibits a fairly random, “boring” cloud of points around zero without any distinct curved pattern. This indicates that a straight line is an appropriate model shape for describing the relationship between mileage and price.
Finally, we evaluate how well the linear model fits the data using the summary statistics.
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