Agenda

  • Multiple linear regression
  • Mathematical formulation
  • Apply the model to mtcars
  • Interpretation
  • Diagnostics
  • 3D Plotly visualization
  • Takeaways

The model (LaTeX)

\[ mpg = \beta_0 + \beta_1 wt + \beta_2 hp + \varepsilon,\qquad \varepsilon \sim N(0,\sigma^2) \]

Matrix form:

\[ \mathbf{y}=\mathbf{X}\boldsymbol{\beta}+\boldsymbol{\varepsilon} \]

Hypothesis tests

\[ H_0:\beta_j=0,\qquad H_1:\beta_j\neq 0 \]

\[ t_j=\frac{\hat{\beta}_j}{SE(\hat{\beta}_j)},\qquad \hat{\beta}_j \pm t_{1-\alpha/2} SE(\hat{\beta}_j) \]

Data preview

##                    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

MPG vs Weight

MPG vs Horsepower

Model fit output

## 
## Call:
## lm(formula = mpg ~ wt + hp, data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.941 -1.600 -0.182  1.050  5.854 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 37.22727    1.59879  23.285  < 2e-16 ***
## wt          -3.87783    0.63273  -6.129 1.12e-06 ***
## hp          -0.03177    0.00903  -3.519  0.00145 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared:  0.8268, Adjusted R-squared:  0.8148 
## F-statistic: 69.21 on 2 and 29 DF,  p-value: 9.109e-12

Coefficient table

## # A tibble: 3 × 5
##   term        estimate conf.low conf.high  p.value
##   <chr>          <dbl>    <dbl>     <dbl>    <dbl>
## 1 (Intercept)  37.2     34.0      40.5    2.57e-20
## 2 wt           -3.88    -5.17     -2.58   1.12e- 6
## 3 hp           -0.0318  -0.0502   -0.0133 1.45e- 3

Interpretation

  • Weight (wt): Expected change in mpg per +1000 lbs, keeping horsepower fixed
  • Horsepower (hp): Expected change in mpg per +1 hp, keeping weight fixed

Residuals vs Fitted

Normal Q–Q Plot

3D Plotly Visualization

Takeaways

  • MLR estimates effects while controlling for other predictors
  • Diagnostics help validate assumptions
  • 3D views help understand interactions