Skip to content

Commit

Permalink
Merge pull request #118 from pedrovma/spreg1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
knaaptime authored Sep 19, 2023
2 parents 3d0b8c0 + c74cf34 commit 4ff0b0d
Show file tree
Hide file tree
Showing 28 changed files with 3,710 additions and 1,823 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

- name: install bleeding edge libpysal (only Ubuntu / Python 3.9)
shell: bash -l {0}
run: pip install git+https://github.com/pysal/libpysal.git@master
run: pip install git+https://github.com/pysal/libpysal.git@main
if: matrix.os == 'ubuntu-latest' && contains(matrix.environment-file, 'DEV')

- name: run tests - bash
Expand Down
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
2 changes: 1 addition & 1 deletion spreg/diagnostics_sur.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def surLMlag(n_eq, WS, bigy, bigX, bigE, bigYP, sig, varb):
"""
# Score
Y = np.hstack((bigy[r]) for r in range(n_eq))
Y = np.hstack([bigy[r] for r in range(n_eq)])
WY = WS * Y
EWY = np.dot(bigE.T, WY)
sigi = la.inv(sig)
Expand Down
Loading

0 comments on commit 4ff0b0d

Please sign in to comment.