In this analysis, we use the PorscheJaguar dataset from
the Stat2Data package to investigate two questions:
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.
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:
(Intercept): 53.605550Mileage: -0.602977PorscheIND: 17.958331Using these coefficients, we obtain two parallel regression lines with the same slope but different intercepts:
PorscheIND = 0): \(\text{Price} = 53.605550 -
0.602977(\text{Mileage})\)PorscheIND = 1): \(\text{Price} = (53.605550 + 17.958331) -
0.602977(\text{Mileage}) = 71.563881 -
0.602977(\text{Mileage})\)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)"
)
model2 and Making PredictionsPorscheIND = 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.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.Mileage = 30 (30,000
miles):
PorscheIND coefficient (17.958331) because the
two lines are parallel and stay a constant vertical distance apart
across all mileage levels.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:
(Intercept): 54.22746396Mileage: -0.62029888PorscheIND: 16.86298877PorscheINT: 0.03089793From these coefficients, each brand gets its own intercept and its own slope:
PorscheIND = 0): \(\text{Price} = 54.22746396 -
0.62029888(\text{Mileage})\)PorscheIND = 1): \(\text{Price} = (54.22746396 + 16.86298877) +
(-0.62029888 + 0.03089793)(\text{Mileage}) = 71.09045273 -
0.58940095(\text{Mileage})\)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)"
)
model3PorscheINT = 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.)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
model1 (Single line): Multiple
R-squared = 0.527, Residual standard error =
12.89model2 (Parallel lines with
indicator): Multiple R-squared = 0.7648,
Residual standard error = 9.25model3 (Separate slopes with
interaction): Multiple R-squared = 0.7648,
Residual standard error = 9.25Adding 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.
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.