rank_mobility(
data,
variable,
first=None,
last=None,
*,
k=5,
top=10,
ascending=False,
title=None,
ref=None,
)
Rank mobility between two years: how much did the units change position?
Ranks the units in each year, compares the two rankings (rank correlations, the change of position of every unit) and follows the units between the quantile groups of the two years.
Parameters
| data |
|
A dataset id, or a panel with geo_id and year, e.g. from load |
required |
| variable |
str |
The numeric column to rank |
required |
| first |
|
The two years to compare (default: the first and last with data); only the units with a value in both are ranked |
None |
| last |
|
The two years to compare (default: the first and last with data); only the units with a value in both are ranked |
None |
| k |
int |
Number of quantile groups of the transition matrix (Q1: the lowest values) |
5 |
| top |
int |
Number of units in .climbers and .fallers |
10 |
| ascending |
bool |
False (default): rank 1 is the highest value; True: the lowest, for variables where less is better |
False |
| title |
str | None |
The title of the rank figure |
None |
Returns
|
result object |
A result with .df (one row per unit, ordered by the last rank: <variable>_<first>, <variable>_<last>, rank_<first>, rank_<last>, rank_change, positive when the unit climbed, and its quantile group_ in each year), .fig (the rank in the first year against the rank in the last, with the line of no change), .matrix (the shares of each group of the first year, rows, found in each group of the last year, columns; rows sum to one), .counts and .fig_matrix (its heatmap), .climbers and .fallers (the top units by rank change), .gt (both as one table), .spearman and .kendall (rank correlations of the two years), .mean_abs_change and .median_abs_change (positions moved by a unit), .share_same_group, .n_units, .n_climbed, .n_fell and .interpret(). |
Example
!pip install -q geo4bolivia
import geo4bolivia as geo
res = geo.rank_mobility("sdg", "index_sdg")
print(res.interpret())
res.fig
The 343 units with data in 2012 and 2024 were ranked by Sustainable Development Index, IDS (0-100) in each year (rank 1 = the highest value). The Spearman correlation of the two rankings is 0.843 and Kendall’s tau is 0.666: the ordering is broadly preserved, with visible reshuffling. Of all pairs of units, about 83% are in the same order in both years.
A unit moved 39.8 positions on average (median 29), 12% of the length of the ranking; two unrelated rankings would differ by about 114 positions. 163 units climbed, 177 fell and 3 kept their rank.
56% of the units are in the same quantile group in both years, out of 5 groups (about 20% would be if the two years were unrelated). The group that kept most of its units is Q5 (77% stayed) and the one that kept fewest is Q3 (37%); Q1 holds the lowest values and Q5 the highest.
The largest climb is Cruz de Machacamarca, from rank 315 to 90; the largest fall is San José de Chiquitos, from rank 42 to 283.
Ranks are relative: a unit can fall while its value rises, when the others rose more. Where values are close together, often in the middle of the distribution, small differences move a unit many positions.
These are associations, not causal effects. A causal reading needs a research design.