spreg.koenker_bassett¶
- spreg.koenker_bassett(reg, z=None)[source]¶
Calculates the Koenker-Bassett test statistic to check for heteroscedasticity. [Gre03, KBJ82]
- Parameters:
- reg
regression
output
output from an instance of a regression class
- z
array
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.
- reg
- Returns:
- kb_result
dictionary
contains the statistic (kb), degrees of freedom (df) and the associated p-value (pvalue) for the test.
- kb
float
scalar value for the Koenker-Bassett test statistic.
- df
integer
degrees of freedom associated with the test
- pvalue
float
p-value associated with the statistic (chi^2 distributed)
- kb_result
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 spreg >>> from spreg 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 = spreg.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