2024-06-05

What will I be doing?

I will be using a “Used Cars” dataset, which I have some experience with from a previous class. I plan to perform a few basic linear regressions in order to determine some possible predictors of a used car’s price.

Data: https://bitbucket.org/remcc/rob-data-sets/downloads/susedcars.csv

(Warning: the file will automatically download when clicking this link!)

Dataset Overview

  • Price

    • Continuous variable, ranging from $995-79995.
  • Trim

    • Categorical variable. Given as 430, 500, 550, or other.
  • IsOneOwner

    • Boolean variable. Given as “t” or “f”.
  • Mileage

    • Continuous numerical variable. Ranges from 1997-255419 miles.

Dataset Overview (cont.)

  • Year

    • Continuous numerical variable. Ranges from 1994-2013.
  • Color

    • Categorical variable. Given as Black, Silver, White, or Other.
  • Displacement

    • Categorical variable. Given as 4.6 L, 5.5 L, or Other.

We will attempt to use Mileage and Year to predict the Price of a used car.

Linear Regression Overview

Recall that a linear regression follows the formula \(y_0 = \beta_0 + \beta_1x\).

  • \(y_0\) represents the response variable, or the variable that we are attempting to model. In our case, we will be modeling price.

  • \(\beta_0\) corresponds to the y-intercept, or in other words the value of the response variable when \(x\) is 0.

  • \(\beta_1\) represents the slope, or otherwise the increase in the response variable per unit increase in \(x\).

Setup

cars = read.csv(url("https://bitbucket.org/remcc/rob-data-sets/downloads/susedcars.csv"))
head(cars)
##   price trim isOneOwner mileage year  color displacement
## 1 43995  550          f   36858 2008 Silver          5.5
## 2 44995  550          f   46883 2012  Black          4.6
## 3 25999  550          f  108759 2007  White          5.5
## 4 33880  550          f   35187 2007  Black          5.5
## 5 34895  550          f   48153 2007  Black          5.5
## 6  5995  500          f  121748 2002  other        other

(Here’s a preview of what the data looks like!)

Mileage vs. Price

mileage_model = lm(price ~ mileage, data = cars)
summary(mileage_model)
## 
## Call:
## lm(formula = price ~ mileage, data = cars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -32670  -7063    239   6293  37024 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  5.636e+04  6.706e+02   84.04   <2e-16 ***
## mileage     -3.500e-01  7.870e-03  -44.47   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10670 on 998 degrees of freedom
## Multiple R-squared:  0.6646, Adjusted R-squared:  0.6643 
## F-statistic:  1978 on 1 and 998 DF,  p-value: < 2.2e-16

This means that \(\beta_0 = 56300\) and \(\beta_1 = -0.35\).

We see that our \(\beta_0 = 56300\) and our \(\beta_1 = -0.35\).

We can also find that \(R^2 = 0.6646\), meaning that there is a moderate negative correlation.

In other words, the greater the mileage, the lower the price!

Mileage vs. Price Graph

plot_mileage = ggplot(cars, aes(x = mileage, y = price)) + geom_point()
plot_mileage + geom_smooth(method = 'lm')
## `geom_smooth()` using formula = 'y ~ x'

We can see that this matches our given regression equation, \(\text{Price} = 56300 - 0.33 \cdot \text{Mileage}\). Positive y-intercept, negative correlation.

Year vs. Price

year_model = lm(price ~ year, data = cars)
summary(year_model)
## 
## Call:
## lm(formula = price ~ year, data = cars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -27849  -5821  -1695   4099  40211 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -7.726e+06  1.322e+05  -58.46   <2e-16 ***
## year         3.865e+03  6.585e+01   58.69   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8731 on 998 degrees of freedom
## Multiple R-squared:  0.7753, Adjusted R-squared:  0.7751 
## F-statistic:  3444 on 1 and 998 DF,  p-value: < 2.2e-16

We find that \(\beta_0 = -772600\) and \(\beta_1 = 3865\).

It seems that year is a slightly stronger predictor than mileage, with \(R^2 = 0.7753\) indicating that they are more closely correlated.

It is also worth noting the range of the “year” variable here, which ranges from 1994 to 2013. This explains the seemingly outrageous \(\beta_0\), which would be the expected price when year = 0.

Year vs. Price Plots

plot_year = ggplot(cars, aes(x = year, y = price)) + geom_point()
plot_year + geom_smooth(method = 'lm')
## `geom_smooth()` using formula = 'y ~ x'

We can see that this matches our given regression equation, \(\text{Price} = -772600 + 3865 \cdot \text{Year}\). Relatively strong positive correlation between year and price!

One more note about year

Luckily for us, ggplot also automatically changes the margins, and doesn’t include the large gap on the x-axis between the datapoints and x = 0.

However, what if we wanted to see this gap?

so, here is an additional plotly figure that allows you to scale the axes interactively!