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:
- y
array
n*1, dependent variable.
- X
array
n*k, independent variable, exlcuding the constant.
- family
str
Model type: ‘Gaussian’, ‘Poisson’, ‘Binomial’
- offset
array
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_fix
array
n*1, the fix intercept value of y
- 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:
- y
array
n*1, dependent variable.
- X
array
n*k, independent variable, including constant.
- family
str
Model type: ‘Gaussian’, ‘Poisson’, ‘logistic’
- n
integer
Number of observations
- k
integer
Number of independent variables
- df_model
float
k-1, where k is the number of variables (including intercept)
- df_residual
float
observations minus variables (n-k)
- mean_y
float
Mean of y
- std_y
float
Standard deviation of y
- fit_params
dict
Parameters passed into fit method to define estimation routine.
- y
- __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
- 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_betas
array
k*1, initial coefficient values, including constant. Default is None, which calculates initial values during estimation.
- tol: float
Tolerence for estimation convergence.
- max_iter
integer
Maximum number of iterations if convergence not achieved.
- solve :string
Technique to solve MLE equations. ‘iwls’ = iteratively (re)weighted least squares (default)
- ini_betas