Skip to content

About

Clean chap-compatible re-implementation of the central ideas in ewars_Plus: Bayesian hierarchical NB+INLA with per-district adaptive lag selection.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ewars_plus_template

A clean chap-compatible re-implementation of the central modelling ideas in the upstream ewars_Plus model, layered on top of the ewars_template backbone (Bayesian hierarchical NB regression with INLA).

What this adds on top of ewars_template

  • Per-(location, covariate) adaptive lag selection over a configurable candidate set (default [7, 10, 12] for weekly, configurable for monthly), using expanding-window cross-validation per location. Each location ends up with its own selected lag for each climate covariate — closer in spirit to ewars_Plus's per-district selection. Ties resolved to the smallest lag (more parsimonious).
  • Single shifted-lag column per covariate, with an INLA-grouped RW1 smooth for the exposure-response shape — matching ewars_Plus's production formula (selected_Model_form_rw). No dlnm crossbasis. The column carries different lags across locations, but the smooth shape is shared, so cross-location pooling on the exposure-response curve is preserved.
  • Optional location-specific deviation (location_specific_effects: true): adds a per-location RW1 smooth on the same grouped covariate (replicate = ID_spat), so each location gets a deviation from the shared curve. Partial-pooled via a shared precision hyperprior. Default false keeps the original shared-smooth-only behaviour.
  • Parent-org-unit lag grouping (lag_grouping: parent): selects one lag per parent org unit instead of one per location, on the population-weighted aggregate of that parent's locations. Cuts the number of CV fits by the district-per-parent factor, and acts as shrinkage where per-location counts are too sparse to pick a lag from.
  • Pluggable nonlinearity backend (nonlinearity: rw1_inla_group | linear): the lagged-covariate effect is built by a swappable backend. Two are shipped today (rw1_inla_group — the ewars_Plus-matched default; linear — standardised linear baseline); adding more (crossbasis, splines, …) is a one-function change in lib.R with no predict.R edits required.

Covariate + location-specific lags vs. the upstream ewars_Plus

This section unpacks what's the same, what's different, and why.

Selection step

Aspect Upstream ewars_Plus ewars_plus_template
Candidate columns paste0(var, "_LAG", Min_lag:Max_lag) — single shifted columns per candidate k (Lag_Model_selection_…R:130) Same idea: candidate k values come from candidate_lags; the scorer builds a shifted column per candidate k in-memory.
Scoring Joint INLA fit on the full multi-variable formula, scored by DIC (Sel_Vars, ~L285), choosing the best joint combination of (var1_LAG_k1, var2_LAG_k2, …). Per-(location, covariate) independent expanding-window CV. Each cell fits a single-covariate NB+INLA on the training fold and scores the next fold by held-out NB log-density. Folds within a (location, covariate) are averaged.
Where the lag varies Per-district. The brittle bit in upstream: different CV folds within one district could pick different k, producing columns like _LAG12 / _LAG10 / _LAG7 that foreach(.combine = rbind) then failed to stack. Per (location, covariate). Folds within a (location, covariate) are aggregated by mean before argmax (smallest-lag tie-break), so each (location, covariate) cell ends up with exactly one selected lag — no across-fold divergence.
Output One selected shifted column name per district per variable, e.g. Selected_lag_Vars = c("rainfall_LAG10", "mean_temperature_LAG7"). data.frame(location, covariate, lag) — one row per (location, covariate). Cached as <model>_lags.rds by train.R so the CV runs once per backtest split, not once per predict call.

Materialisation in the design matrix

Aspect Upstream ewars_Plus ewars_plus_template
Column naming The selected lag is encoded in the column name itself (rainfall_LAG10). Different districts in the same fit use the same column name only when they happen to share the selected lag. Column name is <cov>_lag and doesn't encode the lag. Each row's value is the covariate at the lag that this row's location chose. So location A's rainfall_lag rows hold rainfall[t-2], location B's hold rainfall[t-3], and so on — all in one column.
Why it matters Per-district lag heterogeneity meant the upstream had to fit per district or contort the design matrix; the historical brittleness with _LAG12 / _LAG10 / _LAG7 and rbind came from trying to stack frames with divergent column names. Per-location lag heterogeneity is moved into the values, not the column names. The design matrix has consistent shape across locations regardless of how diverse the lag map is.

