Introduction

Our big question is: Is a Porsche worth more than a Jaguar with the same mileage? We also want to know whether a multiple regression model with an indicator variable (and possibly an interaction variable) predicts price better than a single line.

First, we load the required packages and the PorscheJaguar dataset from Stat2Data.

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

data(PorscheJaguar)
my_data <- PorscheJaguar

Step 1: Exploring the Data and Fitting a Single Line

We start by plotting Price (response variable, in thousands of dollars) against Mileage (explanatory variable, in thousands of miles), colored and shaped by Car brand.

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 higher than the Jaguar points. Next, we fit a single-line regression model (model1) using only Mileage.

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 (52.7%) and the residual standard error is 12.89.

Step 2: Adding an Indicator Variable

To account for the two brands in lm(), we create an indicator variable PorscheIND where 1 represents a Porsche and 0 represents a 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 with both Mileage and PorscheIND.

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

Using the coefficients from model2, we write separate equations for each car brand:

Because both lines have the exact same slope, they appear parallel on the graph:

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

  • Indicator coefficient (17.958331): For two cars with the same mileage, a Porsche is predicted to cost 17.958331 thousand dollars ($17,958.33) more than a Jaguar.
  • Mileage coefficient (-0.602977): For two cars of the same brand, every additional 1,000 miles is associated with a predicted decrease in price of 0.602977 thousand dollars ($602.98).
  • Predictions at Mileage = 30 (30,000 miles):
    • Jaguar: 35.51624 thousand dollars ($35,516.24)
    • Porsche: 53.474571 thousand dollars ($53,474.57)
    • The difference between the two predicted prices is