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

Research Question

Is a Porsche worth more than a Jaguar with the same mileage? Additionally, does one equation with an indicator variable (and maybe an interaction variable) predict better than a single line?

Step 1: Exploring the Groups and Fitting a Single Line

We begin by loading the PorscheJaguar dataset from the Stat2Data package and plotting Price (in thousands of dollars) against Mileage (in thousands of miles), colored and shaped by Car (Jaguar vs. Porsche).

data("PorscheJaguar")
my_data <- PorscheJaguar

ggplot(my_data, aes(x = Mileage, y = Price, color = Car, shape = Car)) +
  geom_point() +
  labs(
    title = "Price vs. Mileage for Porsches and Jaguars",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

In the scatterplot, the Porsche points sit noticeably higher than the Jaguar points. Both groups show a downward trend as mileage increases, and the steepness of their downward patterns looks roughly similar.

Next, we fit a single regression line (model1) ignoring car brand:

model1 <- lm(Price ~ Mileage, data = my_data)
summary(model1)
## 
## Call:
## lm(formula = Price ~ Mileage, data = my_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -33.088 -10.372   2.868   9.233  20.868 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 62.92843    3.16885  19.858  < 2e-16 ***
## Mileage     -0.61269    0.07621  -8.039 5.26e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 12.89 on 58 degrees of freedom
## Multiple R-squared:  0.527,  Adjusted R-squared:  0.5189 
## F-statistic: 64.63 on 1 and 58 DF,  p-value: 5.263e-11

For model1, the R-squared is 0.527 and the residual standard error is 12.89 thousand dollars.

Step 2: Adding an Indicator Variable (PorscheIND)

To account for the difference in price between the two car brands, we create an indicator variable PorscheIND that equals 1 for a Porsche and 0 for a Jaguar, and fit model2 with both Mileage and PorscheIND.

my_data <- mutate(my_data, PorscheIND = ifelse(Car == "Porsche", 1, 0))

my_data %>%
  select(Mileage, Price, Car, PorscheIND) %>%
  head()
##   Mileage Price     Car PorscheIND
## 1    21.5  69.4 Porsche          1
## 2    43.0  56.9 Porsche          1
## 3    19.9  49.9 Porsche          1
## 4    36.0  47.4 Porsche          1
## 5    44.0  42.9 Porsche          1
## 6    49.8  36.9 Porsche          1
model2 <- lm(Price ~ Mileage + PorscheIND, data = my_data)
coef(model2)
## (Intercept)     Mileage  PorscheIND 
##   53.605550   -0.602977   17.958331

From the model coefficients ((Intercept) = 53.605550, Mileage = -0.602977, and PorscheIND = 17.958331), we obtain two parallel lines—one for each brand:

ggplot(my_data, aes(x = Mileage, y = Price, color = Car, shape = Car)) +
  geom_point() +
  geom_abline(intercept = 53.605550, slope = -0.602977, color = "#F8766D") +
  geom_abline(intercept = 71.563881, slope = -0.602977, color = "#00BFC4") +
  labs(
    title = "Price vs. Mileage with Parallel Lines (Model 2)",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

Interpreting the Coefficients and Predictions (Model 2)

  • PorscheIND (17.958331): For two cars with the same mileage, a Porsche is predicted to cost 17.958331 thousand dollars more on average than a Jaguar.
  • Mileage (-0.602977): For two cars of the same brand, as mileage increases by 1 thousand miles, predicted price decreases by 0.602977 thousand dollars on average.
  • Predictions at Mileage = 30 (30,000 miles):
    • Porsche: 53.474571 thousand dollars
    • Jaguar: 35.51624 thousand dollars
    • The difference between the two predicted prices is 17.958331 thousand dollars, which is identical to the PorscheIND coefficient because the two regression lines are parallel.

Step 3: Adding an Interaction Variable (PorscheINT)

To test whether Porsches and Jaguars depreciate at different rates per mile, we create an interaction variable PorscheINT = PorscheIND * Mileage and fit model3.

my_data <- mutate(my_data, PorscheINT = PorscheIND * Mileage)

model3 <- lm(Price ~ Mileage + PorscheIND + PorscheINT, data = my_data)
coef(model3)
## (Intercept)     Mileage  PorscheIND  PorscheINT 
## 54.22746396 -0.62029888 16.86298877  0.03089793

Using the four coefficients ((Intercept) = 54.22746396, Mileage = -0.62029888, PorscheIND = 16.86298877, and PorscheINT = 0.03089793), each group’s line has its own intercept and slope:

ggplot(my_data, aes(x = Mileage, y = Price, color = Car, shape = Car)) +
  geom_point() +
  geom_abline(intercept = 54.22746396, slope = -0.62029888, color = "#F8766D") +
  geom_abline(intercept = 71.09045273, slope = -0.58940095, color = "#00BFC4") +
  labs(
    title = "Price vs. Mileage with Interaction Lines (Model 3)",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

Interpreting the Interaction (Model 3)

  • PorscheINT (0.03089793): The slope of predicted price on mileage is 0.03089793 thousand dollars per thousand miles higher (less steep) for Porsches than for Jaguars—meaning that for each additional 1 thousand miles, a Porsche’s predicted price drops by 0.03089793 thousand dollars less on average than a Jaguar’s.
  • Visually, the two lines on the scatterplot still look very close to parallel across the data points.

Step 4: Model Comparison and Conclusion

Finally, we inspect the summaries of model2 and model3 to compare their fit with model1.

summary(model2)
## 
## Call:
## lm(formula = Price ~ Mileage + PorscheIND, data = my_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -23.8420  -5.6835  -0.5041   5.4209  19.3490 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 53.60555    2.56951  20.862  < 2e-16 ***
## Mileage     -0.60298    0.05427 -11.111 6.89e-16 ***
## PorscheIND  17.95833    2.36951   7.579 3.45e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.174 on 57 degrees of freedom
## Multiple R-squared:  0.7644, Adjusted R-squared:  0.7562 
## F-statistic: 92.48 on 2 and 57 DF,  p-value: < 2.2e-16
summary(model3)
## 
## Call:
## lm(formula = Price ~ Mileage + PorscheIND + PorscheINT, data = my_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -24.3271  -5.6200  -0.4819   5.0278  18.8120 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 54.22746    3.41097  15.898  < 2e-16 ***
## Mileage     -0.62030    0.08254  -7.515 4.88e-10 ***
## PorscheIND  16.86299    4.58044   3.682 0.000523 ***
## PorscheINT   0.03090    0.11024   0.280 0.780302    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.25 on 56 degrees of freedom
## Multiple R-squared:  0.7648, Adjusted R-squared:  0.7522 
## F-statistic: 60.68 on 3 and 56 DF,  p-value: < 2.2e-16

Conclusion

Adding the indicator variable in model2 dramatically improved the fit compared to a single line—R-squared jumped from 0.527 to 0.7644, and the residual standard error dropped from 12.89 to 9.174 thousand dollars. In contrast, adding the interaction variable in model3 added almost nothing (R-squared barely changed from 0.7644 to 0.7648, while the residual standard error slightly worsened to 9.25 thousand dollars). Therefore, model2 is the best model.

Answer to the Research Question: Yes, a Porsche is worth more than a Jaguar with the same mileage—specifically, 17.958331 thousand dollars more on average.