Final INLA formula

Both models use a Cases ~ negative binomial, offset = log(E) likelihood with an iid spatial random effect (replicated across years) and a cyclic RW1 seasonal effect. The covariate piece is what's interesting:

Upstream selected_Model_form_rw (per district, then INLA is called per district):

Cases ~ 1
  + f(ID_spat,    model = "iid", replicate = ID_year)
  + f(week,       model = "rw1", cyclic = TRUE, scale.model = TRUE)
  + f(Var1_Inla_group, model = "rw1", scale.model = TRUE)
  + f(Var2_Inla_group, model = "rw1", scale.model = TRUE)
  + …

where VarN_Inla_group = inla.group(Selected_lag_Vars[N]) and each district's INLA call uses its own selected Selected_lag_Vars.

This template's formula (single joint INLA call across all locations):

Cases ~ 1
  + f(ID_spat,        model = "iid", replicate = ID_year)
  + f(ID_time_cyclic, model = "rw1", cyclic = TRUE, scale.model = TRUE)
  + f(<cov>_lag_grp,  model = "rw1", scale.model = TRUE)               # one per covariate
  [+ f(<cov>_lag_grp_loc, model = "rw1", scale.model = TRUE,
       replicate = ID_spat)]                                            # if location_specific_effects = TRUE
  [+ f(ID_time_cyclic2, model = "rw1", cyclic = TRUE, scale.model = TRUE,
       replicate = ID_spat)]                                            # if region_seasonal = TRUE

where <cov>_lag_grp = inla.group(<cov>_lag). With location_specific_effects = FALSE (default) the exposure-response is a single shared smooth across all locations. With it TRUE, each location gets a partial-pooled deviation from that shared smooth — a hierarchical decomposition where the global RW1 captures the average exposure-response shape and the per-location RW1 captures location-specific deviation. The two precisions are independent hyperparameters, both estimated from the data.

Swapping the nonlinearity

The exposure-response piece is built by a backend function registered in lib.R. The interface is:

function(df, covariate, location_specific_effects) -> list(df = ..., terms = ...)

It mutates df with whatever extra columns the parameterisation needs and returns the formula fragments that should enter the linear predictor. generate_lagged_model loops over covariates, calls the backend per covariate, and paste()s the fragments into the formula string.

Shipping backends:

Name Effect added With location_specific_effects = TRUE
rw1_inla_group (default) f(inla.group(<cov>_lag), model='rw1', scale.model=TRUE) adds a parallel f(..., replicate=ID_spat) deviation on the same grouped column
linear <cov>_lag_z (standardised; NA→0 imputed for prediction-row safety) adds f(ID_spat_<cov>, <cov>_lag_z, model='iid') — per-location random slope

Choose via user_options.nonlinearity in the model config. Adding a new backend (crossbasis, splines, GP, …) is a one-function change:

backend_my_method <- function(df, covariate, location_specific_effects = FALSE) {
  # mutate df, return formula fragment(s)
  list(df = df, terms = "...")
}
nonlinearity_backends$my_method <- backend_my_method

predict.R doesn't need to change — it looks up the backend by name via get_nonlinearity_backend().

Equivalence vs. divergence vs. upstream

  • Lag placement is the same idea: each location picks its own lag from a configurable candidate set, by CV.
  • Lag carrier differs: upstream encodes the lag in the column name and fits per district; this template encodes it in the row's value and fits jointly. Result: same per-location heterogeneity, but one INLA call instead of N, with cross-location pooling for free on the smooth's precision hyperparameter.
  • Exposure-response shape differs by default: upstream's per-district fits give each district its own RW1 smooth. This template defaults to a shared smooth (no per-location deviation). Set location_specific_effects: true to get the per-location RW1 back — and the shared smooth then plays the role of an average / prior mean.
  • What is dropped on purpose: the joint multi-variable DIC-based selection from upstream, the HTTP service, the on-disk session state, the endemic-channel / outbreak-threshold side outputs.

