library(Stat2Data)
library(dplyr)
library(ggplot2)

data("ThreeCars2017")

Introduction

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.

Step 1: Explore the Full Dataset

First, I examined scatterplots of all vehicles in the dataset.

Scatterplot of All Data

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)"
  )

Scatterplot by Car Type

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)"
  )

Interpretation

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.

Step 2: Focus on Accord Vehicles

I filtered the data to include only Accord vehicles.

my_group <- ThreeCars2017 %>%
  filter(CarType == "Accord")

Scatterplot for Accord Vehicles

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)"
  )

Correlation

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.

Interpretation

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.

Step 3: Fit a Linear Regression Model

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

Regression Equation

  • Intercept = 20.8096129
  • Slope = -0.1198119

The regression equation is:

\[ \hat{Price}=20.8096129-0.1198119(Mileage) \]

Interpretation of the Slope

For each additional 1 thousand miles of mileage, the model predicts that the price will decrease by approximately 0.1198119 thousand dollars on average.

Step 4: Add the Regression Line

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)"
  )

Interpretation

The regression line passes through the middle of the cloud of points, which suggests that it represents the overall pattern in the data well.

Conclusion

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.