Machine Learning versus Bayesian Spatial Models for Small-Area Tuberculosis Prediction: A Comparative Study in Metropolitan Santiago, Chile
Draft in preparation
Reproducible R pipeline for district-level TB rate prediction in Metropolitan Santiago, Chile, comparing negative binomial spatial lag, tree-based ML (RF-S, BART, XGBoost), BYM2 (R-INLA), and NNLS stacking.
Location of the study area in Chile (panel A), district-level TB case counts (panel B), and TB rate per 10,000 inhabitants (panel C).
Choropleth maps and observed-vs-predicted scatter plots for NB-lag, BART, RF-S, XGB, and BYM2 (pooled OOF evaluation).
Comparison of MAE, RMSE, BIAS, R², CE, and IA across NB-lag, BART, RF-S, XGB, and BYM2.
OOF predicted rates and residuals for the NNLS stacking meta-learner, with fit metrics compared against XGB and RF-S.
FONIS Nº SA24I0203
| Role | Name | Affiliation | Contact |
|---|---|---|---|
| Co-author / repository manager | José Daniel Conejeros | ISI Foundation, Turin; SENTINET | jdconejeros@uc.cl |
| Corresponding author | Salvador Ayala | Centro de Epidemiología y Políticas de Salud, Facultad de Medicina Clínica Alemana, Universidad del Desarrollo, Santiago | salvadorayala@udd.cl |
Tuberculosis (TB) burden is spatially heterogeneous. Classical disease-mapping approaches (Poisson/NB regression, CAR/BYM models) explicitly model spatial structure but may underfit complex socio-environmental gradients. Machine learning (ML) algorithms capture non-linearities but often treat space indirectly. This repository implements a reproducible comparative pipeline for small-area TB rate prediction in the Metropolitan Region of Santiago (RM), Chile, combining:
- Frequentist spatial regression (NB with spatial lag)
- Tree-based ML (RF-S, BART, XGBoost)
- Bayesian hierarchical spatial models (BYM2 via R-INLA)
- Ensemble stacking (NNLS meta-learner on OOF base-model predictions)
- 343 urban districts with polygon geometry
- Outcome:
casos_tb; rate metrictasa_tb_10k(per 10,000 population) - Queen contiguity spatial weights; train-only spatial lags in cross-validation
- Build a harmonized district-level analytic dataset (census, health access, climate).
- Fit and cross-validate multiple spatial and ML models on a common covariate pool.
- Evaluate predictive performance (MAE, RMSE, BIAS, CE, IA, R²) and residual spatial structure (Moran's I, LISA).
- Produce publication-ready maps, diagnostic panels, variable-importance figures, and tables.
Full statistical and model specifications: 02_code/03_models/README.md.
TB_ML_FONIS/
├── 01_data/
│ ├── raw/ Census, health facilities, climate extracts
│ ├── interim/ Queen spatial weights (nb, lw)
│ └── analysis/ District analytic table (sf + covariates)
├── 02_code/
│ ├── 00_setup/ Paths, map utilities, dependencies
│ ├── 01_prepare/ Dataset construction (01a–01d)
│ ├── 02_descriptives/ Exploratory maps and correlation panels
│ ├── 03_models/ Model fitting (04a–04j) + figures/tables (05)
│ └── python/ Earth Engine climate extraction
├── 03_output/
│ ├── maps/ Descriptive choropleths
│ ├── figures/ Descriptive panels
│ └── models/ Model RDS, metrics CSV, Figures/, Tables/
├── 04_Paper/ Manuscript and supplementary files
└── README.md This file
- R ≥ 4.3 (4.5+ recommended)
- Optional: Python 3 + Earth Engine API for climate re-extraction
Install R dependencies:
Rscript 02_code/00_setup/install_spatial_ml_dependencies.RINLA and fastshap require additional setup; see install_spatial_ml_dependencies.R.
1. Build analytic dataset
Rscript 02_code/01_prepare/01a_prepare_census_covariates.R
Rscript 02_code/01_prepare/01b_prepare_health_covariates.R
Rscript 02_code/01_prepare/01c_prepare_climate_covariates.R
Rscript 02_code/01_prepare/01d_prepare_analysis_dataset.R2. Descriptive analysis
Rscript 02_code/02_descriptives/02_descriptive_analysis.R3. Spatial models (run order in 02_code/03_models/README.md)
Rscript 02_code/03_models/04a_variable_selection_regularization.R
Rscript 02_code/03_models/04b_nb_lag_model.R
Rscript 02_code/03_models/04c_rf_s_model.R
Rscript 02_code/03_models/04d_bart_car_model.R
Rscript 02_code/03_models/04e_xgb_model.R
Rscript 02_code/03_models/04f_inla_model.R
Rscript 02_code/03_models/04g_metamodel_stacking.R
Rscript 02_code/03_models/04h_model_parametrization_table.R
Rscript 02_code/03_models/04j_spatial_residual_diagnostics.R
Rscript 02_code/03_models/05_visualize_spatial_models.R4. Climate extraction (optional refresh)
python 02_code/python/06_temperature_ndvi_extraction.py --dry-run
python 02_code/python/06_temperature_ndvi_extraction.py| Stage | Time |
|---|---|
| Data preparation (01) | < 5 min |
| Descriptives (02) | < 10 min |
| Models 04a–04d, 04g–04j | < 30 min |
| XGB (04e) | 10–30 min |
| BYM2 (04f) | 5–15 min |
| Publication figures (05) | 10–20 min |
| Location | Content |
|---|---|
01_data/analysis/tb_analisis_distritos_rm.rds |
Analysis dataset |
03_output/models/model_*.rds |
Fitted models + OOF predictions |
03_output/models/metrics_all_models.csv |
Cross-model metrics |
03_output/models/Figures/ |
Publication figure panels |
03_output/models/Tables/ |
Publication tables (CSV + XLSX) |
03_output/maps/ |
Descriptive maps |
| Panel | File (example) |
|---|---|
| Predicted rates (OOF) | Figures/predicted/panel_predicted_rates_oof_pooled.png |
| Residual maps | Figures/residual_diagnostics/panel_residual_maps_oof_pooled.png |
| LISA clusters | Figures/residual_diagnostics/panel_lisa_maps_oof_pooled.png |
| Fit metrics | Figures/fit/panel_fit_metrics_oof_pooled.png |
| SHAP + importance | Figures/variables_analysis/panel_shap_importance.png |
| Stacking summary | Figures/stacking/panel_stacking_oof_pooled.png |
| Variable | Description | Source |
|---|---|---|
casos_tb |
Notified TB cases by district | TB surveillance system |
tasa_tb_10k |
Rate per 10,000 inhabitants | Derived from cases and population |
| Domain | Variables (examples) | Source |
|---|---|---|
| Demographics | Population density, sex/age structure, migration, indigenous peoples, overcrowding, mean age | Census 2024 (district aggregates) |
| Health access | APS establishment density, distance to nearest primary-care facility | IDE Chile / Geoportal (Establecimientos de salud de Chile, Febrero 2026) |
| Climate | Mean NDVI, mean/max/min temperature (2024) | Google Earth Engine |
Spatial lag of TB rate (train-only in CV) is added as a predictor for NB-lag, RF-S, BART, and XGB.
All implemented models, mathematical specifications, cross-validation design, metrics, and output contracts are documented in:
Models in the main comparison: NB-lag, BART, RF-S, XGB, BYM2, Stacking. The INLA VIF-reduced variant is supplementary.
Individual-level TB notification data are subject to Chilean health confidentiality rules. District-level aggregates used in this repository are derived from administrative sources described above. Processed analytic files are stored under 01_data/analysis/ when available in the workspace.
Supported by FONIS Nº SA24I0203. We thank the Chilean Ministry of Health and census data providers. Climate covariates use Google Earth Engine extracts (see 02_code/python/).
- Salvador Ayala (corresponding): salvadorayala@udd.cl
- José Daniel Conejeros (code): jdconejeros@uc.cl



