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.4 — Regression with categorical predictors

This notebook contains the code examples from Section 4.4 Regression with categorical predictors 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.

Definitions

Design matrix for linear model lm1

array([[ 1. , 10.96], [ 1. , 8.69], [ 1. , 8.6 ]])
array([10.96, 8.69, 8.6 ])

Design matrix for linear model lm2

array([[ 1. , 0. , 5. , 0. ], [ 1. , 20. , 0. , 4.5], [ 1. , 1. , 0. , 7. ]])
array([[ 0. , 5. , 0. ], [20. , 0. , 4.5], [ 1. , 0. , 7. ]])

Example 1: binary predictor variable

Intercept 52.956522 C(loc)[T.urb] -6.992885 dtype: float64
<Figure size 460x220 with 1 Axes>
(52.95652173913044, 45.96363636363636, -6.992885375494076)

Encoding

0 rur 1 urb 2 urb 3 urb 4 rur Name: loc, dtype: str
array([[1., 0.], [1., 1.], [1., 1.], [1., 1.], [1., 0.]])

Dummy coding for categorical predictors

Loading...
DesignMatrix with shape (4, 3) Intercept C(cat)[T.B] C(cat)[T.C] 1 0 0 1 1 0 1 0 1 1 0 1 Terms: 'Intercept' (column 0) 'C(cat)' (columns 1:3)

Example 2: predictors with three levels

0 hos 1 cli 2 hos 3 eld 4 cli Name: work, dtype: str
array([[1., 0., 1.], [1., 0., 0.], [1., 0., 1.], [1., 1., 0.], [1., 0., 0.]])
Intercept 46.545455 C(work)[T.eld] 4.569930 C(work)[T.hos] 2.668831 dtype: float64
Visualization of the estimated parameters of the model lmw.
0.0077217625749193
(0.5953116925291129, 0.5526627461285684)

Example 3: improved model for the sleep scores

We can mix of numerical and categorical predictors

Intercept 63.606961 C(loc)[T.urb] -5.002404 alc -1.784915 weed -0.840668 exrc 1.783107 dtype: float64
(0.8544615790287664, 1092.5985552344712)
<Figure size 500x160 with 1 Axes>
Loading...

Compare to the model without loc predictor

(12.758115596295502, 0.00047598123084921284)

Everything is a linear model

One-sample t-test as a linear model

1003.8335
(3.087703149420272, 0.0037056653503329644)
Intercept 3.8335 dtype: float64
(3.0877031494203044, 0.0037056653503326456)

Two-sample t-test as a linear model

East vs. West electricity prices

3.000000000000001

Two-sample t-test with pooled variance

(5.022875513276465, 0.00012497067987678515)
[1.9572405258731593, 4.042759474126842]

Linear model approach

Intercept         6.155556
C(loc)[T.West]    3.000000
dtype: float64
(5.022875513276466, 0.00012497067987678472)
array([1.95724053, 4.04275947])
<Figure size 420x310 with 1 Axes>

Example 1 (revisited): urban vs. rural doctors

(-1.9657612140164198, 0.051124603539793693)
(-1.9657612140164178, 0.05112460353979393)

One-way ANOVA as a linear model

(0.5953116925291181, 0.5526627461285655)
Intercept         46.545455
C(work)[T.eld]     4.569930
C(work)[T.hos]     2.668831
dtype: float64
(0.5953116925291129, 0.5526627461285684)
<Figure size 500x300 with 1 Axes>

Nonparametric tests

One-sample Wilcoxon signed-rank test

0.002770629538645153
0.002284150845974416

Mann-Whitney U-test

0.05008336985073764
0.04953388746998925

Kruskal-Wallis analysis of variance by ranks

0.4441051932875236
0.4468887214966093

Explanations

Dummy coding options

DesignMatrix with shape (4, 3) Intercept cat[T.B] cat[T.C] 1 0 0 1 1 0 1 0 1 1 0 1 Terms: 'Intercept' (column 0) 'cat' (columns 1:3)
DesignMatrix with shape (4, 3) Intercept C(cat)[T.B] C(cat)[T.C] 1 0 0 1 1 0 1 0 1 1 0 1 Terms: 'Intercept' (column 0) 'C(cat)' (columns 1:3)
DesignMatrix with shape (4, 3) Intercept C(cat, Treatment)[T.B] C(cat, Treatment)[T.C] 1 0 0 1 1 0 1 0 1 1 0 1 Terms: 'Intercept' (column 0) 'C(cat, Treatment)' (columns 1:3)
DesignMatrix with shape (4, 3) Intercept C(cat, Treatment('B'))[T.A] C(cat, Treatment('B'))[T.C] 1 1 0 1 0 0 1 0 1 1 0 1 Terms: 'Intercept' (column 0) "C(cat, Treatment('B'))" (columns 1:3)

Avoiding perfect collinearity

Intercept 36.718781 iscli 9.826673 iseld 14.396603 ishos 12.495504 dtype: float64
4496839867074116.0
Intercept 60.452901 alc -1.800101 weed -1.021552 exrc 1.768289 dtype: float64

Discussion

Other coding strategies for categorical variables

DesignMatrix with shape (4, 3) C(cat)[A] C(cat)[B] C(cat)[C] 1 0 0 0 1 0 0 0 1 0 0 1 Terms: 'C(cat)' (columns 0:3)
DesignMatrix with shape (4, 3) Intercept C(cat, Sum)[S.A] C(cat, Sum)[S.B] 1 1 0 1 0 1 1 -1 -1 1 -1 -1 Terms: 'Intercept' (column 0) 'C(cat, Sum)' (columns 1:3)
DesignMatrix with shape (4, 3) Intercept C(cat, Diff)[D.A] C(cat, Diff)[D.B] 1 -0.66667 -0.33333 1 0.33333 -0.33333 1 0.33333 0.66667 1 0.33333 0.66667 Terms: 'Intercept' (column 0) 'C(cat, Diff)' (columns 1:3)
DesignMatrix with shape (4, 3) Intercept C(cat, Helmert)[H.B] C(cat, Helmert)[H.C] 1 -1 -1 1 1 -1 1 0 2 1 0 2 Terms: 'Intercept' (column 0) 'C(cat, Helmert)' (columns 1:3)

Exercises

EXX: redo comparison of debate and lectures scores

(-1.7197867420465645, 0.10917234443214427)

EXX: model comparison

(1.5158185269523023, 0.22299549360240414, 2.0)

The result is non-significant which means including the predictor C(work) in the model is not useful.

EXX: run ANOVA test

107.22963883851017 3.091116115443279e-27
(107.22963883851006, 3.091116115443411e-27)
<Figure size 800x600 with 1 Axes>