Skip to content

Commit

Permalink
Several changes
Browse files Browse the repository at this point in the history
This commit incorporates several developments into spreg.

Main changes:
- Refactoring of the output. The way the results from the models are printed to the users has been completely refactored. The new function is simpler and generic. It is based on pandas to create an 'output' attribute to the regression object that contains the most important information on the estimates from the model.
- Addition of slx_lags to all cross-section models. An attribute slx_lags was added to all cross-section models to allow for the direct estimation of SLX models.
- Creation of wrapper functions for the error models. Both error_sp and error_sp_regimes now have wrapper functions that allow for easy estimation of error models based on the kp98, hom or het estimators. The wrapper functions also allow specifications with extra endogenous variables or spatial lag of the dependent variable.
- Several minor updates of fixes were also developed in this commit.

Despite the major changes, this commit does not break any previous code. This update was developed in a private branch by Luc Anselin and Pedro Amaral.
  • Loading branch information
pedrovma committed Sep 13, 2023
1 parent bc55210 commit 8a82e2a
Show file tree
Hide file tree
Showing 21 changed files with 3,063 additions and 794 deletions.
44 changes: 37 additions & 7 deletions spreg/diagnostics_sp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


class LMtests:

"""
Lagrange Multiplier tests. Implemented as presented in :cite:`Anselin1996a`
Expand Down Expand Up @@ -142,7 +141,6 @@ def __init__(self, ols, w, tests=["all"]):


class MoranRes:

"""
Moran's I for spatial autocorrelation in residuals from OLS regression
Expand Down Expand Up @@ -396,7 +394,6 @@ def __init__(self, iv, w, case="nosp"):


class spDcache:

"""
Helper class to compute reusable pieces in the spatial diagnostics module
...
Expand Down Expand Up @@ -669,7 +666,7 @@ def get_vI(ols, w, ei, spDcache):
B = spDcache.AB[1]
trB = np.sum(B.diagonal()) * 4.0
vi = (w.n ** 2 / (w.s0 ** 2 * (w.n - ols.k) * (w.n - ols.k + 2.0))) * (
w.s1 + 2.0 * trA2 - trB - ((2.0 * (spDcache.trA ** 2)) / (w.n - ols.k))
w.s1 + 2.0 * trA2 - trB - ((2.0 * (spDcache.trA ** 2)) / (w.n - ols.k))
)
return vi

Expand Down Expand Up @@ -716,9 +713,6 @@ def akTest(iv, w, spDcache):
p : float
P-value of the test
ToDo:
* Code in as Nancy
* Compare both
"""
mi = get_mI(iv, w, spDcache)
# Phi2
Expand All @@ -731,6 +725,42 @@ def akTest(iv, w, spDcache):
return (mi, ak[0][0], pval[0][0])


def comfac_test(lambd, beta, gamma, vm):
"""
Computes the Spatial Common Factor Hypothesis test as shown in Anselin (1988, p. 226-229)
Parameters
----------
lambd : float
Spatial autoregressive coefficient (as in lambd*Wy)
beta : array
Coefficients of the exogenous (not spatially lagged) variables, without the constant (as in X*beta)
gamma : array
coefficients of the spatially lagged exogenous variables (as in WX*gamma)
vm : array
Variance-covariance matrix of the coefficients
Obs. Needs to match the order of theta' = [beta', gamma', lambda]
Returns
-------
W : float
Wald statistic
pvalue : float
P value for Wald statistic calculated as a Chi sq. distribution
with k-1 degrees of freedom
"""
g = lambd * beta + gamma
G = np.vstack((lambd * np.eye(beta.shape[0]), np.eye(beta.shape[0]), beta.T))

GVGi = la.inv(np.dot(G.T, np.dot(vm, G)))
W = np.dot(g.T, np.dot(GVGi, g))[0][0]
df = G.shape[1]
pvalue = chisqprob(W, df)
return W, pvalue


def _test():
import doctest

Expand Down
Loading

0 comments on commit 8a82e2a

Please sign in to comment.