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?

The maxima has the highest prices out of all the cars, the accords prices come second, and the Mazda6 was the most affordable

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 graph has moderate strength with visable linearity. negative association

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.

slope: -0.1198(expected decrease of this number in y), intercept: 20.80

Step 4: Draw the line yourself

ggplot(accord, aes(x = Mileage, y = Price)) +
  geom_point(alpha = 0.6) +
  geom_abline(intercept = 20.80, slope = -0.1198,
              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! The line passed the check