2024-02-02

Data Overview

The CarPrice dataset contains various attributes of cars, including their engine size and price. Here’s a glimpse of the data:

car_ID symboling CarName fueltype aspiration doornumber carbody drivewheel enginelocation wheelbase carlength carwidth carheight curbweight enginetype cylindernumber enginesize fuelsystem boreratio stroke compressionratio horsepower peakrpm citympg highwaympg price
1 3 alfa-romero giulia gas std two convertible rwd front 88.6 168.8 64.1 48.8 2548 dohc four 130 mpfi 3.47 2.68 9.0 111 5000 21 27 13495
2 3 alfa-romero stelvio gas std two convertible rwd front 88.6 168.8 64.1 48.8 2548 dohc four 130 mpfi 3.47 2.68 9.0 111 5000 21 27 16500
3 1 alfa-romero Quadrifoglio gas std two hatchback rwd front 94.5 171.2 65.5 52.4 2823 ohcv six 152 mpfi 2.68 3.47 9.0 154 5000 19 26 16500
4 2 audi 100 ls gas std four sedan fwd front 99.8 176.6 66.2 54.3 2337 ohc four 109 mpfi 3.19 3.40 10.0 102 5500 24 30 13950
5 2 audi 100ls gas std four sedan 4wd front 99.4 176.6 66.4 54.3 2824 ohc five 136 mpfi 3.19 3.40 8.0 115 5500 18 22 17450
6 2 audi fox gas std two sedan fwd front 99.8 177.3 66.3 53.1 2507 ohc five 136 mpfi 3.19 3.40 8.5 110 5500 19 25 15250

Simple Linear Regression Model

Simple linear regression aims to model the relationship between two variables by fitting a linear equation to observed data. The equation of a simple linear regression line is:

\[ y = \beta_0 + \beta_1x + \epsilon \]

  • \(y\) is the dependent variable (car price),
  • \(x\) is the independent variable (engine size),
  • \(\beta_0\) is the y-intercept,
  • \(\beta_1\) is the slope of the line,
  • \(\epsilon\) is the error term.

Math Equations

  • The least squares estimates: \[ \beta_1 = \frac{\sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n}(x_i - \bar{x})^2} \] \[ \beta_0 = \bar{y} - \beta_1\bar{x} \]

  • The coefficient of determination (R-squared): \[ R^2 = 1 - \frac{\sum_{i=1}^{n}(y_i - \hat{y}_i)^2}{\sum_{i=1}^{n}(y_i - \bar{y})^2} \]

  • The standard error of the estimate: \[ SE = \sqrt{\frac{\sum_{i=1}^{n}(y_i - \hat{y}_i)^2}{n - 2}} \] ## R Code for Linear Regression

The following R code fits a simple linear regression model predicting car price based on engine size:

## 
## Call:
## lm(formula = price ~ enginesize, data = CarPrice_Assignment)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10664.2  -2225.0   -482.4   1588.0  14271.5 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -8005.446    873.221  -9.168   <2e-16 ***
## enginesize    167.698      6.539  25.645   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3889 on 203 degrees of freedom
## Multiple R-squared:  0.7641, Adjusted R-squared:  0.763 
## F-statistic: 657.6 on 1 and 203 DF,  p-value: < 2.2e-16

R Code for ggplot Regression Plot

Here is the R code used to generate the ggplot for visualizing the relationship between engine size and car price:

library(ggplot2)
ggplot(CarPrice_Assignment, aes(x=enginesize, y=price)) +
  geom_point() +
  geom_smooth(method='lm', color='blue') +
  labs(title='Engine Size vs Price', x='Engine Size', y='Price')

ggplot Regression Plot

This plot visualizes the relationship between engine size and car price:

Diagnostic Plots

Diagnostic plots help evaluate the fit and assumptions of the linear regression model:

R Code for Plotly 3D Plot

library(plotly)
fig <- plot_ly(data = CarPrice_Assignment, x = ~enginesize, 
               y = ~price, z = ~horsepower, 
               type = 'scatter3d', mode = 'markers')
fig

Plotly 3D Plot

Visualizing engine size, price, and another variable in 3D: