Search
Poisson_GLM
from spglm.glm import GLM
from spglm.family import Poisson
import libpysal.api as ps
import numpy as np
#Load sample dataset - columbus dataset 
db = ps.open(ps.get_path('columbus.dbf'),'r')

#Set dependent variable
y = np.array(db.by_col("HOVAL"))
y = np.reshape(y, (49,1))
#Round dependent variable and convert to integer for the example since Poisson is for discrete data
y = np.round(y).astype(int)

#Set indepdent varibLES
X = []
X.append(db.by_col("INC"))
X.append(db.by_col("CRIME"))
X = np.array(X).T
#Estimate Poisson GLM

#First instantiate a GLM model object
model = GLM(y, X, family=Poisson()) #Set family to Poisson family object for Poisson GLM

#Then use the fit method to estimate coefficients and compute diagnostics
results = model.fit()
#Estimated prameters, intercept is always the first column on the left
print(results.params)
[ 3.92159085  0.01183491 -0.01371397]
#Parameter standard errors
print(results.bse)
[0.13049161 0.00511599 0.00193769]
#Parameter t-values
print(results.tvalues)
[30.0524361   2.31331634 -7.07748998]
#Model AIC
print(results.aic)
500.8518417993879