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.6 — Generalized linear models

This notebook contains the code examples from Section 4.6 Generalized linear models from the No Bullshit Guide to Statistics.

Notebook setup
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>
datasets/ directory present and ready.

Definitions

Odds
(1.0, 9.000000000000002, 0.25)
Log-odds
(0.0, 2.1972245773362196, -1.3862943611198906)
The logit function
(0.0, 2.1972245773362196, -1.3862943611198906)
<Figure size 500x300 with 1 Axes>
The logistic function
(0.5, 0.8807970779778823, 0.11920292202211755)
<Figure size 500x300 with 1 Axes>
The logistic and logit functions are inverses
0.2
3.000000000000003

Logistic regression

TODO FORMULA

<Figure size 500x300 with 1 Axes>
0.0024726231566347743
0.9999546021312976

Example 1: hiring student interns

(100, 2)
Loading...
Optimization terminated successfully.
         Current function value: 0.138101
         Iterations 10
Intercept   -78.693205
work          1.981458
dtype: float64
Loading...
<Figure size 500x300 with 1 Axes>
<Figure size 500x300 with 1 Axes>
Loading...

Interpreting the model parameters

Parameters as changes in the log-odds
1.9814577697476687
Parameters as ratios of odds
7.253308936265722
Differences in probabilities

What is the marginal effect of the predictor work for an intern who invests 40 hours of effort?

Loading...
Loading...

Prediction

0.9893134055105761

Poisson regression

<Figure size 500x300 with 1 Axes>

Example 2: hard disk failures over time

Loading...
Optimization terminated successfully.
         Current function value: 2.693129
         Iterations 6
Intercept 1.075999 age 0.193828 dtype: float64
Loading...
<Figure size 500x300 with 1 Axes>
Loading...
Interpreting the model parameters
Log-counts
0.1938278482145407
Incidence rate ratio (IRR)
1.213887292102993
Marginal effect

What is the marginal effect of the predictor age for a 10 year old hard disk installation?

Loading...
<Figure size 500x300 with 1 Axes>
Predictions
20.374365915173986
(13.0, 28.0)

Explanations

The exponential family of distributions

  • exponential

  • Gaussian (normal)

  • Poisson

  • Binomial

The generalized linear model template

  • choose

Generalized linear models using statsmodels

Linear model
Intercept 32.465809 effort 4.504850 dtype: float64
Logistic regression
Intercept -78.693205 work 1.981458 dtype: float64
Poisson regression
Intercept 1.075999 age 0.193828 dtype: float64

Fitting generalized linear models

Discussion

Model diagnostics and validation

Dispersion: 0.9869289289681199

Logistic regression as a building blocks for neural networks

The operation of the perceptron, which is the basic building block of neural networks, is essentially the same as linear regression model:

  • constant intercept (bias term)

  • linear combination of inputs

  • nonlinear function used to force the output to be between 0 and 1

Limitations of GLMs

  • GLMs assume observations are independent

  • Assumes distribution M\mathcal{M} is one of the exponential family

  • Outliers can be problematic

  • Interpretability

Exercises

Exercise 1: probabilities to odds and log-odds

(0.4285714285714286, 98.99999999999991, 2.333333333333333)
(-0.8472978603872037, 4.595119850134589, 0.8472978603872034)

Exercise 2: log-odds to probabilities

(0.2689414213699951, 0.7310585786300049, 0.8807970779778823)

Exercise 3: students pass or fail

a) Load the dataset students.csv and add a column passing that contains 1 or 0, based on the above threshold score of 70.

b) Fit a logistic regression model for passing based on effort variable.

Optimization terminated successfully.
         Current function value: 0.276583
         Iterations 8
Intercept -16.257302 effort 2.047882 dtype: float64
<Figure size 500x300 with 1 Axes>

c) Use your model to predict the probability of a new student with effort 10 will pass.

0 0.985536 dtype: float64
0.9855358765361845

Exercise 4: titanic survival data

Fit a logistic regression model that calculates the probability of survival for people who were on the Titanic, based on the data in datasets/exercises/titanic.csv. Use the variables age, sex, and pclass as predictors.

cf. Titanic_Logistic_Regression.ipynb

Optimization terminated successfully.
         Current function value: 0.453279
         Iterations 6
Intercept 3.777013 C(sex)[T.M] -2.522781 C(pclass)[T.2] -1.309799 C(pclass)[T.3] -2.580625 age -0.036985 dtype: float64

Use your logistic regression model to estimate the probability of survival for a 30 year old female traveling in second class.

