Agenda

  • What is Simple Linear Regression?
  • Model & Interpretation
  • Example Dataset
  • Model Fit in R
  • Visualization (ggplot + plotly)
  • Inference (P-values, CI, Prediction)
  • Takeaways

Simple Linear Regression Formula

\[ Y = \beta_0 + \beta_1 X + \varepsilon \]

Where:
- \(\beta_0\) = intercept
- \(\beta_1\) = slope
- \(\varepsilon\) = error term (noise)

Regression Assumptions

\[ \text{Assumptions: Linearity,Independence,Normality,Equal Variance} \]

  • Linearity: Relationship between X and Y is linear
  • Independence: Observations are independent
  • Normality: Errors are normally distributed
  • Equal Variance: Spread of errors is similar across X values

Our Example Data (mtcars)

car mpg wt hp
Mazda RX4 21.0 2.620 110
Mazda RX4 Wag 21.0 2.875 110
Datsun 710 22.8 2.320 93
Hornet 4 Drive 21.4 3.215 110
Hornet Sportabout 18.7 3.440 175
Valiant 18.1 3.460 105
Duster 360 14.3 3.570 245
Merc 240D 24.4 3.190 62

Scatterplot (ggplot #1)

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

Fit the Regression Model (Code Slide Required)

# Fit simple linear regression model
fit <- lm(mpg ~ wt, data = df)
#Display Summary
summary(fit)
## 
## Call:
## lm(formula = mpg ~ wt, data = df)
## 
## 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

Statistical Significance of the Slope

term estimate std.error statistic p.value conf.low conf.high
wt -5.344 0.559 -9.559 0 -6.486 -4.203

Prediction Example

fit lwr upr
21.25 14.93 27.57

Model Diagnostics (ggplot #2)

3D Visualization (plotly requirement)

Statistical Significance of the Slope

term estimate std.error statistic p.value conf.low conf.high
wt -5.344 0.559 -9.559 0 -6.486 -4.203

Prediction Example

fit lwr upr
21.25 14.93 27.57

Conclusion

  • Weight is negatively related to MPG.
  • The model suggests heavier cars burn more fuel.
  • Visual diagnostics indicate the regression is reasonable.
  • This method can be extended to multiple predictors.