Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section 4.2 — Multiple linear regression

Section 4.2 — Multiple linear regression

This notebook contains the code examples from Section 4.2 Multiple linear regression from the No Bullshit Guide to Statistics.

Notebook setup


[notice] A new release of pip is available: 26.2.1 -> 26.2.2
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>
datasets/ directory already exists.

\def\stderr#1{\mathbf{se}_{#1}} \def\stderrhat#1{\hat{\mathbf{se}}_{#1}} \def\Mean{\textbf{mean}} \def\Var{\textbf{var}} \def\Std{\textbf{std}} \def\Freq{\textbf{freq}} \def\RelFreq{\textbf{relfreq}} \def\CI#1{\textbf{CI}_{#1}} \def\CIL#1{\textbf{L}_{#1}} \def\CIU#1{\textbf{U}_{#1}} \def\ci#1{\textbf{ci}_{#1}} \def\cil#1{\textbf{l}_{#1}} \def\ciu#1{\textbf{u}_{#1}} \def\DMeans{\textbf{dmeans}} \def\Prop{\textbf{prop}} \def\DProps{\textbf{dprops}} \def\Err{ {\Large \varepsilon}} \def\calN{\mathcal{N}} \def\Tdist{\mathcal{T}} \def\Expon{\mathrm{Expon}}

(this cell contains the macro definitions like sex‾\stderr{\overline{\mathbf{x}}}, se^\stderrhat{}, mean\Mean, ...)

Definitions

TODO

Doctors dataset

(156, 9)
Loading...

Multiple linear regression model

Y=β0+β1x1+β2x2+⋯+βpxp+ε,Y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \cdots + \beta_p x_p + \Err,

where pp is the number of predictors and ε\Err represents Gaussian noise ε∼N(0,σ)\Err \sim \mathcal{N}(0,\sigma).

Model assumptions

  • (LIN)

  • (INDEPɛ)

  • (NORMɛ)

  • (EQVARɛ)

  • (NOCOL)

Example: linear model for doctors’ sleep scores

We want to know the influence of drinking alcohol, smoking weed, and exercise on sleep score?

Intercept 60.452901 alc -1.800101 weed -1.021552 exrc 1.768289 dtype: float64
<Figure size 500x230 with 1 Axes>

Partial regression plots

Partial regression plot for the predictor alc

Step 1: Obtain the data for the x-axis, residuals of the model alc ~ 1 + others

Step 2: Obtain the data for the y-axis, residuals of the model score ~ 1 + others

Step 3: Fit a linear model for the y-residuals versus the x-residuals.

Step 4: Draw a scatter plot of the residuals and the best-fitting linear model to the residuals.

<Figure size 500x230 with 1 Axes>

The slope parameter for the residuals model is the same as the slope parameter of the alc predictor in the original model lm2.

(-1.8001013152459415, -1.8001013152459397)

Partial regression plot for the predictor weed

<Figure size 500x230 with 1 Axes>

Partial regression plot for the predictor exrc

<Figure size 500x230 with 1 Axes>

(BONUS TOPIC) Partial regression plots using statsmodels

The function plot_partregress defined in statsmodels.graphics.api performs the same steps as the function plot_partreg we used above. but is a little more awkward to use.

When calling the function plot_partregress, you must provide the following arguments:

  • the outcome variable (endog)

  • the predictor you’re interested in (exog_i)

  • the predictors you want to regress out (exog_others)

  • the data frame that contains all these variable (data)

<Figure size 1200x300 with 3 Axes>

The notation ∣X|\textrm{X} you see in the axis labels stands for “given all other predictors,” and is different for each subplot. For example, in the leftmost partial regression plot, the predictor we are focussing on is alc, so the other variables (∣X|\textrm{X}) are weed and exrc.

Plot residuals

<Figure size 500x230 with 1 Axes>
<Figure size 900x250 with 3 Axes>

Model summary table

Loading...

Explanations

Nonlinear terms in linear regression

Example: polynomial regression

298
Intercept 64.784755 age 7.086471 np.square(age) -0.136775 dtype: float64
<Figure size 500x230 with 1 Axes>
<Figure size 500x230 with 1 Axes>

Feature engineering and transformed variables

Bonus example: polynomial regression up to degree 3

Intercept 63.367775 age 7.699216 np.power(age, 2) -0.188982 np.power(age, 3) 0.001165 dtype: float64
<Figure size 500x230 with 1 Axes>

Discussion

Exercises

See the notebook exercises/sec42_multivar_LMs.ipynb.