0 0.795378 dtype: float64

Exercise 5: asthma attacks

Fit a Poisson regression model to the datasets/exercises/asthma.csv dataset.

data source data/asthma.csv

Loading...
Optimization terminated successfully.
         Current function value: 1.707281
         Iterations 6
Intercept -0.315387 C(gender)[T.male] -0.041905 C(res_inf)[T.yes] 0.426431 ghq12 0.049508 dtype: float64

Exercise 6: student admissions dataset

The dataset datasets/exercises/binary.csv contains information about the acceptance decision for 400 students to a prestigious school. Try to fit a logistic regression model for the variable admit using the variables gre, gpa, and rank as predictors.

Loading...
Optimization terminated successfully.
         Current function value: 0.573147
         Iterations 6
Intercept -3.989979 C(rank)[T.2] -0.675443 C(rank)[T.3] -1.340204 C(rank)[T.4] -1.551464 gre 0.002264 gpa 0.804038 dtype: float64

The above model uses the rank=1 as the reference category an the log odds reported are with respect to this catrgory

log⁡p(accept∣rank=1)/log⁡p(accept∣rank=2)=C(rank)rank[T.2]=−0.675443\log p(accept|rank=1) / \log p(accept|rank=2) = \texttt{C(rank)rank[T.2]} = -0.675443

etc. for others rank[T.3] -1.340204 rank[T.4] -1.551464

See LogisticRegressionChangeOfReferenceCategoricalValue.ipynb for exercise recodign relative to different refrence level.

TODO

CUT MATERIAL

Bonus Exercises

Loading...
Constant model
Optimization terminated successfully.
         Current function value: 0.556775
         Iterations 5
Intercept -1.12546 dtype: float64
0.24500000000000005
hon 0 0.755 1 0.245 Name: proportion, dtype: float64
Using only a categorical variable
Optimization terminated successfully.
         Current function value: 0.549016
         Iterations 5
Intercept -1.470852 female 0.592782 dtype: float64
Loading...
(0.18681318681318684, 0.18681318681318682)
(0.2297297297297298, 0.22972972972972974)
(-1.4708517491479534, -1.4708517491479536)
(0.29357798165137605, 0.29357798165137616)
(0.41558441558441545, 0.4155844155844156)
(-0.8780695190539576, -0.8780695190539572)
1.8090145148968668
Logistic regression with a single continuous predictor variable
Optimization terminated successfully.
         Current function value: 0.417683
         Iterations 7
Intercept -9.793942 math 0.156340 dtype: float64

So the model equation is

log⁡(p/(1−p))=logit(p)=−9.793942+.1563404⋅math\log(p/(1-p)) = \texttt{logit}(p) = -9.793942 + .1563404 \cdot \texttt{math}
(0.15634035558592174, 0.15634035558592269)

We can say now that the coefficient for math is the difference in the log odds. In other words, for a one-unit increase in the math score, the expected change in log odds is .1563404.

(1.1692240873242834, 1.1692240873242823)

So we can say for a one-unit increase in math score, we expect to see about 17% increase in the odds of being in an honors class. This 17% of increase does not depend on the value that math is held at.

Logistic regression with multiple predictor variables and no interaction terms
Optimization terminated successfully.
         Current function value: 0.390424
         Iterations 7
Intercept -11.770246 math 0.122959 female 0.979948 read 0.059063 dtype: float64
Logistic regression with an interaction term of two predictor variables
Optimization terminated successfully.
         Current function value: 0.399417
         Iterations 7
Intercept -8.745841 math 0.129378 female -2.899863 femalexmath 0.066995 dtype: float64

Bonus exercise: LA high schools (NOT A VERY GOOD FIT FOR POISSON MODEL)

Dataset info: http://www.philender.com/courses/intro/assign/data.html

This dataset consists of data from computer exercises collected from two high school in the Los Angeles area.

http://www.philender.com/courses/intro/code.html

Loading...
Optimization terminated successfully.
         Current function value: 4.898642
         Iterations 5
Intercept 2.687666 C(gender)[T.M] -0.400921 mathnce -0.003523 langnce -0.012152 dtype: float64
C(gender)[T.M] 0.669703 mathnce 0.996483 langnce 0.987921 dtype: float64
0 0.609079 1 0.736361 Name: C(gender)[T.M], dtype: float64
Optimization terminated successfully.
         Current function value: 4.898642
         Iterations 5
Intercept 2.286745 C(gender, Treatment(1))[T.F] 0.400921 mathnce -0.003523 langnce -0.012152 dtype: float64