| Title: | Geospatial Distribution Dynamics for Simple Features |
| Version: | 0.2.0 |
| Description: | Tools for exploratory geospatial distribution dynamics with 'sf' objects and tidy data. Provides pooled and time-specific classification of longitudinal spatial values, classic discrete Markov transition matrices, spatial Markov matrices conditioned on spatial-lag classes, endpoint and adjacent rank mobility summaries, ergodic-chain summaries, transition-regime homogeneity tests, and 'ggplot2' visualizations. Methods follow Rey (2001) <doi:10.1111/j.1538-4632.2001.tb00444.x> and Rey et al. (2016) <doi:10.1007/s10109-016-0234-x>; design is inspired by the Python 'PySAL' 'giddy' package https://pysal.org/giddy/. |
| URL: | https://github.com/dshkol/griddy, https://dshkol.github.io/griddy/ |
| BugReports: | https://github.com/dshkol/griddy/issues |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| Language: | en-US |
| RoxygenNote: | 7.3.2 |
| Depends: | R (≥ 4.2) |
| Imports: | dplyr, ggplot2, rlang, scales, sf, spdep, tibble, tidyr |
| Suggests: | cancensus, knitr, microbenchmark, pkgdown, rmarkdown, sfdep, spData, testthat (≥ 3.0.0), tidycensus |
| VignetteBuilder: | knitr |
| Config/testthat/edition: | 3 |
| LazyData: | true |
| NeedsCompilation: | no |
| Packaged: | 2026-11-25 03:50:43 UTC; dmitryshkolnik |
| Author: | Dmitry Shkolnik [aut, cre] |
| Maintainer: | Dmitry Shkolnik <shkolnikd@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-25 08:30:02 UTC |
griddy: Geospatial Distribution Dynamics
Description
Tidy helpers for exploratory geospatial distribution dynamics.
Author(s)
Maintainer: Dmitry Shkolnik shkolnikd@gmail.com
See Also
Useful links:
Report bugs at https://github.com/dshkol/griddy/issues
Class Intervals Used By A griddy Object
Description
Class Intervals Used By A griddy Object
Usage
class_intervals(x, ...)
Arguments
x |
A griddy result object. |
... |
Reserved for future methods. |
Value
A tibble of class intervals when available.
Examples
panel <- data.frame(id = rep(1:3, each = 2), year = rep(2020:2021, 3), value = 1:6)
classes <- classify_dynamics(panel, id, year, value, k = 3)
class_intervals(classes)
Classify Longitudinal Values For Distribution Dynamics
Description
Converts numeric values in a long panel into ordered classes suitable for transition analysis.
Usage
classify_dynamics(
data,
id,
time,
value,
method = c("pooled_quantile", "time_quantile", "fixed", "existing"),
k = 5,
breaks = NULL,
labels = NULL
)
Arguments
data |
A data frame or |
id, time, value |
Columns identifying spatial unit, time, and value. |
method |
Classification method. |
k |
Number of quantile classes. |
breaks |
Breaks for |
labels |
Optional class labels. |
Value
A data frame or sf object with a class column and class
grd_classes.
Examples
panel <- data.frame(
id = rep(letters[1:4], each = 3),
year = rep(2020:2022, times = 4),
value = c(8, 9, 11, 10, 12, 13, 15, 14, 16, 20, 22, 25)
)
classes <- classify_dynamics(panel, id, year, value, k = 3)
classes
class_intervals(classes)
Mean First-Passage Times
Description
Computes the matrix of mean first-passage times for an ergodic transition
probability matrix following Kemeny and Snell: entry [i, j] is the
expected number of transitions to reach class j for the first time
starting from class i. Diagonal entries are mean recurrence times,
the reciprocal of the stationary distribution.
Usage
first_passage(x)
Arguments
x |
A |
Value
A square numeric matrix of mean first-passage times.
Examples
prob <- matrix(c(0.9, 0.1, 0.2, 0.8), nrow = 2, byrow = TRUE)
first_passage(prob)
Test Homogeneity Of Transition Matrices Across Regimes
Description
Tests whether transition dynamics are homogeneous across regimes – for a
spatial Markov object, whether the transition matrices conditioned on
spatial-lag class genuinely differ from the pooled matrix (Rey, Kang, and
Wolf 2016). Three statistics are reported: a Pearson chi-squared Q test and
a likelihood-ratio test with the degrees-of-freedom adjustment of
Bickenbach and Bode (2003), and the Kullback information test (Kullback,
Kupperman, and Ku 1962). The Pearson and likelihood-ratio statistics mirror
the PySAL giddy Spatial_Markov homogeneity attributes.
Usage
homogeneity_test(x, ...)
Arguments
x |
A |
... |
Reserved for future methods. |
Details
The Kullback statistic uses origin-state (row) margins, which makes it
algebraically identical to the likelihood-ratio statistic. It is reported
for comparability with giddy.markov.kullback() and uses the same
Bickenbach–Bode degrees of freedom as the other two tests. giddy 2.3.9 and
earlier differ in two ways: the stratum-specific term uses terminal-state
(column) margins (fixed upstream in
https://github.com/pysal/giddy/pull/260), and the degrees of freedom are
the unadjusted r(s - 1)(r - 1), which overstates them when some
transitions are never observed (see
https://github.com/pysal/giddy/issues/265). Origin states with no
transitions in any regime contribute no degrees of freedom to any of the
three tests.
Value
A tibble with one row per test (chi2, likelihood_ratio,
kullback) and columns test, statistic, df, and p_value. The
null hypothesis is that all regimes share the pooled transition matrix;
small p-values indicate the conditional dynamics differ.
References
Bickenbach, F., and Bode, E. (2003). Evaluating the Markov property in studies of economic convergence. International Regional Science Review, 26(3), 363–392. doi:10.1177/0160017603253789
Kullback, S., Kupperman, M., and Ku, H. H. (1962). Tests for contingency tables and Markov chains. Technometrics, 4(4), 573–608. doi:10.1080/00401706.1962.10490041
Rey, S. J., Kang, W., and Wolf, L. (2016). The properties of tests for spatial effects in discrete Markov chain models of regional income distribution dynamics. Journal of Geographical Systems, 18(4), 377–398. doi:10.1007/s10109-016-0234-x
Examples
panel <- data.frame(
id = rep(1:9, each = 3),
year = rep(2020:2022, times = 9),
value = c(
1, 2, 3, 4, 3, 5, 6, 7, 6, 8, 9, 10, 11, 10,
12, 13, 14, 13, 15, 16, 17, 18, 17, 19, 20, 21, 20
)
)
grid <- sf::st_sf(
id = 1:9,
geometry = sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 3, ymax = 3)),
n = c(3, 3)
)
) |>
dplyr::mutate(
nb = sfdep::st_contiguity(geometry),
wt = sfdep::st_weights(nb)
)
spatial <- spatial_markov(panel, id, year, value, geometry = grid, k = 3)
homogeneity_test(spatial)
Spatial Lag Intervals Used By A Spatial Markov Object
Description
Spatial Lag Intervals Used By A Spatial Markov Object
Usage
lag_intervals(x)
Arguments
x |
A |
Value
A tibble of spatial-lag class intervals.
Examples
panel <- data.frame(
id = rep(1:4, each = 2),
year = rep(2020:2021, times = 4),
value = c(1, 2, 2, 3, 4, 3, 5, 6)
)
grid <- sf::st_sf(
id = 1:4,
geometry = sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 2, ymax = 2)),
n = c(2, 2)
)
) |>
dplyr::mutate(
nb = sfdep::st_contiguity(geometry),
wt = sfdep::st_weights(nb)
)
spatial <- spatial_markov(panel, id, year, value, geometry = grid, k = 2)
lag_intervals(spatial)
Estimate Classic Markov Transition Dynamics
Description
Estimates transition counts and probabilities between adjacent periods for a classified long panel.
Usage
markov_dynamics(classes, id, time, state = class)
Arguments
classes |
A classified data frame from |
id, time, state |
Columns identifying spatial unit, time, and state. |
Value
A grd_markov object.
Examples
panel <- data.frame(
id = rep(letters[1:4], each = 3),
year = rep(2020:2022, times = 4),
value = c(8, 9, 11, 10, 12, 13, 15, 14, 16, 20, 22, 25)
)
classes <- classify_dynamics(panel, id, year, value, k = 3)
markov <- markov_dynamics(classes, id, year, class)
markov
transition_matrix(markov)
steady_state(markov)
Markov Mobility Indices
Description
Computes scalar mobility indices summarizing how much movement a transition
probability matrix implies, following Formby, Smith, and Zheng (2004) and
mirroring giddy.mobility.markov_mobility() in PySAL giddy.
Usage
mobility_index(
x,
measure = c("prais", "determinant", "eigen", "bartholomew1", "bartholomew2"),
initial = NULL
)
Arguments
x |
A |
measure |
Character vector of indices to compute. Any of:
|
initial |
Optional initial distribution over classes used by the
Bartholomew indices. Defaults to the uniform distribution, matching
|
Value
A named numeric vector with one element per requested measure. All indices are 0 for the identity matrix (no mobility) and increase with mobility.
Examples
prob <- matrix(c(0.9, 0.1, 0.2, 0.8), nrow = 2, byrow = TRUE)
mobility_index(prob)
mobility_index(prob, measure = "prais")
Plot Rank Mobility
Description
Plot Rank Mobility
Usage
plot_rank_mobility(x)
Arguments
x |
A |
Value
A ggplot object.
Plot Spatial Markov Matrices
Description
Plot Spatial Markov Matrices
Usage
plot_spatial_markov(x)
Arguments
x |
A |
Value
A ggplot object.
Examples
panel <- data.frame(
id = rep(1:4, each = 2),
year = rep(2020:2021, times = 4),
value = c(1, 2, 2, 3, 4, 3, 5, 6)
)
grid <- sf::st_sf(
id = 1:4,
geometry = sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 2, ymax = 2)),
n = c(2, 2)
)
) |>
dplyr::mutate(
nb = sfdep::st_contiguity(geometry),
wt = sfdep::st_weights(nb)
)
spatial <- spatial_markov(panel, id, year, value, geometry = grid, k = 2)
plot_spatial_markov(spatial)
Plot A Transition Matrix
Description
Plot A Transition Matrix
Usage
plot_transition_matrix(x)
Arguments
x |
A |
Value
A ggplot object.
Examples
panel <- data.frame(id = rep(1:4, each = 2), year = rep(2020:2021, 4), value = 1:8)
markov <- panel |>
classify_dynamics(id, year, value, k = 2) |>
markov_dynamics(id, year, class)
plot_transition_matrix(markov)
Compute Rank Mobility
Description
Computes simple rank changes for map-ready exploratory analysis.
Usage
rank_mobility(
data,
id,
time,
value,
compare = c("endpoint", "adjacent"),
descending = TRUE
)
Arguments
data |
A long data frame or |
id, time, value |
Columns identifying spatial unit, time, and value. |
compare |
|
descending |
If |
Value
A grd_rank_mobility data frame or sf object.
Examples
panel <- data.frame(
id = rep(letters[1:4], each = 3),
year = rep(2020:2022, times = 4),
value = c(8, 9, 11, 10, 12, 13, 15, 14, 16, 20, 22, 25)
)
rank_mobility(panel, id, year, value)
Expected Sojourn Times
Description
Computes the expected number of consecutive periods a unit remains in each
class once it enters it, 1 / (1 - p_ii). Absorbing classes (p_ii = 1)
have infinite sojourn times.
Usage
sojourn_time(x)
Arguments
x |
A |
Value
A named numeric vector of expected sojourn times, one per class.
Examples
prob <- matrix(c(0.9, 0.1, 0.2, 0.8), nrow = 2, byrow = TRUE)
sojourn_time(prob)
Estimate Spatial Markov Transition Dynamics
Description
Estimates transition matrices conditioned on the class of each unit's spatial lag at the start of the transition period.
Usage
spatial_markov(
data,
id,
time,
value,
geometry = NULL,
geometry_id = NULL,
listw = NULL,
nb = NULL,
k = 5,
lag_k = k,
class_method = c("pooled_quantile", "time_quantile", "fixed"),
zero.policy = TRUE
)
Arguments
data |
A long data frame or |
id, time, value |
Columns identifying spatial unit, time, and value. |
geometry |
An |
geometry_id |
The column of |
listw |
A row-standardized |
nb |
A |
k |
Number of value classes. |
lag_k |
Number of spatial-lag classes. |
class_method |
Value classification method passed to |
zero.policy |
Passed to |
Value
A grd_spatial_markov object.
Matching weights to units
Each row of geometry, listw, or nb describes one unit. The neighbor
lists refer to rows by position, so build them after the last filter or
reorder of the geography. spatial_markov() matches rows to the units in
data by id, and the row order of the weights does not matter. It stops
when the weights' ids have missing or duplicated values or do not match the
values of id one to one.
When the weights carry no usable ids, rows are matched by position: the
first row is taken to be the unit with the smallest id, the second row
the next, and so on. This happens when geometry has neither a
geometry_id column nor a column named like id, and when the region ids
of listw or nb are not the values of id. spatial_markov() warns in
that case, because weights in any other row order give units the wrong
neighbors without an error. Before griddy 0.2.0, rows were always matched
by position.
Examples
panel <- data.frame(
id = rep(1:4, each = 3),
year = rep(2020:2022, times = 4),
value = c(1, 2, 3, 2, 3, 4, 4, 3, 5, 5, 6, 7)
)
grid <- sf::st_sf(
id = 1:4,
geometry = sf::st_make_grid(
sf::st_bbox(c(xmin = 0, ymin = 0, xmax = 2, ymax = 2)),
n = c(2, 2)
)
) |>
dplyr::mutate(
nb = sfdep::st_contiguity(geometry),
wt = sfdep::st_weights(nb)
)
spatial <- spatial_markov(panel, id, year, value, geometry = grid, k = 2)
spatial
lag_intervals(spatial)
transition_matrix(spatial, "count", lag_class = "Q1")
Estimate Stationary Distribution
Description
Estimate Stationary Distribution
Usage
steady_state(x)
Arguments
x |
A |
Value
A numeric vector.
Examples
prob <- matrix(c(0.8, 0.2, 0.3, 0.7), nrow = 2, byrow = TRUE)
steady_state(prob)
Extract A Transition Matrix
Description
Extract A Transition Matrix
Usage
transition_matrix(x, type = c("probability", "count"), lag_class = NULL)
Arguments
x |
A |
type |
|
lag_class |
Optional lag class for |
Value
A matrix.
Examples
panel <- data.frame(
id = rep(letters[1:4], each = 3),
year = rep(2020:2022, times = 4),
value = c(8, 9, 11, 10, 12, 13, 15, 14, 16, 20, 22, 25)
)
markov <- panel |>
classify_dynamics(id, year, value, k = 3) |>
markov_dynamics(id, year, class)
transition_matrix(markov, "count")
US State Per-Capita Personal Income, 1929-2009
Description
Per-capita personal income for the 48 contiguous US states in nominal
dollars. Mirrors the canonical usjoin panel used in PySAL giddy and the
spatial Markov literature, so examples and validation in this package are
directly comparable to that reference work.
Usage
usjoin
Format
A tibble with 3,888 rows (48 states x 81 years) and 4 columns:
- name
State name.
- state_fips
State FIPS code, integer.
- year
Year, integer 1929 to 2009.
- income
Nominal per-capita personal income, integer USD.
Source
PySAL libpysal examples/us_income/usjoin.csv. The original series
is constructed from US Bureau of Economic Analysis state personal income
tables. See https://github.com/pysal/libpysal.
References
Rey, S. J. (2001). Spatial empirics for economic growth and convergence. Geographical Analysis, 33(3), 195-214.
Rey, S. J., Kang, W., & Wolf, L. (2016). The properties of tests for spatial effects in discrete Markov chain models of regional income distribution dynamics. Journal of Geographical Systems, 18(4), 377-398.
Examples
data(usjoin)
head(usjoin)
range(usjoin$year)