!pip install -q "geo4bolivia[mgwr]"Analyze
This page takes one question through every family of analysis functions: did sustainable development become more equal across Bolivia’s municipalities between about 2012 and about 2024, and what role does space play? Each step is one or two calls, read in a few sentences, with a link to the article that explains the method in full. The page is a tour, not a study: the case study does the same work at full length.
Every function returns a result object. Displayed, it shows its main figure and a reading of the numbers; .fig, .df, .gt and .interpret() give the parts one by one (Get started explains them).
The [mgwr] extra is needed only for the last step.
The data
The dataset sdg holds the Sustainable Development Index (index_sdg, called IDS, from 0 to 100) of the SDG Atlas 2026 for the 343 municipalities, in two periods stored as the years 2012 and 2024, with the census population and the urbanization rate.
import geo4bolivia as geo
ids = geo.load("sdg", groups=["department"])
cols = ["variable", "year", "count", "mean", "std", "min", "max"]
geo.describe(ids, variables=["index_sdg", "urbanization"])[cols]| variable | year | count | mean | std | min | max | |
|---|---|---|---|---|---|---|---|
| 0 | index_sdg | 2012 | 343 | 50.123032 | 5.819148 | 37.2 | 70.5 |
| 1 | index_sdg | 2024 | 343 | 60.081341 | 5.263740 | 47.3 | 77.3 |
| 2 | urbanization | 2012 | 0 | NaN | NaN | NaN | NaN |
| 3 | urbanization | 2024 | 343 | 26.580175 | 31.333405 | 0.0 | 100.0 |
The index rose by about ten points on average, from 50.1 to 60.1. urbanization exists only for 2024, so the models further down describe that year alone.
“2012” and “2024” are nominal: each of the 81 indicators behind the index was measured in its own years, near those two (geo.reference_years("sdg03") lists them for one goal). Rates “per year” below divide by the twelve years between the nominal dates and are approximate.
1. Spatial dependence
Question: do municipalities with similar values lie next to each other? Global Moran’s I measures it for the whole country: it is close to zero when values are placed at random, and positive when neighbours are alike. year="all" computes it in every year.
geo.moran(ids, "index_sdg", year="all").df| year | moran_i | z_sim | p_sim | n_obs | |
|---|---|---|---|---|---|
| 0 | 2012 | 0.432470 | 12.748494 | 0.001 | 343 |
| 1 | 2024 | 0.467933 | 13.782523 | 0.001 | 343 |
Moran’s I is 0.43 in 2012 and 0.47 in 2024. In both periods none of the 999 random reshufflings of the values over the map gave a value as high (p_sim = 0.001, the smallest the test can return), so the index is clearly clustered, and no less in 2024 than in 2012.
Local Moran statistics (LISA) show where. Each municipality is compared with its neighbours and, when the pattern is unlikely under random placement, assigned to a type: High-High (a high value among high neighbours), Low-Low, or one of the two kinds of outlier.
lisa = geo.lisa(ids, "index_sdg", year=2024)
lisaLocal Moran statistics (LISA) locate where Sustainable Development Index, IDS (0-100) in 2024 clusters or stands out, under queen contiguity (1 island(s) attached to nearest neighbor), row-standardized, n=343. The accompanying global Moran’s I is 0.468 (pseudo p = 0.001), consistent with overall clustering of similar values.
At the 0.05 significance level, 116 of 343 units show significant local association: 60 High-High hot spots (high values surrounded by high neighbors) and 45 Low-Low cold spots (low surrounded by low) mark clustering, while 6 High-Low and 5 Low-High units are spatial outliers that break with their surroundings. The remaining 227 units are not significant — their local pattern is compatible with randomness.
LISA pseudo p-values are computed unit by unit without a multiple-testing adjustment, so treat borderline clusters cautiously and read the map as descriptive of where dependence concentrates.
These are associations, not causal effects. A causal reading needs a research design.
Of the 343 municipalities, 60 are High-High and 45 Low-Low; 11 are outliers and 227 are not significant. The next table counts them by department.
where = lisa.df.merge(ids.loc[ids["year"] == 2024, ["geo_id", "department"]])
where.groupby(["department", "cluster"]).size().unstack(fill_value=0)| cluster | High-High | High-Low | Low-High | Low-Low | Not significant |
|---|---|---|---|---|---|
| department | |||||
| Beni | 0 | 2 | 0 | 2 | 16 |
| Chuquisaca | 3 | 0 | 2 | 1 | 23 |
| Cochabamba | 14 | 1 | 0 | 4 | 29 |
| La Paz | 12 | 0 | 0 | 13 | 62 |
| Oruro | 0 | 2 | 0 | 8 | 25 |
| Pando | 1 | 0 | 0 | 0 | 14 |
| Potosí | 3 | 1 | 0 | 13 | 25 |
| Santa Cruz | 17 | 0 | 3 | 4 | 32 |
| Tarija | 10 | 0 | 0 | 0 | 1 |
The High-High municipalities are mostly in Santa Cruz (17), Cochabamba (14), La Paz (12) and Tarija (10, of its 11 municipalities); the Low-Low ones mostly in Potosí (13), La Paz (13) and Oruro (8). The map locates clusters; it does not say why they are there, and “high” and “low” are relative to the national mean of that year.
→ Maps and spatial dependence: class schemes, spatial weights, the permutation test, and how much the result depends on the definition of “neighbour”.
2. Convergence
Question: did the municipalities that started lower rise faster? β-convergence regresses each municipality’s average yearly growth of the index on the logarithm of its initial level. A negative slope means that lower starting points go with faster growth.
beta = geo.beta_convergence(ids, "index_sdg")
betaAcross 343 units, the growth of Sustainable Development Index, IDS (0-100) over a 12-year window is associated with its initial log level with a total slope of -0.0287 (SE 0.00171), statistically significant at the 1% level. The slope is negative — the β-convergence pattern: units that started lower tended to grow faster, narrowing initial gaps. That slope implies a convergence speed of λ = 0.0351 per year and a half-life of about 19.7 years — the time for half of an initial gap to close at this pace.
These are associations, not causal effects. A causal reading needs a research design.
The slope is −0.029 (standard error 0.002), and the initial level accounts for 44% of the differences in growth (R² = 0.44). At that pace half of a gap between two municipalities would close in about 20 years. Two cautions: an index with a ceiling at 100 leaves more room to rise at the bottom, and a negative slope does not by itself mean that the spread of the index fell (step 4 checks that directly).
The other two convergence functions need more years than the Atlas has:
geo.sigma_convergenceestimates the trend of the dispersion over time, so it needs at least three years.geo.convergence_clubsfollows each unit’s path over time to sort units into groups that converge among themselves, so it needs at least six.
Two datasets have long yearly series: pop (gridded population, 2001–2020) and ntl (night-time lights per capita, 2012–2020), both for 333 municipalities. Called on a dataset that is too short, either function says so and names these two.
→ Convergence: β, σ and clubs: all three methods, the spatial versions of β-convergence, and the long panels.
3. Transitions and mobility
Question: did the clusters of step 1 persist, and how much did municipalities change position? geo.lisa_transitions runs the LISA analysis in both periods, on the same units and neighbours, and counts the moves between types.
moves = geo.lisa_transitions(ids, "index_sdg")
movesLocal Moran statistics (LISA) of Sustainable Development Index, IDS (0-100) were computed in 2012 and in 2024 for the same 343 units, under queen contiguity (1 island(s) attached to nearest neighbor), row-standardized, n=343, and each unit was assigned to a type at the 0.05 level. Global Moran’s I is 0.432 in 2012 (pseudo p = 0.001) and 0.468 in 2024 (pseudo p = 0.001): the overall clustering of similar values changed little.
275 of 343 units (80%) are of the same type in both years and 68 changed type. Of the 104 units in a significant cluster or outlier in 2012, 71 kept their type, 23 are no longer significant in 2024 and 10 switched to another type. 35 units that were not significant in 2012 belong to a cluster or are an outlier in 2024.
Of the 46 High-High hot spots (high values surrounded by high values) of 2012, 39 are still High-High in 2024, a year with 60 of them. Of the 44 Low-Low cold spots (low values surrounded by low values) of 2012, 30 are still Low-Low in 2024, a year with 45 of them. The most common change is from Not significant to High-High (17 units).
Read the changes with care. A unit’s type depends on whether its pseudo p-value is below the threshold, computed unit by unit without a multiple-testing adjustment, so moves to and from Not significant can come from borderline p-values. High and low are relative to the mean of each year, and a unit can change type because its neighbours’ values changed while its own did not.
These are associations, not causal effects. A causal reading needs a research design.
Rows are the type in 2012 and columns the type in 2024. 275 of the 343 municipalities (80%) have the same type in both periods. Of the 46 High-High municipalities of 2012, 39 are still High-High; of the 44 Low-Low, 30 are still Low-Low and 13 are no longer significant. No municipality moved between High-High and Low-Low. Moves to and from “Not significant” can come from p-values near the threshold, so read them with care.
geo.rank_mobility ranks the municipalities in each period (rank 1 is the highest index) and compares the two rankings.
ranks = geo.rank_mobility(ids, "index_sdg")
ranksThe 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.
Each point is a municipality; on the diagonal it kept its rank. The Spearman correlation of the two rankings is 0.84 (1 would be an unchanged order), a municipality moved 40 positions on average (median 29), and 56% of them stayed in the same fifth of the ranking. The top fifth is the most stable (77% stayed). A rank is relative: a municipality can lose positions while its index rises, when others rose more.
→ Transitions and mobility: Markov and spatial Markov transition matrices, which need the [dynamics] extra, and these two functions in detail.
4. Inequality
Question: how unequal are the municipalities, and is the inequality between departments or inside them? geo.inequality computes the Gini and Theil indices in each year (both are 0 when every unit has the same value) and draws the Lorenz curve.
ineq = geo.inequality(ids, "index_sdg")
ineq.gt| Inequality: Sustainable Development Index, IDS (0-100) | ||||
| 2012 and 2024, 343 units | ||||
| 2012 | 2024 | Change | Change (%) | |
|---|---|---|---|---|
| Gini index | 0.06501 | 0.04934 | -0.01567 | -24.1 |
| Theil index | 0.006629 | 0.003806 | -0.002824 | -42.6 |
| With two years the measures can be compared, but their trend cannot be tested: that needs three or more years. | ||||
Both measures fell: the Gini index from 0.065 to 0.049 and the Theil index from 0.0066 to 0.0038. With two periods the two values can be compared, but a trend cannot be tested. The values are small because the index is bounded and most municipalities lie within a few points of each other; they are not comparable with the Gini of income or population.
Theil’s index can be split exactly into inequality between groups (differences among department averages) and within them.
theil = geo.theil_decomposition(ids, "index_sdg", by="department")
theilThe Theil index of Sustainable Development Index, IDS (0-100) splits additively into inequality between the 9 department groups and inequality within them (between + within = total, exactly).
In the latest year (2024), the total Theil index is 0.00381: about 15% of it lies between department groups and 85% within them — most inequality plays out among units inside the same department.
Over the window, the between-group share fell from 20% to 15% — group means are pulling closer together relative to the differences inside groups.
These are associations, not causal effects. A causal reading needs a research design.
Differences between the nine departments account for 20% of the inequality in 2012 and 15% in 2024. Most of the inequality between municipalities is found inside departments, and the part between departments shrank faster than the part within.
→ Regional inequality: what each measure is sensitive to, the Lorenz curve, trends on long panels, the spatial Gini, and what an inequality index of a bounded indicator can and cannot say.
5. Models
Question: what goes with a higher index, and is the relationship the same once neighbours are taken into account? geo.regression fits ordinary least squares, with several specifications side by side. Here the index of 2024 is regressed on the urbanization rate, first alone and then with population and department fixed effects (a separate intercept for each department).
reg = geo.regression(ids, "index_sdg",
[["urbanization"], ["urbanization", "population"]],
fixed_effects=[None, "department"])
reg.gt| Regression of Sustainable Development Index, IDS (0-100) | ||
| Ordinary least squares, 2024 | ||
| Sustainable Development Index, IDS (0-100) | ||
|---|---|---|
| (1) | (2) | |
| Urbanization rate (% of population) | 0.094*** | 0.087*** |
| (0.008) | (0.008) | |
| Population (census) | 7.05e-06*** | |
| (2.22e-06) | ||
| Intercept | 57.582*** | |
| (0.313) | ||
| Department fixed effects | No | Yes |
| Observations | 343 | 343 |
| R² | 0.313 | 0.449 |
| Adjusted R² | 0.311 | 0.432 |
| *** p<0.01, ** p<0.05, * p<0.10. Standard errors in parentheses: heteroskedasticity-robust (HC1). | ||
A municipality whose urbanization rate is ten percentage points higher has an index about 0.9 points higher (coefficient 0.094). The coefficient barely moves (0.087) when municipalities are compared only with others of the same department and population is held constant. Urbanization alone accounts for 31% of the variation of the index.
Least squares assumes that the errors of neighbouring municipalities are unrelated. geo.spatial_diagnostics tests that on the residuals and points to the spatial model that fits the pattern it finds.
diag = geo.spatial_diagnostics(ids, "index_sdg", "urbanization")
diag| Spatial dependence diagnostics for Sustainable Development Index, IDS (0-100) | |||
| OLS residual tests | W: queen contiguity (1 island(s) attached to nearest neighbor), row-standardized, n=343 | |||
| Statistic | df | p-value | |
|---|---|---|---|
| Moran's I (residuals) | 0.4700 | -- | 0.0000 |
| LM lag | 168.5305 | 1 | 0.0000 |
| LM error | 187.1010 | 1 | 0.0000 |
| Robust LM lag | 4.5047 | 1 | 0.0338 |
| Robust LM error | 23.0751 | 1 | 0.0000 |
| LM SARMA | 191.6057 | 2 | 0.0000 |
| Anselin-Florax recommendation: error | |||
Moran’s I on the OLS residuals is 0.47 (statistically significant at the 1% level) under queen contiguity (1 island(s) attached to nearest neighbor), row-standardized, n=343 — the residuals cluster in space, so the non-spatial model leaves spatial structure on the table.
At α = 0.05, the tests that reject no-spatial-dependence are: LM lag, LM error, robust LM lag, robust LM error. The robust forms are the decisive ones — each tests one dependence channel while allowing for the other.
Recommendation: error — Both robust LM tests reject at alpha = 0.05 (robust LM lag = 4.50, robust LM error = 23.08), so the larger robust statistic wins and points to the error model. With both channels active, consider durbin (SDM) - it nests both the lag and the spatially-lagged-X channels and lets the data separate them.
These are associations, not causal effects. A causal reading needs a research design.
The residuals are as clustered as the index itself (Moran’s I = 0.47), so urbanization does not account for the spatial pattern of step 1. Both robust tests reject, and the one for a spatial error model is the larger (23.1 against 4.5), which is the model the function recommends.
sem = geo.spatial_model(ids, "index_sdg", "urbanization", model="error")
sem| Spatial error (sem) model of Sustainable Development Index, IDS (0-100) | ||||
| ML estimates | n = 343 | R² = 0.313 | AIC = 1849.4 | ||||
| Estimate | Std. Error | z | p | |
|---|---|---|---|---|
| CONSTANT | 57.9036*** | (0.5841) | 99.14 | 0.0000 |
| Urbanization rate (% of population) | 0.0815*** | (0.0066) | 12.26 | 0.0000 |
| lambda | 0.6749*** | (0.0488) | 13.82 | 0.0000 |
| *** p<0.01, ** p<0.05, * p<0.10 (z-based) | ||||
| W: queen contiguity (1 island(s) attached to nearest neighbor), row-standardized, n=343 | ||||
A spatial error (SEM) model of Sustainable Development Index, IDS (0-100) was estimated by ML on 343 units (weights: queen contiguity (1 island(s) attached to nearest neighbor), row-standardized, n=343).
The spatial error parameter is λ = 0.675 (statistically significant at the 1% level): unmodeled shocks are spatially correlated across neighbors, which matters for standard errors more than for the coefficients themselves.
This specification has no direct/indirect impact decomposition; coefficients are read directly from the table.
Model fit: (pseudo-)R² = 0.313, AIC = 1849 (compare AIC across specifications estimated on the same sample).
These are associations, not causal effects. A causal reading needs a research design.
In the spatial error model the coefficient of urbanization is 0.081, a little lower than the least-squares one, and the spatial parameter λ is 0.67: what the model leaves unexplained is strongly correlated between neighbours. The association with urbanization holds; the neighbourhood pattern has another source, which this model does not identify.
→ Regression, panel and spatial models: fixed effects and clustered errors, panel estimators, and the spatial lag and Durbin models with their direct and spillover estimates.
6. Spatial heterogeneity
Question: is the association with urbanization the same everywhere? The models of step 5 estimate one coefficient for the country. Multiscale geographically weighted regression (MGWR) fits a regression at every municipality, giving more weight to the nearest ones, so the coefficient becomes a surface that can be mapped. It also chooses how many neighbours to use (the bandwidth): a small bandwidth means an association that changes over short distances. The search takes about half a minute.
mg = geo.mgwr(ids, "index_sdg", "urbanization")
mg.figs["urbanization"]local = mg.df.merge(ids.loc[ids["year"] == 2024, ["geo_id", "department"]])
print(mg.bw, "| R2 =", round(mg.r2, 3))
local.groupby("department")["urbanization_coef"].mean().round(2).sort_values(){'const': 44.0, 'urbanization': 90.0} | R2 = 0.629
department
Pando 0.32
Beni 0.34
Santa Cruz 0.38
La Paz 0.52
Tarija 0.55
Oruro 0.62
Chuquisaca 0.73
Cochabamba 0.76
Potosí 0.79
Name: urbanization_coef, dtype: float64
MGWR standardizes the variables, so the coefficients are in standard deviations. The local coefficient of urbanization is positive everywhere, from 0.21 to 1.01, and significant in 326 of the 343 municipalities. It is estimated from the 90 nearest municipalities at each place, about a quarter of the country: a regional rather than a national pattern. On average it is highest in Potosí, Cochabamba and Chuquisaca (0.73 to 0.79) and lowest in Pando, Beni and Santa Cruz (0.32 to 0.38).
→ Spatial heterogeneity: MGWR: bandwidths, the local tests, several regressors, and how to read the coefficient maps.
What the tour found
All of these are associations in the data; none shows a cause.
- More equal, by the measures used here. Between about 2012 and about 2024 the index rose in 342 of the 343 municipalities, municipalities that started lower rose faster (β = −0.029), and the Gini and Theil indices of the index fell.
- Clustered, and no less so. Moran’s I is 0.43 in 2012 and 0.47 in 2024. Four of five municipalities have the same LISA type in both periods, and none moved between a High-High and a Low-Low cluster.
- A reshuffled middle, a stable top. The ranking changed (rank correlation 0.84), mostly in its middle; 77% of the top fifth stayed there.
- Mostly within departments. Differences between department averages are 15% of the inequality in 2024, down from 20%.
- Urbanization goes with a higher index, everywhere but not equally. The coefficient is positive in the national models and in every local one, and larger in Potosí, Cochabamba and Chuquisaca than in Santa Cruz, Beni and Pando. It leaves the spatial clustering of the index unaccounted for.
So the gaps between municipalities narrowed while the geography of high and low values stayed largely in place. What keeps it in place is a question these descriptive tools raise and cannot answer.
Where next
- Case study: the Sustainable Development Index: the same question, at full length.
- Articles: one article per method.
- Explore: the first look at a variable, before any of this.
- Function reference: every function, with an example you can paste into Colab.