Introduction to Generalized Linear Models (GLM) with Python

What is a generalized linear model?

A generalized linear model is a general term for statistical models such as linear regression, Poisson regression, and logistic regression that explain the response variable (y) by the explanatory variable (x). More specifically, it is a statistical model determined by probability distributions, linear predictors, and link functions.

Probability distribution

The probability distribution that the response variable follows. The binomial distribution and the Poisson distribution are often used to handle discrete data such as count data. The normal distribution and the gamma distribution are often used to handle continuous data that represents continuous quantities such as stock prices.

Linear predictor

A model expression represented by a linear combination of explanatory variables. You can specify which explanatory variable to use and which interaction term (the term represented by the product of the explanatory variables) to use.

z = β_0 + β_{1}x_{1} + β_{2}x_{2}

Link function

A function that transforms an expression to correspond to a linear predictor. Thanks to the link function, the probability that the value can only take 0 to 1 can also correspond to the linear predictor. The link function to be used is determined to some extent depending on the distribution, so if you want to know more, please refer to the linked book in the reference below the article.

log(y) = β_0 + β_{1}x_{1} + β_{2}x_{2}

Implementation in Python

The generalized linear model can be easily executed using R's glm function. However,

I think there are many people who say that, so I will try it with Python. When I looked for it, I found a module like statsmodels, an Rglm function.

Module installation

$ pip install statsmodels
$ pip install patsy #After importing stats models, I was told that it is necessary, so install it
$ pip install pandas #Install for data processing

Data acquisition and modeling

import statsmodels.api as sm
import pandas as pd

#Read the data in the reference URL below
data3a = pd.read_csv("http://hosho.ees.hokudai.ac.jp/~kubo/stat/iwanamibook/fig/poisson/data3a.csv")
#Create a linear predictor with variable x and constant term
data3a.x_c = sm.add_constant(data3a.x)

#Create a generalized linear model of Poisson distribution for distribution and logarithmic link function for link function
#Logarithmic link function is specified by default for Poisson distribution
model = sm.GLM(data3a.y, data3a.x_c, family=sm.families.Poisson())
result = model.fit()

#result
result.summary()

statsmodels Benri!

in conclusion

It was a generalized linear model that extended statistical models such as linear models, but it is still difficult to incorporate real-life events into such a simple model. The book below also describes techniques such as the generalized linear mixed model, which is an evolution of the generalized linear model, so please refer to it.

reference

http://hosho.ees.hokudai.ac.jp/~kubo/ce/IwanamiBook.html

http://statsmodels.sourceforge.net/devel/glm.html

Recommended Posts

Introduction to Generalized Linear Models (GLM) with Python
Introduction to Statistical Modeling for Data Analysis Generalized Linear Models (GLM)
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
[Introduction to Python] Let's use foreach with Python
[Python] Introduction to CNN with Pytorch MNIST
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Statistical Hypothesis Testing with stats models
[Python] Easy introduction to machine learning with python (SVM)
Introduction to Artificial Intelligence with Python 1 "Genetic Algorithm-Theory-"
Markov Chain Chatbot with Python + Janome (1) Introduction to Janome
Markov Chain Chatbot with Python + Janome (2) Introduction to Markov Chain
Introduction to Artificial Intelligence with Python 2 "Genetic Algorithm-Practice-"
Introduction to Tornado (1): Python web framework started with Tornado
Introduction to Python language
Introduction to OpenCV (python)-(2)
Introduction to formation flight with Tello edu (Python)
Introduction to Python with Atom (on the way)
Introduction to Vector Autoregressive Models (VAR) with stats models
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Introduction to Python] How to iterate with the range function?
[Chapter 5] Introduction to Python with 100 knocks of language processing
Introduction to Mathematics Starting with Python Study Memo Vol.1
Reading Note: An Introduction to Data Analysis with Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
Introduction to Linear Algebra in Python: A = LU Decomposition
Overview of generalized linear models and implementation in Python
Introduction to Vector Error Correcting Models (VECM) with stats models
[Chapter 4] Introduction to Python with 100 knocks of language processing
Connect to BigQuery with Python
Introduction to Python Django (2) Win
Connect to Wikipedia with Python
Post to slack with Python 3
Introduction to RDB with sqlalchemy Ⅰ
Introduction to serial communication [Python]
Switch python to 2.7 with alternatives
Write to csv with Python
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
[Python] Linear regression with scikit-learn
An introduction to Python Programming
Introduction to Python For, While
20200329_Introduction to Data Analysis with Python Second Edition Personal Summary
Introduction to her made with Python ~ Tinder automation project ~ Episode 5
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
[Raspi4; Introduction to Sound] Stable recording of sound input with python ♪
[Introduction to Python] How to get data with the listdir function
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Link to get started with python
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
Nice to meet you with python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Introduction to Generalized Estimates by statsmodels
Try to operate Facebook with Python
Introduction to Python Hands On Part 1
Output to csv file with Python
[Introduction to Python] How to parse JSON