This page was generated from notebooks/Poisson_GLM.ipynb. Interactive online version: Binder badge

Poisson GLMΒΆ

[1]:
from spglm.glm import GLM
from spglm.family import Poisson
import libpysal
import numpy
[2]:
# Load sample dataset - columbus dataset
db = libpysal.io.open(libpysal.examples.get_path("columbus.dbf"))

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

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

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

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