In this analysis, we investigate whether we can predict a car’s price
based on its mileage using data from the ThreeCars2017
dataset.
library(Stat2Data)
library(dplyr)
library(ggplot2)
data("ThreeCars2017")
First, we look at scatterplots of all cars in the dataset—both overall and separated by car model.
# 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)"
)
# Scatterplot 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)"
)
Observation: Across all car types, there is a general negative relationship between mileage and price. Looking at the distinct car types, each model clusters in different price ranges with varying spreads.
We now filter the dataset to focus exclusively on Honda Accords
(CarType == "Accord").
my_group <- ThreeCars2017 %>%
filter(CarType == "Accord")
# Scatterplot for Accords
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 correlation
cor(my_group$Mileage, my_group$Price, use = "complete.obs")
## [1] -0.8489441
Correlation and Association: The correlation coefficient is \(r = -0.8489441\). This indicates a strong, negative, linear relationship between mileage and price for Honda Accords.
We fit a simple linear regression model with Price as
the response variable and Mileage as the explanatory
variable.
model <- lm(Price ~ Mileage, data = my_group)
coef(model)
## (Intercept) Mileage
## 20.8096129 -0.1198119
Model Parameters: * Intercept:
20.8096129 * Slope:
-0.1198119
Slope Interpretation: For every additional 1 thousand miles driven, the predicted price of a Honda Accord decreases by 0.1198119 thousand dollars (approximately $119.81).
Finally, we plot the Honda Accord data along with our fitted regression line.
ggplot(data = my_group, aes(x = Mileage, y = Price)) +
geom_point(alpha = 0.6) +
geom_abline(
intercept = 20.8096129,
slope = -0.1198119,
color = "steelblue",
linewidth = 1
) +
labs(
title = "Price vs. Mileage for Honda Accords",
subtitle = "Price = 20.8096129 - 0.1198119(Mileage) | r = -0.8489",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
Visual Assessment: The fitted regression line passes directly through the center of the scatterplot points, confirming that our calculated intercept and slope correctly summarize the linear trend.