We begin by loading the required packages and the
ThreeCars2017 dataset. We then create two scatterplots: one
plain plot of all data points and one categorized by car model
(CarType).
# Load libraries
library(Stat2Data)
library(dplyr)
library(ggplot2)
# Load data
data("ThreeCars2017")
# Plain scatterplot of all data
ggplot(ThreeCars2017, aes(x = Mileage, y = Price)) +
geom_point() +
labs(
title = "Price vs. Mileage for Three Cars",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
# Scatterplot with color and shape mapped to CarType
ggplot(ThreeCars2017, aes(x = Mileage, y = Price, color = CarType, shape = CarType)) +
geom_point() +
labs(
title = "Price vs. Mileage by Car Type",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
Observation: Accords tend to be more expensive when they have lower mileage compared to the other car types in the dataset.
We now filter the dataset to focus strictly on Honda Accords, create a scatterplot, and calculate the correlation coefficient (\(r\)).
# Filter dataset to keep only Accord
my_group <- ThreeCars2017 %>%
filter(CarType == "Accord")
# Scatterplot for Accord group
ggplot(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)"
)
# Compute correlation coefficient r
cor(my_group$Mileage, my_group$Price, use = "complete.obs")
## [1] -0.8489441
Association & Correlation: The correlation coefficient is \(r = -0.8489441\). This indicates a strong, negative, roughly linear relationship between mileage and price for Honda Accords. As mileage increases, price consistently decreases.
Next, we fit a simple linear regression model with Price
as the response variable and Mileage as the explanatory
variable.
# Fit linear model
model <- lm(Price ~ Mileage, data = my_group)
# View coefficients
coef(model)
## (Intercept) Mileage
## 20.8096129 -0.1198119
Model Coefficients & Interpretation: -
Intercept: 20.8096129 (The predicted price
of a brand-new Accord with 0 miles is approximately 20.81 thousand
dollars, or $20,810). - Slope: -0.1198119
(For every additional 1,000 miles driven, the predicted price of a Honda
Accord decreases by approximately 0.1198 thousand dollars, or about
$120).
Finally, we overlay the estimated regression line on the scatterplot of Honda Accords using the calculated slope and intercept.
# Scatterplot with regression line overlaid
ggplot(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 with Linear Model",
subtitle = "Price = 20.8096129 - 0.1198119 * Mileage | r = -0.8489441",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
Model Verification: The regression line runs smoothly through the middle of the scatterplot points, confirming that the line correctly models the negative linear trend between mileage and price.