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

data("ThreeCars2017")

Step 1: Exploring the Data

The purpose of this analysis is to see whether mileage can be used to predict the price of a car and how much I should trust that prediction.

The explanatory variable is Mileage, measured in thousands of miles, and the response variable is Price, measured in thousands of dollars.

ggplot(ThreeCars2017, aes(x = Mileage, y = Price)) +
  geom_point() +
  labs(
    title = "Mileage vs. Price for All Cars",
    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 = "Mileage vs. Price by Car Type",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)",
    color = "Car Type",
    shape = "Car Type"
  )

Observations

All three car types show a similar downward trend, with prices generally decreasing as mileage increases. However, there is some variation in prices among the different car types, even at similar mileages.

Step 2: Focusing on Accord

For the rest of the analysis, I focused only on Accord cars.

my_group <- ThreeCars2017 %>%
  filter(CarType == "Accord")
ggplot(my_group, aes(x = Mileage, y = Price)) +
  geom_point(alpha = 0.6) +
  labs(
    title = "Mileage vs. Price for Accord",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

Next, I calculated the correlation coefficient to understand the direction and strength of the relationship.

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

The scatterplot shows a strong, negative, roughly linear relationship between mileage and price.

The correlation coefficient is -0.8489441, which supports what I observed in the scatterplot because it is close to -1. This means that Accords with higher mileage generally have lower prices.

Step 3: Finding the Regression Line

I used simple linear regression to model the relationship between Mileage and Price.

Since Price is the response variable and Mileage is the explanatory variable, I used Price ~ Mileage.

model <- lm(Price ~ Mileage, data = my_group)

coef(model)
## (Intercept)     Mileage 
##  20.8096129  -0.1198119

The regression equation is:

Predicted Price = 20.8096129 - 0.1198119 × Mileage

Both Price and Mileage are measured in thousands.

Interpreting the Slope

The slope is -0.1198119. This means that for every additional 1,000 miles, the predicted price of an Accord decreases by about $119.81.

The negative slope also matches the negative correlation coefficient.

Step 4: Adding the Regression Line

I added the regression line to the scatterplot to see how well it follows the data.

ggplot(my_group, aes(x = Mileage, y = Price)) +
  geom_point(alpha = 0.6) +
  geom_abline(
    intercept = 20.8096129,
    slope = -0.1198119,
    color = "steelblue",
    size = 1
  ) +
  labs(
    title = "Mileage vs. Price for Accord",
    subtitle = "Price = 20.8096129 - 0.1198119 x Mileage; r = -0.8489441",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

Checking the Line

The regression line passes through the middle of the points and follows the overall downward trend. This suggests that the line represents the relationship between mileage and price reasonably well.

Step 5: Making a Prediction

I used the regression equation to predict the price of an Accord with 60,000 miles.

Since Mileage is measured in thousands of miles, I used x = 60.

predicted_price <- 20.8096129 + (-0.1198119) * 60

predicted_price
## [1] 13.6209

The result is measured in thousands of dollars.

According to the regression equation, an Accord with 60,000 miles has a predicted price of approximately $13,621.

Step 6: Evaluating the Prediction

To decide how much I should trust this prediction, I examined the mileage range in the dataset.

range(my_group$Mileage, na.rm = TRUE)
## [1]   3.0 150.5

I also looked at cars with mileage between 50,000 and 70,000 miles.

nearby_cars <- my_group %>%
  filter(Mileage >= 50, Mileage <= 70)

data.frame(
  Number_of_Cars = nrow(nearby_cars),
  Minimum_Price = if (all(is.na(nearby_cars$Price))) NA_real_
                  else min(nearby_cars$Price, na.rm = TRUE),
  Maximum_Price = if (all(is.na(nearby_cars$Price))) NA_real_
                  else max(nearby_cars$Price, na.rm = TRUE)
)
##   Number_of_Cars Minimum_Price Maximum_Price
## 1              8             9          17.9

Since 60,000 miles falls within the observed mileage range, this prediction is an example of interpolation rather than extrapolation.

The strong negative correlation and roughly linear pattern make the prediction more reasonable. However, the actual price of an individual car could still differ from the predicted price.

Step 7: Predicting Far Outside the Data

Next, I considered predicting the price of an Accord with 250,000 miles.

far_predicted_price <- 20.8096129 + (-0.1198119) * 250

far_predicted_price
## [1] -9.143362

Interpreting the Far Prediction

The predicted price for an Accord with 250,000 miles is approximately -$9,143.36, which is not realistic because a car cannot have a negative selling price.

This is an example of extrapolation because 250,000 miles is outside the mileage range in the dataset. I would not trust this prediction because the relationship between mileage and price might not continue in the same way beyond the observed data.

Conclusion

Overall, mileage appears to be a useful variable for predicting the price of an Accord.

The scatterplot shows a strong, negative, roughly linear relationship, which is supported by the correlation coefficient of -0.8489441.

The regression equation predicts that the price of an Accord decreases as mileage increases.

I would trust the prediction at 60,000 miles more than the prediction at 250,000 miles because it is within the observed data range.

However, mileage does not explain every difference in car prices, so predictions should still be treated as estimates.