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?

I notice that for all three cars, the higher the mileage the lower the price ranges.

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?

The association is negative, nonlinear, and weak. Since I can not see the line, that makes it weak in strength, and since it is not moving in a line and actually has some curves, it would be nonlinear. So yes, my value of r (-0.849) does fit.

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.

My slope/mileage is : -0.120 My intercept is: 20.810 Every time the mileage increases by one, we would expect the price to decrease by -0.11

Step 4: Draw the line yourself

ggplot(accord, aes(x = Mileage, y = Price)) +
  geom_point(alpha = 0.6) +
  geom_abline(intercept = 20.810, slope = -0.120,
              color = "steelblue", linewidth = 1) +
  labs(
    title    = "Mileage vs. Price for Used Honda Accords",
    subtitle = "yhat = -0.120 x + 20.810,  r = -0.849",
    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!