library(Stat2Data)
library(dplyr)
library(ggplot2)
data(ThreeCars2017)
ggplot(ThreeCars2017, aes(x = Mileage, y = Price)) +
geom_point() +
labs(
title = "Mileage vs. Price for Used Cars",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
ggplot(ThreeCars2017, aes(x = Mileage, y = Price,
color = CarType, shape = CarType)) +
geom_point() +
labs(
title = "Mileage vs. Price by Car Type",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)",
color = "Car Type",
shape = "Car Type"
)
Question 1: What do you notice about the three car types in the second plot?
In the second plot, I notice that all the car types are pretty scattered but they show a strong-ish negative association.
accord <- ThreeCars2017 %>%
filter(CarType == "Accord")
ggplot(accord, aes(x = Mileage, y = Price)) +
geom_point(alpha = 0.6) +
labs(
title = "Mileage vs. Price for Used Honda Accords",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
cor(accord$Mileage, accord$Price, use = "complete.obs")
## [1] -0.8489441
Question 2: Describe the association: its direction, linearity, and strength. Does your value of r fit what you see in the plot?
The association for Accords is pretty negative. I’d say the points are pretty distanced so the r is not too strong but strong enough. I see a tiny curve at the end but besides that it seems linear. r is -0.85 so I’d say this was an accurate description.
model <- lm(Price ~ Mileage, data = accord)
coef(model)
## (Intercept) Mileage
## 20.8096129 -0.1198119
Question 3: What are your slope and intercept? Interpret the slope in context, paying attention to the units.
My slope is -0.119 and my intercept is 20.809. So y-hat = -0.119x + 20.809. For each miel increased, the model predicts the price to decrease by -0.119 on average.
ggplot(accord, aes(x = Mileage, y = Price)) +
geom_point(alpha = 0.6) +
geom_abline(intercept = 20.809, slope = -0.119,
color = "steelblue", linewidth = 1) +
labs(
title = "Mileage vs. Price for Used Honda Accords",
subtitle = "yhat = ____ x + ____, r = ____",
x = "Mileage (thousands of miles)",
y = "Price (thousands of dollars)"
)
Question 4: Does your line pass the check of running through the middle of the points?
My line passes through the middle of the points!