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

data(ThreeCars2017)

Step 1: Look at all of the cars

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.

Step 2: Focus on one car type

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.

Step 3: Fit the line and read its slope and intercept

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

Step 4: Draw the line yourself

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.