Introduction

In this analysis, we investigate the PorscheJaguar dataset from the Stat2Data package to answer our main research question: Is a Porsche worth more than a Jaguar with the same mileage? Along the way, we test whether adding an indicator variable—and potentially an interaction variable—predicts Price (in thousands of dollars) from Mileage (in thousands of miles) better than a single regression line.

Step 1: Exploring the Data and Fitting a Single Line

First, we load the required packages, save PorscheJaguar as my_data, and plot Price against Mileage colored and shaped by car brand (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)"
  )

Looking at the scatterplot, Porsches sit noticeably higher on the graph than Jaguars for comparable mileage, while the Jaguar points appear to follow a slightly steeper downward pattern (with one low-mileage, lower-priced Jaguar standing out from the rest).

Next, we fit a simple linear regression model (model1) using Mileage as the sole explanatory variable for Price.

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, R-squared is 0.527 and the residual standard error is 12.89 (thousand dollars). Because this single line ignores car brand, it leaves substantial variation unexplained.

Step 2: Adding an Indicator Variable

To account for brand differences in lm(), we create an indicator variable PorscheIND that equals 1 when Car is "Porsche" and 0 when Car is "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, which predicts Price using both Mileage and PorscheIND.

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

From coef(model2), the intercept is 53.605550, the Mileage coefficient is -0.602977, and the PorscheIND coefficient is 17.958331. Substituting 0 and 1 for PorscheIND gives each brand’s parallel regression line:

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 by Car Brand (Parallel Lines Model)",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

Interpreting Coefficients and Predictions in model2

  • Brand (PorscheIND = 17.958331): For two cars with the same mileage, a Porsche is predicted to cost $17,958.33 more than a Jaguar on average.
  • Mileage (Mileage = -0.602977): For two cars of the same brand, every additional 1,000 miles decreases the predicted price by approximately $602.98 on average.
  • Predictions at 30,000 miles (Mileage = 30):
    • Jaguar: 35.51623 thousand dollars ($35,516.23)
    • Porsche: 53.474561 thousand dollars ($53,474.56)
    • The difference between these two predictions (53.474561 - 35.51623 = 17.958331) is identical to the PorscheIND coefficient because the two lines are parallel.

Step 3: Testing an Interaction Variable

To check whether the two car brands depreciate at different rates with mileage, we create the 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

From coef(model3), the intercept is 54.22746396, Mileage is -0.62029888, PorscheIND is 16.86298877, and PorscheINT is 0.03089793. Separating the equations by brand gives:

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 by Car Brand (Interaction Model)",
    x = "Mileage (thousands of miles)",
    y = "Price (thousands of dollars)"
  )

The interaction coefficient (0.03089793) means that for every additional 1,000 miles, the predicted price of a Porsche decreases by $30.90 less than the predicted price of a Jaguar. Visually, however, the two lines still look very close to parallel across the range of the data.

Step 4: Comparing Models and Final Conclusion

Finally, we inspect summary(model2) and summary(model3) to compare model fit across 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 Explanatory Variables R-squared Residual Standard Error
model1 (Single Line) Mileage 0.527 12.89
model2 (Indicator / Parallel Lines) Mileage, PorscheIND 0.7644 9.174
model3 (Interaction) Mileage, PorscheIND, PorscheINT 0.7648 9.25

Conclusion

Adding the PorscheIND indicator variable in model2 improved the fit dramatically over the single-line model1—raising R-squared from 0.527 to 0.7644 and lowering the residual standard error from 12.89 to 9.174. In contrast, adding the interaction term in model3 barely changed R-squared (0.7648) and slightly worsened the residual standard error (9.25). Therefore, model2 (the parallel-lines indicator model) is the best fit. Based on our analysis, an average Porsche is worth more than an average Jaguar with the same mileage—specifically about $17,958.33 more.