Introduction

  • Simple linear regression helps us understand the relationship between a predictor \(x\) and a response \(y\)
  • It assumes a linear relationship
  • We use least squares to estimate the coefficient

The Math!!!

\[ \hat{y} = \hat{\beta_0} + \hat{\beta_1} x \]

where: \[ \hat{\beta_1} = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2} \]

and

\[ \hat{\beta_0} = \bar{y} - \hat{\beta_1}\bar{x} \]

Explaining the Math

-> \(y\): Dependent variable — Miles per Gallon (mpg)

-> \(x\): Independent variable — Car weight (wt) (in 1000 lbs)

-> \(\beta_0\): Intercept — predicted value of \(y\) when \(x = 0\)

-> \(\beta_1\): Slope — change in \(y\) for a one-unit increase in \(x\)

-> \(\varepsilon\): Random error term representing unexplained variation

-> \(\hat{y} = \hat{\beta_0} + \hat{\beta_1}x\): Predicted regression line -> \(\hat{\beta_1}\): Slope estimator -> \(\hat{\beta_0}\): Intercept estimator

Example Dataset

-> For this assignment, I chose the mtcars dataset as an example

model <- lm(mpg ~ wt, data = mtcars)
summary(model)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

-> x = w*t (car weight) -> y = miles per gallon

Scatter Plot with Regression Line

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

Residual Plot (ggplot)

3D Plot (Interactive!) (plotly)

Conclusion

-> The regression line helps us predict MPG based on car weight.

-> The slope tells us that heavier cars tend to have a lower fuel efficiency.

-> \(R^2\) indicates how well the line fits the data.

-> Applications: engineering, transportation, and energy efficiency :D