spreg.t_stat

spreg.t_stat(reg, z_stat=False)[source]

Calculates the t-statistics (or z-statistics) and associated p-values. [Gre03]

Parameters:
regregression object

output instance from a regression model

z_statbool

If True run z-stat instead of t-stat

Returns:
ts_resultlist of tuples

each tuple includes value of t statistic (or z statistic) and associated p-value

Examples

We first need to import the needed modules. Numpy is needed to convert the data we read into arrays that spreg understands and pysal to perform all the analysis. The diagnostics module is used for the tests we will show here and the OLS and TSLS are required to run the models on which we will perform the tests.

>>> import numpy as np
>>> import libpysal
>>> from libpysal import examples
>>> import spreg
>>> from spreg import OLS

Open data on Columbus neighborhood crime (49 areas) using libpysal.io.open(). This is the DBF associated with the Columbus shapefile. Note that libpysal.io.open() also reads data in CSV format; since the actual class requires data to be passed in as numpy arrays, the user can read their data in using any method.

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

Before being able to apply the diagnostics, we have to run a model and, for that, we need the input variables. Extract the CRIME column (crime rates) from the DBF file and make it the dependent variable for the regression. Note that PySAL requires this to be an numpy array of shape (n, 1) as opposed to the also common shape of (n, ) that other packages accept.

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

Extract INC (income) and HOVAL (home value) vector from the DBF to be used as independent variables in the regression. Note that PySAL requires this to be an nxj numpy array, where j is the number of independent variables (not including a constant). By default this model adds a vector of ones to the independent variables passed in, but this can be overridden by passing constant=False.

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

Run an OLS regression. Since it is a non-spatial model, all we need is the dependent and the independent variable.

>>> reg = OLS(y,X)

Now we can perform a t-statistic on the model:

>>> testresult = spreg.t_stat(reg)
>>> print("%12.12f"%testresult[0][0], "%12.12f"%testresult[0][1], "%12.12f"%testresult[1][0], "%12.12f"%testresult[1][1], "%12.12f"%testresult[2][0], "%12.12f"%testresult[2][1])
14.490373143689 0.000000000000 -4.780496191297 0.000018289595 -2.654408642718 0.010874504910

We can also use the z-stat. For that, we re-build the model so we consider HOVAL as endogenous, instrument for it using DISCBD and carry out two stage least squares (TSLS) estimation.

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

Once the variables are read as different objects, we are good to run the model.

>>> reg = spreg.TSLS(y, X, yd, q)

With the output of the TSLS regression, we can perform a z-statistic:

>>> testresult = spreg.t_stat(reg, z_stat=True)
>>> print("%12.10f"%testresult[0][0], "%12.10f"%testresult[0][1], "%12.10f"%testresult[1][0], "%12.10f"%testresult[1][1], "%12.10f"%testresult[2][0], "%12.10f"%testresult[2][1])
5.8452644705 0.0000000051 0.3676015668 0.7131703463 -1.9946891308 0.0460767956