Load Packages and Data

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

data(PorscheJaguar)

my_data <- PorscheJaguar

Step 1: Look at the Groups and Fit One Line

Research Question: Is a Porsche worth more than a Jaguar with the same mileage?

The explanatory variable is Mileage (thousands of miles), and the response variable is Price (thousands of dollars). The group variable is Car (Jaguar or Porsche).

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

First, fit a single regression line using Mileage to predict 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

Analysis:

The R-squared was 0.5270, meaning that Mileage explained 52.7% of the variation in Price. The residual standard error was 12.89.

In the scatterplot, Porsche prices generally sit higher than Jaguar prices, although both groups have relatively high and similar patterns.

Step 2: Add an Indicator Variable

The lm() function needs numbers, so an indicator variable turns the two groups into 1s and 0s. PorscheIND equals 1 for a Porsche and 0 for 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

Next, fit Model 2 using Mileage and PorscheIND.

model2 <- lm(Price ~ Mileage + PorscheIND, data = my_data)

coef(model2)
## (Intercept)     Mileage  PorscheIND 
##   53.605550   -0.602977   17.958331

The regression equation is:

Price = 53.605550 - 0.602977(Mileage) + 17.958331(PorscheIND)

The separate equations are:

Jaguar:

Price = 53.605550 - 0.602977(Mileage)

Porsche:

Price = 71.56388 - 0.602977(Mileage)

ggplot(my_data, aes(x = Mileage, y = Price, color = Car)) +
  geom_point() +
  geom_abline(intercept = 53.605550, slope = -0.602977,
              color = "#F8766D") +
  geom_abline(intercept = 71.56388, slope = -0.602977,
              color = "#00BFC4") +
  labs(title = "Price vs. Mileage: Jaguar and Porsche",
       x = "Mileage (thousands of miles)",
       y = "Price (thousands of dollars)")

Interpretation of the Indicator Coefficient:

For two cars with the same mileage, a Porsche is predicted to cost 17.958331 thousand dollars more than a Jaguar.

Interpretation of the Mileage Coefficient:

For every additional 1,000 miles, the predicted price of a car decreases by 0.602977 thousand dollars, holding the car type constant.

Predictions at Mileage = 30:

53.605550 - 0.602977 * 30
## [1] 35.51624
71.56388 - 0.602977 * 30
## [1] 53.47457
53.47457 - 35.51624
## [1] 17.95833

The predicted Jaguar price was 35.51624 thousand dollars, and the predicted Porsche price was 53.47457 thousand dollars.

The difference between the predictions was approximately 17.95833 thousand dollars, which is essentially equal to the PorscheIND coefficient.

The two lines are parallel because they have the same slope.

Step 3: Add an Interaction Variable

The indicator variable allows the two groups to have different intercepts but forces them to have the same slope. An interaction variable, which multiplies the indicator by the explanatory variable, allows the slopes to differ too.

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

Fit Model 3 using 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

The coefficients are:

The separate equations are:

Jaguar:

Price = 54.22746396 - 0.62029888(Mileage)

Porsche:

Price = 71.09045 - 0.589401(Mileage)

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

Interpretation of the Interaction Coefficient:

The interaction coefficient is 0.03089793. For every additional 1,000 miles, the predicted price of a Porsche decreases approximately 0.0309 thousand dollars less than the predicted price of a Jaguar.

The two lines have slightly different slopes, although they look close to parallel.

The PorscheIND coefficient represents the predicted price difference when Mileage equals zero, not the difference at every mileage.

Step 4: Which Model Is Best?

Compare the results from Model 2 and Model 3.

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:

Model R-squared Residual Standard Error
Model 1 0.5270 12.89
Model 2 0.7644 9.174
Model 3 0.7648 9.25

Adding PorscheIND improved the model because R-squared increased from 0.5270 to 0.7644 and residual standard error decreased from 12.89 to 9.174.

Adding PorscheINT did not provide a meaningful improvement because R-squared increased only slightly, while residual standard error increased.

Final Model Selection:

I would personally choose Model 2 because its more simple and doesn’t need an interaction variable. It explains almost the same amount of variation as Model 3 and has a lower residual SE.

Final Conclusion:

A Porsche is predicted to cost about $17,958 more than a Jaguar with the same amount of mileage. This information is supported by the 17.958331 coefficient in Model 2.

Overall, adding an indicator variable improved the model, while adding an interaction variable provided almost no additional predictive benefit.