2024-10-28

Multiple Linear Regression

  • Multiple Linear Regression is similar to simple linear regression but it can have multiple variables which then contribute to the output.
  • The format for multiple linear regression is \(y = b_0 + b_1x_1 +b_2x_2 + ... + b_nx_n\).
  • \(b_0\) is the y-intercept and all the other b’s are the “weights” assigned to that variable. \(x_1\) to \(x_n\) are the column values.

Exploring the Data

  • The first step for multiple linear regression is exploring the data including head, summary, str etc.
  • After exploring the data and figuring out the information, data visualizations can help to know more about the data.

Exploring the Data 2

Next Steps

  • I explored the rest of the data and those two variables seemed to have the most impact on the price; however, since color is a categorical value I will label encode it so it works with multiple linear regression.
  • After the data can be used to build the model, I will graph it with plotly. Although more than two variables can be used to predict the price, I will only use two to create a plot in 3 dimensions.

Encoding and Building the Model

# Encode the data
diamonds$color_encoded <- as.numeric(factor(diamonds$color))
# Build the model
mlr <- lm(price ~ carat + color_encoded, data = diamonds)

# Predictions
diamonds$predicted_price <- predict(mlr)

A summary of the model is available at the end of the presentation.

Visualization of the model

Statistical methods to improve model

There are multiple ways to improve this model for example: - We can increase the amount of variables without a problem to make it more accurate; however, this has to be done with caution since it is prone to over fitting. - We can check for patterns in the errors to see if it has some kind of bias in some variables, and we can modify it to adjust for these. - Remove outliers in the modifying the data part, we can use standard deviation to remove \(|x| > 3\) since outliers can affect models in a negative way.

Summary of the model

## 
## Call:
## lm(formula = price ~ carat + color_encoded, data = diamonds)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -18831.5   -806.6    -82.2    621.1  12030.6 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   -1576.860     16.684  -94.52   <2e-16 ***
## carat          8013.954     14.204  564.19   <2e-16 ***
## color_encoded  -246.228      3.958  -62.21   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1496 on 53937 degrees of freedom
## Multiple R-squared:  0.8594, Adjusted R-squared:  0.8594 
## F-statistic: 1.649e+05 on 2 and 53937 DF,  p-value: < 2.2e-16