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?
They are scattered in a downward (Negative) looking pattern.
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?
Negative, Linear, MOderate. r fits well.
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.
-0.01198x + 20.809. When milage increases by 1 the price decreases by 0.01198
ggplot(accord, aes(x = Mileage, y = Price)) +
geom_point(alpha = 0.6) +
geom_abline(intercept =20.809, slope = -0.01198,
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?
YES.