Introduction

In this analysis, we use the PorscheJaguar dataset from the Stat2Data package to investigate two questions:

  1. Is a Porsche worth more than a Jaguar with the same mileage?
  2. Does a multiple regression equation with an indicator variable (and possibly an interaction variable) predict price better than a single regression line?

Step 1: Exploring the Groups and Fitting One Line

First, we load the required packages and dataset, and create a scatterplot of Price (in thousands of dollars) versus Mileage (in thousands of miles), colored and shaped by Car.

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

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, Porsches sit slightly higher in price than Jaguars, while both groups follow downward patterns with about the same steepness.

Next, we fit a single regression line (model1) predicting Price from Mileage alone, 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 Multiple R-squared is 0.527 and the residual standard error is 12.89 thousand dollars.

Step 2: Adding an Indicator Variable

Because lm() requires numerical inputs rather than text categories, we create an indicator variable PorscheIND that equals 1 when Car == "Porsche" and 0 when Car == "Jaguar".

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

Now we fit model2 using both Mileage and PorscheIND as explanatory variables.

model2 <- lm(Price ~ Mileage + PorscheIND, data = my_data)
coef(model2)
## (Intercept)     Mileage  PorscheIND 
##   53.605550   -0.602977   17.958331

From the output of coef(model2), the three estimated coefficients are:

Using these coefficients, we obtain two parallel regression lines with the same slope but different intercepts:

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 model2 and Making Predictions

  • Indicator coefficient (PorscheIND = 17.958331): For two cars with the same mileage, a Porsche is predicted to cost 17.958331 thousand dollars (about $17,958) more than a Jaguar.
  • Slope coefficient (Mileage = -0.602977): For every additional thousand miles on the odometer, the price of a car is predicted to decrease by 0.602977 thousand dollars, holding the car brand constant.
  • Predictions at Mileage = 30 (30,000 miles):
    • Jaguar: 35.516246 thousand dollars
    • Porsche: 53.474571 thousand dollars
    • The difference between the two predictions is 17.958325 thousand dollars, which matches the PorscheIND coefficient (17.958331) because the two lines are parallel and stay a constant vertical distance apart across all mileage levels.

Step 3: Adding an Interaction Variable

While model2 allows different intercepts, it forces both brands to have the exact same slope. Adding an interaction variable (PorscheINT = PorscheIND * Mileage) allows Porsches and Jaguars to have different slopes as well.

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

Now we fit model3 with Mileage, PorscheIND, and PorscheINT.

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

From coef(model3), the four estimated coefficients are:

From these coefficients, each brand gets its own intercept and its own 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 Separate Slopes (Model 3)",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

Interpreting model3

  • Interaction coefficient (PorscheINT = 0.03089793): The slope of the Porsche price line is 0.03089793 thousand dollars per thousand miles greater (less negative) than the slope of the Jaguar price line, indicating that Porsches depreciate only very slightly slower per thousand miles than Jaguars. (Note that in this model, PorscheIND = 16.86298877 represents only the predicted gap at Mileage = 0.)
  • Visual comparison: Even when allowed to have separate slopes, the two lines still look very close to parallel across the data.

Step 4: Comparing the Models and Answering the Main Question

Finally, we inspect summary(model2) and summary(model3) to compare all three models.

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

Model Comparison

  • model1 (Single line): Multiple R-squared = 0.527, Residual standard error = 12.89
  • model2 (Parallel lines with indicator): Multiple R-squared = 0.7648, Residual standard error = 9.25
  • model3 (Separate slopes with interaction): Multiple R-squared = 0.7648, Residual standard error = 9.25

Adding the indicator variable (model2 vs. model1) helped substantially: Multiple R-squared jumped from 0.527 to 0.7648 and the residual standard error dropped from 12.89 to 9.25, showing that accounting for car brand explains much more of the variation in price. By contrast, adding the interaction variable (model3 vs. model2) added almost nothing, leaving Multiple R-squared (0.7648) and the residual standard error (9.25) unchanged.

Conclusion

model2 is the best model to use because it achieves the highest R-squared (0.7648) and lowest residual standard error (9.25) without adding the unnecessary complexity of an interaction term that provides no meaningful improvement in fit.

To answer our main question: Yes, a Porsche is worth more than a Jaguar with the same mileage. Based on model2, a Porsche is predicted to cost 17.958331 thousand dollars (or about $17,958) more than a Jaguar with the same mileage.