In this analysis, we examine the relationship between car mileage and
price for Honda Accords using the ThreeCars2017 dataset
from the Stat2Data package. Our primary goal is to
determine whether we can predict an Accord’s price based on its
mileage.
First, we load the required packages and examine the dataset as a whole.
# Load required packages
library(Stat2Data)
library(dplyr)
library(ggplot2)
# Load dataset
data("ThreeCars2017")
# Plot 1: Plain scatterplot
ggplot(data = ThreeCars2017, aes(x = Mileage, y = Price)) +
geom_point() +
labs(
title = "Car Price vs. Mileage",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
# Plot 2: Scatterplot grouped by CarType
ggplot(data = ThreeCars2017, aes(x = Mileage, y = Price, color = CarType, shape = CarType)) +
geom_point() +
labs(
title = "Car Price vs. Mileage by Car Type",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
When comparing the groups in the second plot, we observe that the Accord and Maxima have the highest prices overall, with the Accord specifically having lower mileage. The Mazda model tends to have the lowest prices and higher mileage, though one Accord still displays the highest overall mileage in the dataset.
To prevent confounding across different car models, we filter the dataset to focus exclusively on Honda Accords.
# Filter for Accord only
my_group <- ThreeCars2017 %>%
filter(CarType == "Accord")
# Scatterplot of my_group
ggplot(data = my_group, aes(x = Mileage, y = Price)) +
geom_point(alpha = 0.6) +
labs(
title = "Price vs. Mileage for Honda Accords",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
# Calculate the correlation coefficient r
cor(my_group$Mileage, my_group$Price, use = "complete.obs")
## [1] -0.8489441
The scatterplot displays a linear association with a negative direction and moderate strength. Calculating the correlation coefficient yields \(r = -0.849\), which strongly aligns with the moderately strong down-to-the-right linear pattern seen in the scatterplot.
We fit a simple linear regression model with Price as the response variable (\(y\)) and Mileage as the explanatory variable (\(x\)).
# Fit the linear regression model
model <- lm(Price ~ Mileage, data = my_group)
# View the slope and intercept
coef(model)
## (Intercept) Mileage
## 20.8096129 -0.1198119
From coef(model), we obtain: *
Intercept: 20.81 * Slope: -0.1198
For every additional 1,000 miles on an Accord, the predicted price decreases by about \(119.80 (or 0.1198 thousand dollars). The negative sign of our slope matches our correlation coefficient (\)r = -0.849$).
Finally, we overlay the fitted linear regression line onto the scatterplot of Honda Accords using our calculated slope and intercept.
# Scatterplot with the regression line added
ggplot(data = my_group, aes(x = Mileage, y = Price)) +
geom_point(alpha = 0.6) +
geom_abline(intercept = 20.81, slope = -0.1198, color = "steelblue", size = 1) +
labs(
title = "Honda Accord Price vs. Mileage",
subtitle = "Price = 20.81 - 0.1198*(Mileage), r = -0.849",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
The fitted regression line passes directly through the middle of the data points as expected, providing a reliable visual check of our model fit.