Skip to content

Commit

Permalink
Merge pull request #158 from pedrovma/main
Browse files Browse the repository at this point in the history
Updating spreg to 1.7
  • Loading branch information
knaaptime authored Sep 24, 2024
2 parents f50f0b1 + f498b51 commit 8c19228
Show file tree
Hide file tree
Showing 28 changed files with 2,442 additions and 1,311 deletions.
1 change: 0 additions & 1 deletion .github/workflows/unittests.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
matrix:
os: [ubuntu-latest]
environment-file:
- ci/39-latest.yaml
- ci/310-min.yaml
- ci/310-latest.yaml
- ci/311-latest.yaml
Expand Down
2 changes: 1 addition & 1 deletion ci/310-latest.yaml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels:
dependencies:
- python=3.10
# required
- libpysal
- libpysal>=4.12.1
- numpy>=1.3
- pandas>=1.0
- scipy>=0.11
Expand Down
2 changes: 1 addition & 1 deletion ci/310-min.yaml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels:
dependencies:
- python=3.10
# required
- libpysal
- libpysal>=4.12.1
- numpy>=1.3
- pandas>=1.0
- scipy>=0.11
Expand Down
2 changes: 1 addition & 1 deletion ci/311-latest.yaml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels:
dependencies:
- python=3.11
# required
- libpysal
- libpysal>=4.12.1
- numpy>=1.3
- pandas>=1.0
- scipy>=0.11
Expand Down
2 changes: 1 addition & 1 deletion ci/312-latest.yaml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels:
dependencies:
- python=3.12
# required
- libpysal
- libpysal>=4.12.1
- numpy>=1.3
- pandas>=1.0
- scipy>=0.11
Expand Down
27 changes: 0 additions & 27 deletions ci/39-latest.yaml

This file was deleted.

