Skip to content

adding alternative option in local_moran and moral_local_rate #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions esda/moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,7 @@ def by_col(
def Moran_BV_matrix(variables, w, permutations=0, varnames=None):
"""
Bivariate Moran Matrix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that your editor removed blank lines in the docstring. Can you please put them back (all of them)?

Calculates bivariate Moran between all pairs of a set of variables.

Parameters
----------
variables : array or pandas.DataFrame
Expand All @@ -531,36 +529,24 @@ def Moran_BV_matrix(variables, w, permutations=0, varnames=None):
results : dictionary
(i, j) is the key for the pair of variables, values are
the Moran_BV objects.

Examples
--------

open dbf

>>> import libpysal
>>> f = libpysal.io.open(libpysal.examples.get_path("sids2.dbf"))

pull of selected variables from dbf and create numpy arrays for each

>>> varnames = ['SIDR74', 'SIDR79', 'NWR74', 'NWR79']
>>> vars = [np.array(f.by_col[var]) for var in varnames]

create a contiguity matrix from an external gal file

>>> w = libpysal.io.open(libpysal.examples.get_path("sids2.gal")).read()

create an instance of Moran_BV_matrix

>>> from esda.moran import Moran_BV_matrix
>>> res = Moran_BV_matrix(vars, w, varnames = varnames)

check values

>>> round(res[(0, 1)].I,7)
0.1936261
>>> round(res[(3, 0)].I,7)
0.3770138

"""
try:
# check if pandas is installed
Expand Down Expand Up @@ -889,6 +875,9 @@ class Moran_Local(object):
value to use as a weight for the "fake" neighbor for every island. If numpy.nan,
will propagate to the final local statistic depending on the `stat_func`. If 0, then
the lag is always zero for islands.
alternative: string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
alternative: string
alternative: {"two-tailed", "one-tailed"}
(default="two-tailed")

This follows the pattern used above.

possible values -> "one-tailed"/ "two-tailed"
default value ->"two-tailed"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part should contain a description of what the argument controls, i.e. what does it mean if I use "one-tailed" vs "two-tailed". Does not have to be detailed but just a hint, an explanation in plain words. Like in permutations, where we say "number of random permutations for calculation of pseudo p_values".


Attributes
----------
Expand Down Expand Up @@ -1005,6 +994,7 @@ def __init__(
keep_simulations=True,
seed=None,
island_weight=0,
alternative = "two-tailed"
):
y = np.asarray(y).flatten()
self.y = y
Expand Down Expand Up @@ -1063,6 +1053,12 @@ def __init__(
self.VI_sim = np.nan
self.z_sim = np.nan
self.p_z_sim = np.nan
if alternative=="one-tailed":
self.p_z_sim= self.p_z_sim*2
folded_replicates = np.abs(self.rlisas - np.median(self.rlisas, axis=1, keepdims=True))
self.p_sim = (folded_replicates >= np.abs(self.Is[:,None])).mean(axis=1)



def __calc(self, w, z):
zl = slag(w, z)
Expand Down Expand Up @@ -1575,6 +1571,7 @@ def __init__(
keep_simulations=True,
seed=None,
island_weight=0,
alternative="two-tailed"
):
e = np.asarray(e).flatten()
b = np.asarray(b).flatten()
Expand All @@ -1592,6 +1589,7 @@ def __init__(
n_jobs=n_jobs,
keep_simulations=keep_simulations,
seed=seed,
alternative=alternative
)

@classmethod
Expand Down