library(Stat2Data)
library(dplyr)
library(ggplot2)
data("ThreeCars2017")
The purpose of this analysis is to determine whether Price (thousands of dollars) can be predicted from Mileage (thousands of miles) for Accord vehicles in the ThreeCars2017 dataset.
First, I examined scatterplots of all vehicles in the dataset.
ggplot(ThreeCars2017, aes(x = Mileage, y = Price)) +
geom_point() +
labs(
title = "ThreeCars2017: Mileage vs Price",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
ggplot(
ThreeCars2017,
aes(
x = Mileage,
y = Price,
color = CarType,
shape = CarType
)
) +
geom_point() +
labs(
title = "ThreeCars2017: Mileage vs Price by CarType",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
The cars are generally mixed together on the graph, with no major differences among the groups. However, the Maxima vehicles appear mostly in the upper half of the graph.
I filtered the data to include only Accord vehicles.
my_group <- ThreeCars2017 %>%
filter(CarType == "Accord")
ggplot(my_group, aes(x = Mileage, y = Price)) +
geom_point(alpha = 0.6) +
labs(
title = "Accord: Mileage vs Price",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
cor(
my_group$Mileage,
my_group$Price,
use = "complete.obs"
)
## [1] -0.8489441
The argument use = "complete.obs" tells R to skip rows
with missing values.
There is a strong negative linear association between Mileage and Price. The correlation coefficient is r = -0.8489441, which agrees with the scatterplot because the value is close to -1, indicating a strong negative linear relationship.
The response variable is Price and the explanatory variable is Mileage.
model <- lm(Price ~ Mileage, data = my_group)
coef(model)
## (Intercept) Mileage
## 20.8096129 -0.1198119
The regression equation is:
\[ \hat{Price}=20.8096129-0.1198119(Mileage) \]
For each additional 1 thousand miles of mileage, the model predicts that the price will decrease by approximately 0.1198119 thousand dollars on average.
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 = "Accord: Mileage vs Price",
subtitle = "Price = 20.8096129 + (-0.1198119)Mileage, r = -0.8489441",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
The regression line passes through the middle of the cloud of points, which suggests that it represents the overall pattern in the data well.
There is a strong negative linear relationship between Mileage and Price for Accord vehicles in the ThreeCars2017 dataset. The correlation coefficient of -0.8489441 indicates that higher-mileage vehicles tend to have lower prices. The regression model can be used to predict the price of an Accord from its mileage, and the strong correlation suggests that predictions are reasonably trustworthy for mileage values similar to those observed in the data.