This report evaluates three linear regression models to predict
vehicle Price (in thousands of dollars) from
Mileage (in thousands of miles) across two car brands
(Jaguar and Porsche):
Price
using only Mileage.PorscheIND) to allow different intercepts by
brand while keeping a common slope.PorscheINT) to allow both different intercepts and
different slopes by brand.model1 <- lm(Price ~ Mileage, data = my_data)
coef(model1)
## (Intercept) Mileage
## 62.9284289 -0.6126897
ggplot(my_data, aes(x = Mileage, y = Price, color = Car, shape = Car)) +
geom_point() +
geom_abline(intercept = coef(model1)[1], slope = coef(model1)[2]) +
labs(
title = "Price vs. Mileage (Single Regression Line)",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
We create PorscheIND (1 for Porsche,
0 for Jaguar) and fit model2.
my_data <- mutate(my_data, PorscheIND = ifelse(Car == "Porsche", 1, 0))
model2 <- lm(Price ~ Mileage + PorscheIND, data = my_data)
coef(model2)
## (Intercept) Mileage PorscheIND
## 53.605550 -0.602977 17.958331
Mileage = 30)PorscheIND = 0): \(\widehat{\text{Price}} = 53.6057 - 0.60298 \times
\text{Mileage}\)
Mileage = 30: $35,516.24PorscheIND = 1): \(\widehat{\text{Price}} = 71.5640 - 0.60298 \times
\text{Mileage}\)
Mileage = 30: $53,474.57PorscheIND (17.958331):
For two cars with the same mileage, a Porsche is predicted to cost
$17,958.33 more than a Jaguar, on average. Because the
lines are parallel, the difference between predicted prices at any
mileage is always equal to this coefficient.Mileage (-0.602982): For
two cars of the same brand, every additional 1,000 miles decreases the
predicted price by approximately $602.98, on
average.ggplot(my_data, aes(x = Mileage, y = Price, color = Car, shape = Car)) +
geom_point() +
geom_abline(intercept = 53.605704, slope = -0.602982, color = "#F8766D") +
geom_abline(intercept = 71.564035, slope = -0.602982, color = "#00BFC4") +
labs(
title = "Price vs. Mileage by Car Brand (Parallel Lines Model)",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
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
PorscheIND = 0,
PorscheINT = 0): Intercept =
54.22746396, Slope = -0.62029888PorscheIND = 1,
PorscheINT = Mileage): Intercept =
71.09045273, Slope = -0.58940095PorscheINT (0.03089793):
For every additional 1,000 miles, the predicted