spglm.glm.GLM

class spglm.glm.GLM(y, X, family=<spglm.family.Gaussian object>, offset=None, y_fix=None, constant=True)[source]

Generalised linear models. Can currently estimate Guassian, Poisson and Logisitc regression coefficients. GLM object prepares model input and fit method performs estimation which then returns a GLMResults object.

Parameters:
yarray

n*1, dependent variable.

Xarray

n*k, independent variable, exlcuding the constant.

familystr

Model type: ‘Gaussian’, ‘Poisson’, ‘Binomial’

offsetarray

n*1, the offset variable at the ith location. For Poisson model this term is often the size of the population at risk or the expected size of the outcome in spatial epidemiology. Default is None where Ni becomes 1.0 for all locations.

y_fixarray

n*1, the fix intercept value of y

Examples

>>> import libpysal
>>> from spglm.glm import GLM
>>> from spglm import family
>>> db = libpysal.io.open(libpysal.examples.get_path('columbus.dbf'),'r')
>>> y = np.array(db.by_col("HOVAL"))
>>> y = np.reshape(y, (49,1))
>>> X = []
>>> X.append(db.by_col("INC"))
>>> X.append(db.by_col("CRIME"))
>>> X = np.array(X).T
>>> model = GLM(y, X, family=family.Gaussian())
>>> results = model.fit()
>>> results.params
array([46.42818268,  0.62898397, -0.48488854])
Attributes:
yarray

n*1, dependent variable.

Xarray

n*k, independent variable, including constant.

familystr

Model type: ‘Gaussian’, ‘Poisson’, ‘logistic’

ninteger

Number of observations

kinteger

Number of independent variables

df_modelfloat

k-1, where k is the number of variables (including intercept)

df_residualfloat

observations minus variables (n-k)

mean_yfloat

Mean of y

std_yfloat

Standard deviation of y

fit_paramsdict

Parameters passed into fit method to define estimation routine.

__init__(y, X, family=<spglm.family.Gaussian object>, offset=None, y_fix=None, constant=True)[source]

Initialize class

Methods

__init__(y, X[, family, offset, y_fix, constant])

Initialize class

df_model()

df_resid()

fit([ini_betas, tol, max_iter, solve])

Method that fits a model with a particular estimation routine.

Attributes

mean_y

std_y

df_model()[source]
df_resid()[source]
fit(ini_betas=None, tol=1e-06, max_iter=200, solve='iwls')[source]

Method that fits a model with a particular estimation routine.

Parameters:
ini_betasarray

k*1, initial coefficient values, including constant. Default is None, which calculates initial values during estimation.

tol: float

Tolerence for estimation convergence.

max_iterinteger

Maximum number of iterations if convergence not achieved.

solve :string

Technique to solve MLE equations. ‘iwls’ = iteratively (re)weighted least squares (default)