[PYTHON] Poisson regression analysis

Poisson regression analysis

Poisson regression analysis is also one of nonlinear regression analysis. The distribution function can be estimated by the maximum likelihood estimation method assuming a Poisson distribution. This analysis method is basically an approach that can be used for those that have "count data" as the dependent variable. Also, the dependent variable must be an integer greater than or equal to 0. The following are typical examples of Poisson regression analysis that can be used. ex) Number of traffic accidents, number of visitors, number of children ... Please note that using it just because the distribution looks like a Poisson distribution will result in an incorrect analysis.

Poisson Regression Analysis Theory

The distribution function of a typical Poisson distribution is as follows. $ \frac{\lambda^s exp(-\lambda)}{s!}$

In Poisson regression analysis, we put the following model. E[y_i|X_i]=exp(X\beta) = \lambda

Next, find $ P (y_i | X_i) $. P(y_i|X_i) = \frac{exp(X\beta)^{y_i} exp[exp(X\beta)]}{y_i!}

Next, find the likelihood function. $L(\beta)=\Pi P(y_i|X_i) $ The maximum likelihood estimate of this cannot be calculated by hand, so let the optimization function calculate it.

Effect of Poisson regression analysis

Calculating the marginal effect of $ E [y_i | X_i] = exp (X \ beta) $ \frac{\partial E[y_i|X_i] }{\partial X} = \beta exp(X\beta)=\beta \bar{y}

Python code

import statsmodels.api as sm
import pandas as pd
#read data
data=pd.read_csv("___.csv")
target=data.loc[:,"name"]
explain=data.loc[:,["names"]]

#it is necessary to add constant variable
explain=sm.add_constant(explain)

model=sm.Poisson(target, explain)
result=model.fit()
#you can get beta but not Partial effect
print(result.summary())

#you can get Partial Effect!!
print(result.get_margeff(at="overall").summary())
#if you change [at="overall"], the way of calculation can be change 

Recommended Posts

Poisson regression analysis
Regression analysis method
Basics of regression analysis
Regression analysis with NumPy
Regression analysis in Python
What is Logistic Regression Analysis?
Multiple regression analysis with Keras
Simple regression analysis in Python
First simple regression analysis in Python
Machine learning algorithm (multiple regression analysis)
Machine learning algorithm (simple regression analysis)
Data analysis for improving POG 3-Regression analysis-
Simple regression analysis implementation in Keras
What is Multinomial Logistic Regression Analysis?
Logistic regression analysis Self-made with python
I tried multiple regression analysis with polynomial regression
Logistic regression
Machine learning with python (2) Simple regression analysis
Logistic regression
Linear regression
Parabolic analysis
Single regression analysis by least squares method
Time series analysis # 6 Spurious regression and cointegration
[Machine learning] Regression analysis using scikit learn
[scikit-learn, matplotlib] Multiple regression analysis and 3D drawing
Creating multiple output models for regression analysis [Beginner]
Easy Lasso regression analysis with Python (no theory)