2024-03-22

Introduction to Linear Regression

The link between independent and dependent variables is modeled using linear regression. Important points

  • \(\textbf{Dependent Variable}\): The variable being predicted (\(Y\)).
  • \(\textbf{Independent Variables}\): Predictors of the dependent variable (\(X_i\)).
  • \(\textbf{Linearity}\): Assumes a linear relationship between variables.
  • \(\textbf{Applications}\): Used across various fields for prediction.
  • \(\textbf{Objective}\): To find a linear equation that best describes the data.

This method facilitates clear predictions and insights into variable interactions.

The Math Behind Linear Regression

The equation of a simple linear regression line is \(y = \beta_0 + \beta_1x\), where:

\(y\) is the dependent variable, \(x\) is the independent variable, \(\beta_0\) is the y-intercept, \(\beta_1\) is the slope.

Dataset Overview

Now let’s examine some of the {mtcars} dataset’s most important statistics that relate to our linear regression study.

## $summary
##       mpg              wt       
##  Min.   :10.40   Min.   :1.513  
##  1st Qu.:15.43   1st Qu.:2.581  
##  Median :19.20   Median :3.325  
##  Mean   :20.09   Mean   :3.217  
##  3rd Qu.:22.80   3rd Qu.:3.610  
##  Max.   :33.90   Max.   :5.424  
## 
## $range
##      mpg    wt
## min 10.4 1.513
## max 33.9 5.424

Simple Linear Regression in R

Here’s how to use the lm function in R to carry out a basic linear regression. We model the relationship between the weight (wt) and fuel efficiency (mpg) of an automobile.

linearModel <- lm(mpg ~ wt, data=mtcars)
coef_summary <- summary(linearModel)$coefficients
r_squared <- summary(linearModel)$r.squared
list(Coefficients = coef_summary, R_squared = r_squared)
## $Coefficients
##              Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 37.285126   1.877627 19.857575 8.241799e-19
## wt          -5.344472   0.559101 -9.559044 1.293959e-10
## 
## $R_squared
## [1] 0.7528328

Plotting the Regression Line

Understanding the relationship between the variables is aided by seeing the linear regression model.

## `geom_smooth()` using formula = 'y ~ x'

Residuals Analysis

It is essential to analyze residuals in order to verify the linear regression’s assumptions.

3D Plot with Plotly

We can utilize Plotly to generate a 3D scatter plot for a more dynamic visualization.

Conclusion

An effective method for comprehending and forecasting relationships between variables is simple linear regression. R and its libraries, such as ggplot2 and plotly, allow us to visualize and analyze these interactions in an efficient manner.

Thank you