Skip to content

Epi-Esp-Lab/TB_ML_FONIS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Machine Learning versus Bayesian Spatial Models for Small-Area Tuberculosis Prediction: A Comparative Study in Metropolitan Santiago, Chile

Draft in preparation

GitHub Repo stars GitHub watchers GitHub forks GitHub commit activity GitHub contributors GitHub last commit GitHub language count GitHub top language GitHub License GitHub repo file or directory count GitHub code size in bytes

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.

Main figures

Figure 1. Study area and spatial distribution of tuberculosis outcomes in Metropolitan Santiago

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).

Figure 1. Study area and TB outcomes

Figure 2. Out-of-fold predicted TB rates by model

Choropleth maps and observed-vs-predicted scatter plots for NB-lag, BART, RF-S, XGB, and BYM2 (pooled OOF evaluation).

Figure 2. OOF predicted TB rates by model

Figure 3. Cross-model predictive performance (pooled out-of-fold)

Comparison of MAE, RMSE, BIAS, R², CE, and IA across NB-lag, BART, RF-S, XGB, and BYM2.

Figure 3. Cross-model fit metrics

Figure 4. Stacking ensemble: spatial predictions, residuals, and model comparison

OOF predicted rates and residuals for the NNLS stacking meta-learner, with fit metrics compared against XGB and RF-S.

Figure 4. Stacking ensemble evaluation

Funding

FONIS Nº SA24I0203

Authors

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

Project overview

Background

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)

Unit of analysis

  • 343 urban districts with polygon geometry
  • Outcome: casos_tb; rate metric tasa_tb_10k (per 10,000 population)
  • Queen contiguity spatial weights; train-only spatial lags in cross-validation

Objectives

  1. Build a harmonized district-level analytic dataset (census, health access, climate).
  2. Fit and cross-validate multiple spatial and ML models on a common covariate pool.
  3. Evaluate predictive performance (MAE, RMSE, BIAS, CE, IA, R²) and residual spatial structure (Moran's I, LISA).
  4. Produce publication-ready maps, diagnostic panels, variable-importance figures, and tables.

Full statistical and model specifications: 02_code/03_models/README.md.


Repository structure

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

Reproducibility

System requirements

  • 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.R

INLA and fastshap require additional setup; see install_spatial_ml_dependencies.R.

Pipeline execution

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.R

2. Descriptive analysis

Rscript 02_code/02_descriptives/02_descriptive_analysis.R

3. 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.R

4. Climate extraction (optional refresh)

python 02_code/python/06_temperature_ndvi_extraction.py --dry-run
python 02_code/python/06_temperature_ndvi_extraction.py

Approximate total runtime

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

Key outputs

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

Main figure panels (05)

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

Variables

Outcomes

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

Covariates (selection)

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.


Model documentation

All implemented models, mathematical specifications, cross-validation design, metrics, and output contracts are documented in:

02_code/03_models/README.md

Models in the main comparison: NB-lag, BART, RF-S, XGB, BYM2, Stacking. The INLA VIF-reduced variant is supplementary.


Data availability

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.


Acknowledgments

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/).


Contact

About

Machine learning predict tuberculosis rate

Resources

License

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors