Skip to content

Commit

Permalink
adapt import to libpysal update
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi committed Oct 24, 2023
1 parent c0ead26 commit ae8a959
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
34 changes: 18 additions & 16 deletions spreg/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
"Luc Anselin [email protected], Nicholas Malizia [email protected] "
)

from libpysal.common import *
import scipy.sparse as SP
from math import sqrt, pi

from libpysal.common import MISSINGVALUE
import numpy as np
import numpy.linalg as la
import scipy.sparse as SP
from scipy import stats

from .utils import spmultiply, sphstack, spmin, spmax


Expand Down Expand Up @@ -160,12 +165,9 @@ def t_stat(reg, z_stat=False):
vm = reg.vm # (array) coefficients of variance matrix (k x k)
betas = reg.betas # (array) coefficients of the regressors (1 x k)
variance = vm.diagonal()
tStat = (
betas[list(range(0, len(vm)))].reshape(
len(vm),
)
/ np.sqrt(variance)
)
tStat = betas[list(range(0, len(vm)))].reshape(
len(vm),
) / np.sqrt(variance)
ts_result = []
for t in tStat:
if z_stat:
Expand Down Expand Up @@ -678,15 +680,15 @@ def jarque_bera(reg):
"""
n = reg.n # (scalar) number of observations
u = reg.u # (array) residuals from regression
u2 = u ** 2
u3 = u ** 3
u4 = u ** 4
u2 = u**2
u3 = u**3
u4 = u**4
mu2 = np.mean(u2)
mu3 = np.mean(u3)
mu4 = np.mean(u4)
S = mu3 / (mu2 ** (1.5)) # skewness measure
K = mu4 / (mu2 ** 2) # kurtosis measure
jb = n * (((S ** 2) / 6) + ((K - 3) ** 2) / 24)
K = mu4 / (mu2**2) # kurtosis measure
jb = n * (((S**2) / 6) + ((K - 3) ** 2) / 24)
pvalue = stats.chisqprob(jb, 2)
jb_result = {"df": 2, "jb": jb, "pvalue": pvalue}
return jb_result
Expand Down Expand Up @@ -776,7 +778,7 @@ def breusch_pagan(reg, z=None):
0.0193
"""
e2 = reg.u ** 2
e2 = reg.u**2
e = reg.u
n = reg.n
k = reg.k
Expand Down Expand Up @@ -919,7 +921,7 @@ def white(reg):
0.0013
"""
e = reg.u ** 2
e = reg.u**2
k = int(reg.k)
n = int(reg.n)
y = reg.y
Expand Down Expand Up @@ -1084,7 +1086,7 @@ def koenker_bassett(reg, z=None):
"""
# The notation here matches that of Greene (2003).
u = reg.u ** 2
u = reg.u**2
e = reg.u
n = reg.n
k = reg.k
Expand Down
16 changes: 7 additions & 9 deletions spreg/diagnostics_tsls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"Luc Anselin [email protected], Nicholas Malizia [email protected] "
)

from libpysal.common import *
import numpy as np
from scipy import stats
from scipy.stats import pearsonr

__all__ = ["t_stat", "pr2_aspatial", "pr2_spatial"]
Expand Down Expand Up @@ -118,12 +119,9 @@ def t_stat(reg, z_stat=False):
vm = reg.vm # (array) coefficients of variance matrix (k x k)
betas = reg.betas # (array) coefficients of the regressors (1 x k)
variance = vm.diagonal()
tStat = (
betas.reshape(
len(betas),
)
/ np.sqrt(variance)
)
tStat = betas.reshape(
len(betas),
) / np.sqrt(variance)
ts_result = []
for t in tStat:
if z_stat:
Expand Down Expand Up @@ -221,7 +219,7 @@ def pr2_aspatial(tslsreg):
y = tslsreg.y
predy = tslsreg.predy
pr = pearsonr(y.flatten(), predy.flatten())[0]
pr2_result = float(pr ** 2)
pr2_result = float(pr**2)
return pr2_result


Expand Down Expand Up @@ -329,7 +327,7 @@ def pr2_spatial(tslsreg):
y = tslsreg.y
predy_e = tslsreg.predy_e
pr = pearsonr(y.flatten(), predy_e.flatten())[0]
pr2_result = float(pr ** 2)
pr2_result = float(pr**2)
return pr2_result


Expand Down

0 comments on commit ae8a959

Please sign in to comment.