10 changes: 5 additions & 5 deletions docs/_static/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
@article{Anselin2021,
Author = {Anselin, Luc and Amaral, Pedro},
Journal = {},
Number = {},
Pages = {},
Journal = {Journal of Geographical Systems},
Number = {26},
Pages = {209–234},
Title = {Endogenous Spatial Regimes},
Url = {https://www.researchgate.net/publication/353411566_Endogenous_Spatial_Regimes},
Url = {https://doi.org/10.1007/s10109-023-00411-2},
Volume = {},
Year = {2021}}
Year = {2024}}

@article{McMillen1992,
Abstract = {ABSTRACT. Commonly-employed spatial autocorrelation models imply heteroskedastic errors, but heteroskedasticity causes probit to be inconsistent. This paper proposes and illustrates the use of two categories of estimators for probit models with spatial autocorrelation. One category is based on the EM algorithm, and requires repeated application of a maximum-likelihood estimator. The other category, which can be applied to models derived using the spatial expansion method, only requires weighted least squares.},
Expand Down
20 changes: 16 additions & 4 deletions docs/api.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ API reference

.. _models_api:

Classic Models
--------------

.. autosummary::
:toctree: generated/

spreg.OLS
spreg.TSLS

Spatial Regression Models
-------------------------

Expand All @@ -15,10 +24,10 @@ These are the standard spatial regression models supported by the `spreg` packag
.. autosummary::
:toctree: generated/

spreg.OLS
spreg.GM_Lag
spreg.ML_Lag
spreg.GMM_Error
spreg.ML_Error
spreg.GM_Lag
spreg.GM_Error
spreg.GM_Error_Het
spreg.GM_Error_Hom
Expand All @@ -28,11 +37,11 @@ These are the standard spatial regression models supported by the `spreg` packag
spreg.GM_Endog_Error
spreg.GM_Endog_Error_Het
spreg.GM_Endog_Error_Hom
spreg.TSLS
spreg.ThreeSLS
spreg.NSLX

Discrete Choice Models
----------------------

.. autosummary::
:toctree: generated/

Expand Down Expand Up @@ -60,6 +69,8 @@ Regimes models are variants of spatial regression models which allow for structu
spreg.GM_Endog_Error_Regimes
spreg.GM_Endog_Error_Hom_Regimes
spreg.GM_Endog_Error_Het_Regimes
spreg.OLS_Endog_Regimes
spreg.GM_Lag_Endog_Regimes
spreg.Skater_reg

Seemingly-Unrelated Regressions
Expand Down Expand Up @@ -107,6 +118,7 @@ Diagnostic tests are useful for identifying model fit, sufficiency, and specific
spreg.akaike
spreg.schwarz
spreg.condition_index
spreg.dwh
spreg.jarque_bera
spreg.breusch_pagan
spreg.white
Expand Down
3 changes: 3 additions & 0 deletions spreg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import contextlib
from importlib.metadata import PackageNotFoundError, version

Expand All @@ -17,13 +18,15 @@
from .ml_error_regimes import *
from .ml_lag import *
from .ml_lag_regimes import *
from .nslx import *
from .ols import *
from .ols_regimes import *
from .panel_fe import *
from .panel_re import *
from .probit import *
from .regimes import *
from .skater_reg import *
#from .spsearch import *
from .sp_panels import *
from .sputils import *
from .sur import *
Expand Down
46 changes: 26 additions & 20 deletions spreg/diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
Diagnostics for regression estimations.
Diagnostics for regression estimations.
"""

__author__ = (
"Luc Anselin luc.anselin@asu.edu, Nicholas Malizia nicholas.malizia@asu.edu "
)
Expand Down Expand Up @@ -39,7 +38,7 @@
]


def f_stat(reg, df=0):
def f_stat(reg,df=0):
"""
Calculates the f-statistic and associated p-value for multiple
coefficient constraints :cite:`Greene2003`.
Expand All @@ -50,7 +49,7 @@ def f_stat(reg, df=0):
----------
reg : regression object
output instance from a regression model
df : number of coefficient constraints
df : number of coefficient constraints
(zero constraint for last df coefficients in betas)
Returns
Expand Down Expand Up @@ -101,15 +100,15 @@ def f_stat(reg, df=0):
utu = reg.utu # (scalar) residual sum of squares
# default case, all coefficients
if df == 0:
r = k - 1
r = k-1
predy = reg.predy # (array) vector of predicted values (n x 1)
mean_y = reg.mean_y # (scalar) mean of dependent observations
U = np.sum((predy - mean_y) ** 2)
else: # F test on last df coefficients
else: # F test on last df coefficients
y = reg.y
r = df
x0 = reg.x[:, :-r]
olsr = BaseOLS(y, x0) # constrained regression
x0 = reg.x[:,:-r]
olsr = BaseOLS(y,x0) # constrained regression
rtr = olsr.utu
U = rtr - utu
fStat = (U / r) / (utu / (n - k))
Expand Down Expand Up @@ -1386,10 +1385,17 @@ def likratiotest(reg0, reg1):
raise Exception("Missing or improper log-likelihoods in regression objects")
if likr < 0.0: # always enforces positive likelihood ratio
likr = -likr
pvalue = stats.chisqprob(likr, 1)
likratio = {"likr": likr, "df": 1, "p-value": pvalue}
return likratio

# generalize to multiple parameters, e.g., spatial Durbin
df = reg1.k - reg0.k
if not(df > 0):
df = 1

#pvalue = stats.chisqprob(likr, 1)
pvalue = stats.chisqprob(likr, df)
#likratio = {"likr": likr, "df": 1, "p-value": pvalue}
likratio = {"likr": likr, "df": df, "p-value": pvalue}
return likratio

def dwh(reg):
"""
Expand All @@ -1408,23 +1414,23 @@ def dwh(reg):
and associated p-value
"""
n = reg.n
ny = reg.yend.shape[1] # number of endogenous variables
n = reg.n
ny = reg.yend.shape[1] # number of endogenous variables
qq = reg.h # all exogenous and instruments
xx = reg.z # all exogenous and endogenous
# get predicted values for endogenous variables on all instruments
py = np.zeros((n, ny))
py = np.zeros((n,ny))
for i in range(ny):
yy = reg.yend[:, i].reshape(n, 1)
ols1 = BaseOLS(y=yy, x=qq)
yy = reg.yend[:, i].reshape(n,1)
ols1 = BaseOLS(y=yy,x=qq)
yp = ols1.predy
py[0:n, i] = yp.flatten()
py[0:n,i] = yp.flatten()
nxq = sphstack(xx, py)
# F-test in augmented regression
ols2 = BaseOLS(y=reg.y, x=nxq)
dwh = f_stat(ols2, df=ny)
return dwh

return dwh

def _test():
import doctest
Expand Down
Loading

0 comments on commit 8c19228

Please sign in to comment.