spreg.diagnostics.koenker_bassett

spreg.diagnostics.koenker_bassett(reg, z=None)[source]

Calculates the Koenker-Bassett test statistic to check for heteroscedasticity. [KBJ82][Gre03]

Parameters
regregression output

output from an instance of a regression class

zarray

optional input for specifying an alternative set of variables (Z) to explain the observed variance. By default this is a matrix of the squared explanatory variables (X**2) with a constant added to the first column if not already present. In the default case, the explanatory variables are squared to eliminate negative values.

Returns
kb_resultdictionary

contains the statistic (kb), degrees of freedom (df) and the associated p-value (pvalue) for the test.

kbfloat

scalar value for the Koenker-Bassett test statistic.

dfinteger

degrees of freedom associated with the test

pvaluefloat

p-value associated with the statistic (chi^2 distributed)

Notes

x attribute in the reg object must have a constant term included. This is standard for spreg.OLS so no testing done to confirm constant.

Examples

>>> import numpy as np
>>> import libpysal
>>> from libpysal import examples
>>> import diagnostics
>>> from ols import OLS

Read the DBF associated with the Columbus data.

>>> db = libpysal.io.open(examples.get_path("columbus.dbf"),"r")

Create the dependent variable vector.

>>> y = np.array(db.by_col("CRIME"))
>>> y = np.reshape(y, (49,1))

Create the matrix of independent variables.

>>> X = []
>>> X.append(db.by_col("INC"))
>>> X.append(db.by_col("HOVAL"))
>>> X = np.array(X).T

Run an OLS regression.

>>> reg = OLS(y,X)

Calculate the Koenker-Bassett test for heteroscedasticity.

>>> testresult = diagnostics.koenker_bassett(reg)

Print the degrees of freedom for the test.

>>> testresult['df']
2

Print the test statistic.

>>> print("%1.3f"%testresult['kb'])
5.694

Print the associated p-value.

>>> print("%1.4f"%testresult['pvalue'])
0.0580