This page was generated from user-guide/weights/categorical_lag.ipynb. Interactive online version: Binder badge

Categorical Spatial Lags

[ ]:
>>> from libpysal.graph._spatial_lag import _lag_spatial
>>> import numpy as np
>>> from libpysal.weights.util import lat2W
>>> from libpysal.graph import Graph
>>> graph = Graph.from_W(lat2W(3,3))

[36]:
>>> y = np.arange(9)
>>> _lag_spatial(graph, y)


[36]:
array([ 4.,  6.,  6., 10., 16., 14., 10., 18., 12.])
[ ]:
>>> y = np.array([*'ababcbcbc'])
>>> _lag_spatial(graph, y, categorical=True)
array(['b', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b'], dtype=object)
[ ]:

[ ]:

[28]:
>>> y[3] = 'a'
>>> y
[28]:
array(['a', 'b', 'a', 'a', 'c', 'b', 'c', 'b', 'c'], dtype='<U1')
[34]:
>>> np.random.seed(12345)
>>> _lag_spatial(graph, y, categorical=True, ties='random')
[34]:
array(['a', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b'], dtype=object)
[35]:
>>> _lag_spatial(graph, y, categorical=True, ties='random')
[35]:
array(['b', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b'], dtype=object)
[30]:
>>> _lag_spatial(graph, y, categorical=True, ties='tryself')
[30]:
array(['a', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b'], dtype=object)
[31]:
_lag_spatial(graph, y, categorical=True)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[31], line 1
----> 1 _lag_spatial(graph, y, categorical=True)

File ~/para/1_projects/code-pysal-libpysal/libpysal/libpysal/graph/_spatial_lag.py:58, in _lag_spatial(graph, y, categorical, ties)
     56 n_ties = gb.apply(_check_ties).sum()
     57 if n_ties and ties == 'raise':
---> 58     raise ValueError(
     59         f"There are {n_ties} ties that must be broken "
     60         f"to define the categorical "
     61         "spatial lag for these observations. To address this "
     62         "issue, consider setting `ties='tryself'` "
     63         "or `ties='random'` or consult the documentation "
     64         "about ties and the categorical spatial lag."
     65     )
     66 elif ties in ('random', 'tryself', 'raise'):
     67     return gb.apply(_get_categorical_lag).values

ValueError: There are 2 ties that must be broken to define the categorical spatial lag for these observations. To address this issue, consider setting `ties='tryself'` or `ties='random'` or consult the documentation about ties and the categorical spatial lag.
[ ]:

[ ]:

[1]:
>>> import libpysal
>>> import numpy as np
>>> np.random.seed(12345)
>>> w = libpysal.weights.lat2W(3, 3)
>>> y = np.array([*'ababcbcbc'])
>>> y_l = libpysal.weights.lag_categorical(w, y)
>>> np.array_equal(y_l, np.array(['b', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b']))


[1]:
True
[2]:
from libpysal import graph
[3]:
g = graph.Graph.from_W(w)
[4]:
g.adjacency
[4]:
focal  neighbor
0      1           1.0
       3           1.0
1      0           1.0
       2           1.0
       4           1.0
2      1           1.0
       5           1.0
3      0           1.0
       4           1.0
       6           1.0
4      1           1.0
       3           1.0
       5           1.0
       7           1.0
5      2           1.0
       4           1.0
       8           1.0
6      3           1.0
       7           1.0
7      4           1.0
       6           1.0
       8           1.0
8      5           1.0
       7           1.0
Name: weight, dtype: float64
[5]:
from libpysal.graph._spatial_lag import _lag_spatial
[6]:
import numpy
_lag_spatial(g, numpy.array(y), categorical=True)
[6]:
array(['b', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b'], dtype=object)

No Ties

[7]:
_lag_spatial(g, numpy.array(y), categorical=True, ties='tryself')
[7]:
array(['b', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b'], dtype=object)
[8]:
np.array_equal(y_l, _lag_spatial(g, numpy.array(y), categorical=True, ties='random'))
[8]:
True
[9]:
np.array_equal(y_l, _lag_spatial(g, numpy.array(y), categorical=True, ties='raise'))
[9]:
True

Ties

[10]:
y[3] = 'a'
y
[10]:
array(['a', 'b', 'a', 'a', 'c', 'b', 'c', 'b', 'c'], dtype='<U1')
[14]:
np.random.seed(12345)
[15]:
_lag_spatial(g, numpy.array(y), categorical=True, ties='random')
[15]:
array(['a', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b'], dtype=object)
[12]:
_lag_spatial(g, numpy.array(y), categorical=True, ties='tryself')
[12]:
array(['a', 'a', 'b', 'c', 'b', 'c', 'b', 'c', 'b'], dtype=object)
[13]:
y_l = libpysal.weights.lag_categorical(w, y)
np.array_equal(y_l, _lag_spatial(g, numpy.array(y), categorical=True, ties='raise'))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[13], line 2
      1 y_l = libpysal.weights.lag_categorical(w, y)
----> 2 np.array_equal(y_l, _lag_spatial(g, numpy.array(y), categorical=True, ties='raise'))

File ~/para/1_projects/code-pysal-libpysal/libpysal/libpysal/graph/_spatial_lag.py:58, in _lag_spatial(graph, y, categorical, ties)
     56 n_ties = gb.apply(_check_ties).sum()
     57 if n_ties and ties == 'raise':
---> 58     raise ValueError(
     59         f"There are {n_ties} ties that must be broken "
     60         f"to define the categorical "
     61         "spatial lag for these observations. To address this "
     62         "issue, consider setting `ties='tryself'` "
     63         "or `ties='random'` or consult the documentation "
     64         "about ties and the categorical spatial lag."
     65     )
     66 elif ties in ('random', 'tryself', 'raise'):
     67     return gb.apply(_get_categorical_lag).values

ValueError: There are 2 ties that must be broken to define the categorical spatial lag for these observations. To address this issue, consider setting `ties='tryself'` or `ties='random'` or consult the documentation about ties and the categorical spatial lag.
[ ]:
y_l
[ ]: