2024-02-04

Intro to Linear Regression

Linear Regression is a fundamental tool in the tool belt of a data scientist. It provides methods for analyzing and modeling relationships with data sets. In the data science world linear regression is a statistical technique employed to understand the correlation between independent variables and a dependent variable. The primary objective is to create a linear equation that best represents the patterns and trends observed in data.Predictive modeling are wonderful for predicting future outcomes and uncovering hidden patterns and making informed decisions based on historical trends. Data Scientist will often leverage linear regressions to extract meaningful insights from large data, which can identify key factors that can influence specific outcomes; from predicting consumer behavior, optimizing marketing strategies or understanding a products performance. With these capabilities linear regression is a powerful and indispensable tool for Data Scientist.

Basics of Linear Regression

In Linear Regression we have two types of variables:
-Dependent Variable: also known as the response variable this is the variable in which we are trying to predict or explain. Denoted by \(Y\) or \(y\)
-Independent Variable(s): The variables believed to impact the dependent variable. In simple linear regression there is only one independent variable denoted as \(X\) or \(x\), while in multiple linear regression there can be more than one, denoted as \(X_1,X_2,....X_k\) or \(x_1,x_2,...x_k\) where \(k\) is the number or independent variables.

Formulas of Linear Regression

-Simple Linear Regression Model:

\(y=\beta_0 + \beta_1 x+ \varepsilon\)

-Multiple Linear Regression Model:

\(y=\beta_0 + \beta_1 x_1+\beta_2x_2+...\beta_kx_k+ \varepsilon\)

Assumptions of Linear Regression

data(mtcars)
model<- lm(mtcars$disp ~ mtcars$hp)
ggplot(mtcars, aes(x = disp, y = hp)) +
  geom_point(size = 4, color = "red", shape = 17, alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE, color = "lightblue") +
  labs(title = "Linearity ", x = "Displacement", y = "Horsepower", color = "Legend Title")##code displayed for Linearity Assumption


Above we have the R code for the Linearity Assumption using the dataset mtcars.
-The equation used for Linearity is:\(disp=\beta_0+\beta_1hp+\varepsilon\)
-The equation used for Indepence Residuals is : \(y_i=\beta_0 + \beta_1x_{1i}+\beta_2x_{2i}+...\beta_{pi}x_{pi}+ \varepsilon\)

Model Fitting with R

This is a basic example using the same data set in previous slide to exempliy a Model Fitting with R:

Diagnostics

Running the autoplot() function in R will generate these diagnostic plots:

3D Visulization with Plotly

Here we have a 3D wireframe plot to visualize our data:

This wirefram visualizes the relationship between the predictors, horsepower and weight, and the response variable, displacement.

More Assumptions

-Here we have a Q-Q plot used for normal distribution assumption and plot to show homoscedasticity or equal variance of residuals in linear regression:

Conclusion

As a studying Data Scientist, I’ve been learning about these functions in probability but with this assignment I’ve really had a break through and found how all these functions are being put to work and how powerful R is to show and visualize the data. Leveraging basic linear regression, while acknowledging the importance of assumptions to model fitting with predictions and the diagnostic tools to validate assumptions and identify outliers. I have lots to learn and the more I find myself buried in these assignments and come out on top the more I find myself falling in love with the skill set I am building.