This folder is Part 5 of Module 04. A county with few cases can post a rate that looks extreme only because the count is small. Eleven notebooks replace that raw rate with a smoothed one and say how much the smoothing moved it. The running example is diabetes in 217 northeastern counties (diabetes_northeast.shp), with a simulation whose true risk is known.
Disease Mapping and Bayesian Smoothing. This notebook. Says why the raw rate misleads and lists the tools that follow.
Motivation: the Small-Area Estimation Problem. Shows, with the county numbers, that small populations make noisy rates.
Data and Making the Problem Visible. Builds the county file, a simulation with known truth, and the cancer point data.
Raw Estimates and Naïve Inference. Computes the crude rate, the standardised mortality ratio, and an exact Poisson interval.
Empirical Bayes Smoothing. Shrinks rates toward a global mean, a normal mean, and a mean borrowed from neighbours.
Spatial Bayesian Models: BYM and BYM2. Fits the Besag-York-Mollié model, with a spatial effect and an unstructured effect, plus covariates and the chance of exceeding a threshold.
MCMC Implementation. Runs the two sampling backends, checks the diagnostics, and shows what under-sampling does.
Comparing and Evaluating Smoothing Methods. Scores the smoothed maps against the known truth and asks what smoothing does to apparent clusters.
Shrinkage Assessment and Interpretation. Finds which counties move the most, and why.
Reporting, Interpretation and Ethics. Covers uncertainty, suppression of small counts, and the ecological fallacy.
Hand-offs and Exercises. Carries the maps forward to Parts 6 and 9, and works five problems.
Library:geolattice.spatial_statistics.bayes
Overview
Parts 2 and 4 both worked with Dia_pct, diagnosed diabetes prevalence, as if every county’s reported rate were equally trustworthy. It is not: a rate computed from a population of 5,000 is far noisier than the same rate computed from a population of 2 million, purely from sampling variability, and Part 4, Chapter 4.7 already previewed why that matters for cluster detection specifically. Part 5 is the full treatment: why small-area rates are unstable, and the two families of tools, empirical Bayes and fully Bayesian spatial smoothing, that fix it by borrowing strength, either across all areas or across neighbours.
Step
What we do
Objectives
State precisely what this Part will build and validate
Package map
expected_counts, smr, the eb_* family, BYM: what each does and when to reach for it
MCMC backends
The dependency-free built-in sampler (always available) vs. PyMC/NUTS (optional, faster mixing, needed for BYM2)
First look
One raw rate, one smoothed rate, side by side, before any of the machinery is explained
Locate the repository root, import the spatial-statistics helpers, and point DATA at the example files.
Installation. This notebook needs installation option 3 (core, spatial analysis and spatial statistics): pip install "geolattice[statistics]". See “Installation options” in the GeoLattice README.
Code
import sysfrom pathlib import Pathimport numpy as npimport pandas as pdimport geopandas as gpdimport matplotlib.pyplot as pltROOT = Path.cwd().resolve()for candidate in [ROOT, *ROOT.parents]:if (candidate /"pyproject.toml").is_file() and (candidate /"Data").is_dir(): ROOT = candidatebreakelse:raiseFileNotFoundError("Could not find the GeoLattice repository root.")sys.path.insert(0, str(ROOT))DATA = ROOT /"Data"/"00_shared"/"data_spatial_stat"import geolattice.spatial_statisticsfrom geolattice.viz import apply_map_stylefrom geolattice.spatial_statistics.weights import Queenfrom geolattice.spatial_statistics.bayes import expected_counts, smr, eb_gamma_poisson, BYM, pymc_availableapply_map_style()C = {"blue": "#2b6cb0", "red": "#c0392b", "green": "#2f855a", "orange": "#dd6b20","grey": "#718096", "purple": "#6b46c1", "teal": "#2c7a7b"}print(f"geolattice.spatial_statistics : version {geolattice.spatial_statistics.__version__}")print(f"PyMC available: {pymc_available()} (this Part runs either way)")
geolattice.spatial_statistics : version 0.3.49
PyMC available: True (this Part runs either way)
Objectives
By the end of Part 5:
Understand why small-area rates are unstable: a direct consequence of Poisson sampling variance (Chapter 5.2), made visible with a funnel plot rather than a paragraph of assertion.
Implement empirical Bayes smoothing: global (gamma-Poisson), normal/James-Stein, and spatial (local neighbourhood) variants (Chapter 5.5), and know when each is the more sensible choice.
Understand and fit the BYM model: the standard fully Bayesian spatial disease-mapping model, with both its built-in dependency-free sampler and, optionally, PyMC/NUTS (Chapters 5.6-5.7).
Compare raw and smoothed estimates, both visually and against a known simulated truth (Chapter 5.8).
Interpret shrinkage correctly: which areas move, by how much, and toward what (Chapter 5.9).
BYM’s backend='gibbs' (the default) is a blocked Gibbs/Metropolis sampler with no extra dependencies, every notebook in this Part runs end to end without PyMC. backend='pymc' (or 'auto') uses NUTS, which mixes faster and is required for the model='bym2' parametrisation; PyMC is installed in this environment (confirmed above), so Chapters 5.6-5.7 use it directly for the comparison it enables, with every PyMC-only cell clearly marked.
A First Look
Before any theory: one variable, one raw rate, one smoothed rate, on the familiar 217 Northeast counties.
Look at the smallest counties (top of the table, sorted by population): their raw SMR and EB-smoothed value differ the most: exactly the counties Chapter 5.2 explains are noisiest to begin with. The largest counties (bottom of the table, not shown here) barely move at all. This asymmetric response: small areas shrink more, large areas barely change: is the entire mechanism Part 5 develops in full over the next ten chapters.
This chapter set the objectives and map for Part 5.
What we did
Stated the six concrete objectives this Part works toward.
Tabulated every function geolattice.spatial_statistics.bayes offers and which chapter develops it.
Confirmed both MCMC backends (built-in Gibbs, optional PyMC/NUTS) are available in this environment.
Took a first, informal look at raw vs. EB-smoothed rates, and confirmed the smallest counties move the most.
Practical takeaways
Every notebook in this Part is written to run with the dependency-free built-in sampler; where PyMC adds real value (BYM2, faster mixing, a cross-backend validation check) those cells are used directly here since PyMC is available, and are clearly marked so they can be skipped in an environment without it.
The core empirical fact motivating this whole Part, small areas move more under smoothing, is already visible in one summary statistic, before any of the underlying theory is developed.
Next steps
Chapter 5.2 (Motivation: the Small-Area Estimation Problem) makes the mechanism behind that asymmetry explicit: Poisson sampling variance, and the funnel-plot picture of exactly how it depends on population size.
Lawson, A. B. (2018). Bayesian Disease Mapping: Hierarchical Modeling in Spatial Epidemiology (3rd ed.). CRC Press.
The standard modern reference for this entire Part
Banerjee, S., Carlin, B. P. & Gelfand, A. E. (2014). Hierarchical Modeling and Analysis for Spatial Data (2nd ed.). CRC Press.
Rigorous treatment of the BYM model and its Bayesian foundations
Waller, L. A. & Gotway, C. A. (2004). Applied Spatial Statistics for Public Health Data. Wiley.
Accessible introduction to small-area estimation for health data
Key papers
Paper
Contribution
Besag, J., York, J. & Mollié, A. (1991). Bayesian image restoration, with two applications in spatial statistics. Annals of the Institute of Statistical Mathematics, 43, 1-20.
Origin of the BYM model
Clayton, D. & Kaldor, J. (1987). Empirical Bayes estimates of age-standardized relative risks for use in disease mapping. Biometrics, 43, 671-681.
Origin of the empirical Bayes approach to disease mapping