esda.Moran_Rate¶
-
class esda.Moran_Rate(e, b, w, adjusted=
True, transformation='r', permutations=999, two_tailed=True)[source]¶ Adjusted Moran’s I Global Autocorrelation Statistic for Rate Variables [Assuncao and Reis, 1999]
- Parameters:¶
- e : array¶
an event variable measured across n spatial units
- b : array¶
a population-at-risk variable measured across n spatial units
- w : W | Graph¶
spatial weights instance as W or Graph aligned with e and b
- adjusted : boolean¶
whether or not Moran’s I needs to be adjusted for rate variable
- transformation : {'R', 'B', 'D', 'U', 'V'}¶
weights transformation, default is row-standardized “r”. Other options include “B”: binary, “D”: doubly-standardized, “O”: restore original transformation (applicable only if
wis passed asW), “V”: variance-stabilizing.- two_tailed : boolean¶
If True (default), analytical p-values for Moran’s I are two-tailed, otherwise they are one tailed.
- permutations : int¶
number of random permutations for calculation of pseudo p_values
- y[source]¶
rate variable computed from parameters e and b if adjusted is True, y is standardized rates otherwise, y is raw rates
- Type:¶
array
- two_tailed[source]¶
If True, p_norm and p_rand are two-tailed p-values, otherwise they are one-tailed.
- Type:¶
boolean
- p_sim[source]¶
(if permutations>0) p-value based on permutations (one-sided) null: spatial randomness alternative: the observed I is extreme if it is either extremely greater or extremely lower than the values obtained from permutaitons
- Type:¶
array
Examples
>>> import libpysal, numpy >>> w = libpysal.io.open(libpysal.examples.get_path("sids2.gal")).read() >>> f = libpysal.io.open(libpysal.examples.get_path("sids2.dbf")) >>> e = numpy.array(f.by_col('SID79')) >>> b = numpy.array(f.by_col('BIR79')) >>> from esda import Moran_Rate >>> mi = Moran_Rate(e, b, w, two_tailed=False) >>> "%6.4f" % mi.I '0.1662' >>> "%6.4f" % mi.p_norm '0.0042'Methods
by_col(df, events, populations[, w, ...])Function to compute a Moran_Rate statistic on a dataframe
plot_scatter([ax, scatter_kwds, ...])Plot a Moran scatterplot with optional coloring for significant points.
plot_simulation([ax, legend, fitline_kwds])Global Moran's I simulated reference distribution.
-
classmethod by_col(df, events, populations, w=
None, inplace=False, pvalue='sim', outvals=None, swapname='', **stat_kws)[source]¶ Function to compute a Moran_Rate statistic on a dataframe
- Parameters:¶
- df : pandas.DataFrame¶
a pandas dataframe with a geometry column
- events : string or list of strings¶
one or more names where events are stored
- populations : string or list of strings¶
one or more names where the populations corresponding to the events are stored. If one population column is provided, it is used for all event columns. If more than one population column is provided but there is not a population for every event column, an exception will be raised.
- w : W | Graph¶
spatial weights instance as W or Graph aligned with the dataframe. If not provided, this is searched for in the dataframe’s metadata
- inplace : bool¶
a boolean denoting whether to operate on the dataframe inplace or to return a series contaning the results of the computation. If operating inplace, the derived columns will be named ‘column_moran_rate’
- pvalue : string¶
a string denoting which pvalue should be returned. Refer to the the Moran_Rate statistic’s documentation for available p-values
- outvals : list of strings¶
list of arbitrary attributes to return as columns from the Moran_Rate statistic
- **stat_kws : keyword arguments¶
options to pass to the underlying statistic. For this, see the documentation for the Moran_Rate statistic.
- Returns:¶
If inplace, None, and operation is conducted on dataframe
in memory. Otherwise, returns a copy of the dataframe with
the relevant columns attached.
-
plot_scatter(ax=
None, scatter_kwds=None, fitline_kwds=None, losh_scaling_factor=False, losh_inference=None, a=2)[source]¶ Plot a Moran scatterplot with optional coloring for significant points.
- Parameters:¶
- ax : matplotlib.axes.Axes, optional¶
Pre-existing axes for the plot, by default None.
- scatter_kwds : dict, optional¶
Additional keyword arguments for scatter plot, by default None.
- fitline_kwds : dict, optional¶
Additional keyword arguments for fit line, by default None.
- losh_scaling_factor : bool | int | float, by default False¶
Scale the observations by LOSH. When set to a number, it is treated as the multiplicative factor applied to
exp(LOSH.Hi)when converting LOSH values into marker areas.- losh_inference : str, optional¶
Inference method for
LOSH. SeeLOSHfor supported options. Applies only iflosh_scaling_factoris notFalse.- a : int or float, default=2¶
Residual exponent passed to
esda.losh.LOSH.fit(). The default corresponds to a variance-based LOSH measure. Applies only iflosh_scaling_factoris notFalse.
- Returns:¶
Axes object with the Moran scatterplot.
- Return type:¶
matplotlib.axes.Axes
-
plot_simulation(ax=
None, legend=False, fitline_kwds=None, **kwargs)[source]¶ Global Moran’s I simulated reference distribution.
- Parameters:¶
- ax : matplotlib.axes.Axes, optional¶
Pre-existing axes for the plot, by default None.
- legend : bool, optional¶
Plot a legend, by default False
- fitline_kwds : dict, optional¶
Additional keyword arguments for vertical Moran fit line, by default None.
- **kwargs : keyword arguments, optional¶
Additional keyword arguments for KDE plot passed to
seaborn.kdeplot, by default None.
- Returns:¶
Axes object with the Moran scatterplot.
- Return type:¶
matplotlib.axes.Axes
Notes
This requires optional dependencies
matplotlibandseaborn.Examples
>>> import libpysal, numpy >>> w = libpysal.io.open(libpysal.examples.get_path("stl.gal")).read() >>> f = libpysal.io.open(libpysal.examples.get_path("stl_hom.txt")) >>> y = numpy.array(f.by_col['HR8893']) >>> from esda import Moran >>> mi = Moran(y, w)Default plot:
>>> mi.plot_simulation()Customized styling that turns the distribution into a pink line and line indicating I to a black line:
>>> mi.plot_simulation(fitline_kwds={"color": "k"}, color="pink", shade=False)