Grouping lag selection by parent org unit

Per-location selection costs locations x covariates x candidate_lags x folds INLA fits. On a weekly Laos dataset of 146 districts with two covariates, three candidates and three folds that is 2628 fits in a single train call. Setting lag_grouping: parent selects per province instead — 18 groups, so 324 fits.

chap-core already supplies what this needs. It reads the parent property off each geojson feature and writes it as a parent column into the CSVs handed to the model, so nothing here parses geojson:

{"id": "J41dVMJoZF7", "parent": "W6sNfkJcXGC", "level": 3}

How selection changes:

lag_grouping: location (default) lag_grouping: parent
Series scored One per location One per parent, built by summing Cases and E and taking population-weighted means of the covariates
CV fits locations x cov x lags x folds parents x cov x lags x folds
Resulting lag map One lag per (location, covariate) The parent's lag, fanned out to every location in it

The lag map handed to add_lagged_columns still has one row per (location, covariate) either way — it errors on a missing pair — so the design matrix is unchanged.

The parent column must actually be populated. chap-core fills - when the dataset has no geojson, and chap eval discovers one only if it sits next to the CSV under the same stem (data.csv -> data.geojson). If the ids are missing or all -, lag_grouping: parent would put every location in a single group, so the model warns and falls back to per-location selection instead.

Covariates are weighted by population rather than averaged flat, so a province's climate series is not dominated by its smallest district.

What this deliberately does not include from ewars_Plus

  • No HTTP/plumber service. The MLproject contract uses simple Rscript entry points.
  • No on-disk session state per district.
  • No endemic-channel / outbreak-threshold side outputs. The model returns posterior-predictive samples only; chap-core consumes those and renders alarms downstream if desired.
  • No foreach(.combine = rbind) per-fold stacking. Per-(location, covariate) frames are built with consistent column names and combined with plain do.call(rbind, ...) — no brittle column-name divergence like the patterns we patched in CHAP-core's external_models/ewars_plus_api_patch.

Configuration

additional_continuous_covariates:
  - rainfall
  - mean_temperature
user_option_values:
  candidate_lags: [7, 10, 12]
  lag_selection_cv_folds: 3
  precision: 1
  region_seasonal: false
  lag_grouping: location             # or "parent" to share lags within a parent org unit
  location_specific_effects: false   # add per-location RW1 deviation on grouped covariate
  nonlinearity: rw1_inla_group       # or "linear"

Set n_lags: [7, 10] (or n_lags: 7) to bypass selection and use a manual lag per covariate.

Layout

MLproject              entry points and adapters
example_config.yaml    default user options
train.R                resolves lags (manual override or CV) and writes
                       `<model>_lags.rds` as a companion file
predict.R              reads the cached lags, fits INLA, samples
lib.R                  period-offset helpers, lag-selection, parent
                       grouping, nonlinearity backends, formula builder,
                       lag resolution
tests/                 testthat unit tests for the helpers

Lag resolution priority in predict.R: (1) cached <model>_lags.rds written by train.R; (2) n_lags manual override from config; (3) in-predict CV fallback. The cache means the CV loop runs once per backtest split rather than once per predict call.

Running the tests

Rscript -e "testthat::test_dir('tests')"

The lag-selection tests inject a stub scoring function and do not require INLA. The end-to-end smoke test does require INLA + dlnm and runs against example_data_monthly.

About

Clean chap-compatible re-implementation of the central ideas in ewars_Plus: Bayesian hierarchical NB+INLA with per-district adaptive lag selection.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages