This page is for language models and coding agents that use
griddy on behalf of a user. It states the current API
exactly, including the functions added in 0.2.0, which may postdate your
training data. A machine-readable index of this site is at llms.txt.
Every page also has a Markdown version at the same address, with
.md in place of .html.
What this package is
griddy measures how values in a spatial panel move
between classes over time. It estimates classic and spatial Markov
chains, summarizes them, tests whether transition dynamics differ across
regimes, and measures rank mobility. Inputs are long data frames or
sf objects. Outputs are tidy tables, S3 objects with print
methods, and ggplot2 plots. The methods follow Rey (2001)
and Rey, Kang, and Wolf (2016). PySAL giddy is the Python
reference implementation; Coverage of
PySAL giddy maps each of its features to a griddy
function, a short recipe, or another package.
Input shape
One row per unit per period. id, time, and
value are bare column names (tidy evaluation), not
strings.
For spatial Markov, pass geometry: an sf
tibble with one row per unit, taken from a single period, carrying
nb and wt list-columns from
sfdep. Its rows are matched to units by id: through a
column with the same name as id, or through the column
named in geometry_id. listw and
nb arguments are accepted for older code; prefer
geometry.
geom <- geography |>
dplyr::mutate(
nb = sfdep::st_contiguity(geometry),
wt = sfdep::st_weights(nb)
)Functions
These are the exported functions in griddy 0.2.0, with
their exact signatures.
# Classification and chains
classify_dynamics(data, id, time, value,
method = c("pooled_quantile", "time_quantile", "fixed", "existing"),
k = 5, breaks = NULL, labels = NULL)
markov_dynamics(classes, id, time, state = class)
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)
transition_matrix(x, type = c("probability", "count"), lag_class = NULL)
class_intervals(x, ...)
lag_intervals(x)
# Chain summaries (x is a grd_markov object or a probability matrix)
steady_state(x)
sojourn_time(x) # new in 0.2.0
first_passage(x) # new in 0.2.0
mobility_index(x, # new in 0.2.0
measure = c("prais", "determinant", "eigen", "bartholomew1", "bartholomew2"),
initial = NULL)
# Inference (x is a grd_spatial_markov object or a list of count matrices)
homogeneity_test(x, ...) # new in 0.2.0
# Rank mobility and plots
rank_mobility(data, id, time, value,
compare = c("endpoint", "adjacent"), descending = TRUE)
plot_transition_matrix(x)
plot_spatial_markov(x)
plot_rank_mobility(x)What each one returns:
-
classify_dynamics(): the input with an orderedclasscolumn, as agrd_classesobject. -
markov_dynamics(): agrd_markovobject withtransitions(one row per unit and step) andmatrix(one row per cell). -
spatial_markov(): agrd_spatial_markovobject.transitionsaddslag_classandspatial_lag.matrixis keyed bylag_class,from_state, andto_state. -
transition_matrix(): a base matrix. Withlag_class, the conditional matrix for that spatial-lag class. -
steady_state(),sojourn_time(): named numeric vectors.first_passage(): a matrix. -
mobility_index(): a named vector with all five measures by default. -
homogeneity_test(): a tibble with columnstest,statistic,df, andp_value, and rowschi2,likelihood_ratio, andkullback. -
rank_mobility(): the input with ranks added. Endpoint comparisons addstart_time,end_time,start_rank, andend_rank; adjacent comparisons addto_timeandto_rank. Both addrank_changeandabs_rank_change.
A complete workflow
The bundled usjoin panel has per-capita income for 48 US
states from 1929 to 2009.
library(griddy)
library(dplyr)
library(sf)
library(sfdep)
data(usjoin)
data(us_states, package = "spData")
geom <- us_states |>
filter(NAME %in% usjoin$name) |>
mutate(nb = st_contiguity(geometry), wt = st_weights(nb))
relative <- usjoin |>
group_by(year) |>
mutate(rel_income = income / mean(income)) |>
ungroup()
chain <- relative |>
classify_dynamics(name, year, rel_income, k = 5) |>
markov_dynamics(name, year, class)
sojourn_time(chain)
#> Q1 Q2 Q3 Q4 Q5
#> 11.712121 5.765152 4.860759 6.603448 16.083333
mobility_index(chain, "prais")
#> prais
#> 0.1695447
spatial <- spatial_markov(
relative, name, year, rel_income,
geometry = geom, geometry_id = NAME, k = 5
)
homogeneity_test(spatial)
#> # A tibble: 3 × 4
#> test statistic df p_value
#> <chr> <dbl> <int> <dbl>
#> 1 chi2 201. 60 4.72e-17
#> 2 likelihood_ratio 171. 60 1.49e-12
#> 3 kullback 171. 60 1.49e-12Pitfalls
-
Give the weights ids.
spatial_markov()matches the rows ofgeometryto units through a column with the same name asid, or through the column named ingeometry_id, so row order does not matter. Forlistwornb, it uses the region ids when they are the values ofid; set them withspdep::poly2nb(x, row.names = x$id). Without usable ids it matches rows by position, in ascending order ofid, and warns. Do not ignore that warning: weights in any other row order give units the wrong neighbors. Before 0.2.0, rows were always matched by position, with no warning. -
Build
nbandwtlast. They refer to rows ofgeometryby position. Filtering or reorderinggeometryafter building them gives units the wrong neighbors, and matching by id cannot detect it. -
Balance the panel for spatial Markov. Every unit
must appear in every period, and the weights must cover exactly the
panel’s units;
spatial_markov()stops otherwise. -
Use relative values for chain summaries. With
pooled breaks on a growing nominal series, units drift into the top
class and stay there. The chain becomes absorbing:
steady_state()puts all mass in the top class,sojourn_time()returnsInf, andfirst_passage()stops with an error. Divide by the period mean or median before classifying, as in the workflow above. -
first_passage()needs an irreducible chain: every class reachable from every other. -
Empty rows are
NA. A class with no departures hasNAprobabilities, not zeros, and zero counts. Conditional matrices for spatial-lag classes often have such rows. The coverage article shows how to fill them the way giddy’sfill_empty_classesdoes. -
Lag classes are measured at the start of each
transition, from the spatial lag in
from_time. -
kullbackequalslikelihood_ratio. With origin-state margins the two statistics are identical, and both use the Bickenbach and Bode (2003) degrees of freedom. Do not report them as independent evidence. -
Pass counts, not probabilities, to
homogeneity_test(). For regimes other than spatial-lag classes, pass a list of count matrices with the same dimensions. -
Rank direction. With
descending = TRUE(the default), the largest value has rank 1, and a positiverank_changemeans a unit moved up.
What griddy does not do
- Full and geographic rank Markov chains, the Kendall’s tau family including LIMA, and rose diagrams are planned. The coverage article has recipes for the rank chains with existing functions.
- Sequence analysis is out of scope; use
TraMineR. - Communicating, recurrent, and absorbing class analysis of reducible
chains is out of scope; use
markovchain.
References
Bickenbach, F., and Bode, E. (2003). Evaluating the Markov property in studies of economic convergence. International Regional Science Review, 26(3), 363–392. https://doi.org/10.1177/0160017603253789
Rey, S. J. (2001). Spatial empirics for economic growth and convergence. Geographical Analysis, 33(3), 195–214. https://doi.org/10.1111/j.1538-4632.2001.tb00444.x
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. https://doi.org/10.1007/s10109-016-0234-x