Introduction & Data Exploration

In this analysis, we investigate whether we can predict the price of a used car based on its mileage, and how much we should trust that prediction. We use data from the ThreeCars2017 dataset in the Stat2Data package.

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

data("ThreeCars2017")

First, we explore the scatterplot of all cars in the dataset, colored and shaped by car type to observe group patterns.

# Plain scatterplot of all data
ggplot(ThreeCars2017, aes(x = Mileage, y = Price)) +
  geom_point() +
  labs(
    title = "Price vs. Mileage for Used Cars",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

# Scatterplot with group variable mapped to color and shape
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)",
    color = "Car Type",
    shape = "Car Type"
  )

When looking at all car types together, the data points appear pretty scattered.


Group Analysis: Honda Accords

To perform a focused linear regression analysis, we filter the dataset to examine only Honda Accords (CarType == "Accord").

# Filter dataset for Accord
my_group <- ThreeCars2017 %>%
  filter(CarType == "Accord")

# Scatterplot for Honda Accords
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)"
  )

# Calculate correlation coefficient r
cor(my_group$Mileage, my_group$Price, use = "complete.obs")
## [1] -0.8489441

Correlation Analysis

The association between Mileage and Price for Honda Accords is negative, linear, and of moderate strength. The calculated correlation coefficient is \(r = -0.8489441\), which indicates a clear negative relationship: as mileage increases, price generally decreases.


Simple Linear Regression Model

We fit a simple linear regression model with Price as the response variable and Mileage as the explanatory variable.

# Fit the linear model
model <- lm(Price ~ Mileage, data = my_group)

# Output slope and intercept
coef(model)
## (Intercept)     Mileage 
##  20.8096129  -0.1198119

Model Interpretation

From the output of coef(model): - Intercept: \(20.8096129\) - Slope: \(-0.1198119\)

Interpretation in Context: For every additional 1,000 miles driven on a Honda Accord, the predicted price decreases by approximately 0.120 thousand dollars ($120).


Visualizing the Fitted Line

Finally, we overlay the regression line on top of our scatterplot using the slope and intercept parameters obtained from our fitted model.

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",
    subtitle = "Price = 20.81 - 0.120(Mileage), r = -0.85",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

The line passes through the center of the cloud of points, visually confirming a valid fit for our model.