
Compute Weighted Ensemble Predictions from Multiple Parameter Sets
Source:R/calc_model_ensemble.R
calc_model_ensemble.RdRuns simulations for multiple parameter sets (with stochastic reruns
per set) and aggregates results using importance weights. Returns a
mosaic_ensemble object containing weighted mean, median, and quantile
envelopes for cases and deaths.
With an observation_model (as run_MOSAIC() supplies), every
member trajectory also receives an observation-level draw consistent with
the calibration likelihood, so the interval envelope is a posterior
predictive interval for the OBSERVED counts rather than for the engine's
trajectories alone. Cases: each weekly total (the blocks the dispersion was
estimated on) is drawn from a negative binomial around the member's weekly
total with the run's per-location weekly size \(k\), then apportioned to
the week's days in proportion to the member's daily counts (integer counts,
every day within one of its exact share and equal to it in expectation,
weekly totals exact). Deaths (when deaths_integration is supplied):
each weekly total gets the integrated deaths likelihood's quasi-Poisson
variance \(\phi_j \mu\) around the member's expected reported deaths,
coupled to the member's post-hoc deaths so that \(\phi_j = 1\) leaves them
unchanged. The draws are seeded from each member's simulation seed. The
central lines (*_mean, *_median) stay ENGINE-level: the noise is
mean-preserving, so the engine mean is the exact predictive mean, and the
median is the central trajectory rather than a quantile of the noise. See
.mosaic_ensemble_summaries().
This is the computation half of the ensemble workflow. Use
plot_model_ensemble to render plots from the returned object.
Usage
calc_model_ensemble(
config,
parameter_seeds = NULL,
configs = NULL,
parameter_weights = NULL,
n_simulations_per_config = 10L,
envelope_quantiles = c(0.025, 0.25, 0.75, 0.975),
PATHS = NULL,
priors = NULL,
sampling_args = list(),
n_cases_warmup_mask = 2L,
mask_final_deaths_step = FALSE,
score_idx_cases = 1L,
score_idx_deaths = 1L,
parallel = FALSE,
n_cores = NULL,
root_dir = NULL,
capture_trajectories = FALSE,
trajectory_channels = .MOSAIC_TRAJECTORY_CHANNELS_DEFAULT,
trajectory_n_lines = 150L,
trajectory_scratch_dir = NULL,
reduce_trajectories = TRUE,
deaths_integration = NULL,
observation_model = NULL,
verbose = TRUE
)Arguments
- config
Base configuration object (provides observed data and template).
- parameter_seeds
Numeric vector of seeds for
sample_parameters. Each seed generates a different parameter set.- configs
List of pre-sampled configuration objects (direct mode). Mutually exclusive with
parameter_seeds.- parameter_weights
Numeric vector of importance weights, same length as
parameter_seedsorconfigs. Normalized internally to sum to 1. IfNULL, all parameter sets are weighted equally.- n_simulations_per_config
Integer. Stochastic reruns per parameter set. Default
10L.- envelope_quantiles
Numeric vector of quantiles for confidence intervals. Must be even length to form lower/upper pairs. Default
c(0.025, 0.25, 0.75, 0.975)for 50 and 95 percent CIs.- PATHS
List of paths from
get_paths. Required for sampling mode.- priors
Priors object for parameter sampling. Required for sampling mode.
- sampling_args
Named list of additional arguments for
sample_parameters.- n_cases_warmup_mask
Integer. Number of LEADING cases timesteps that are an initial-condition warm-up transient (seeded E flushing into new_symptomatic before the SEIR dynamics settle). Default
2L, matchingplot_model_ensemble. This value is NOT applied to any of the returned series here; it is recorded in the returnedartifact_maskelement so downstream scoring (R2/bias) can exclude these positions. Set to0Lto record "no cases warm-up mask".- mask_final_deaths_step
Logical. If
TRUE, record that the FINAL deaths timestep is a structural zero to exclude from scoring. That was true of the laser-cholera engine (reported_deathswritten at tick and leading-trimmed, so the last slot was never written; laser-cholera issue #82). Since v0.96.0 the R engine reports deaths on the same row as cases, and the post-hoc death redraw fills the final column, so the default isFALSE. This value is NOT applied to any returned series here; it is recorded in the returnedartifact_maskelement for downstream scoring.- score_idx_cases, score_idx_deaths
Integer (1-based). Per-channel scored time-window START index (burn-in / deaths-era start). Columns strictly BEFORE these indices are unscored and recorded in
artifact_maskso R2/bias scoring drops them. Default1L(no-op). NOT applied to the returned series here.- parallel
Logical. Use parallel cluster for simulations. Default
FALSE.- n_cores
Integer or
NULL. Number of cores whenparallel = TRUE.- root_dir
Character. MOSAIC root directory. Required when
parallel = TRUE.- capture_trajectories
Logical. When
TRUE, harvest the comprehensive internal-state channels (trajectory_channels) from each member and attach a compact$trajectories(mosaic_trajectories) object – a per-channel central line (field$median, kept for schema stability) + a uniform-thinned set of actual member trajectories + derived series.reported_cases/reported_deathsare the ENGINE-level member trajectories (cases_engine_array/deaths_engine_array). Their central line follows the trajectory reducer's defaultcentral_method– the weighted median forreported_cases, the weighted mean forreported_deathsanddisease_deaths– matching the prediction plots'predicted_central; every other captured channel uses the weighted median.I_totalis the sum of the Isym and Iasym medians,mass_balancea ratio of compartment weighted means,CFRa ratio of 28-day rolling weighted-mean deaths and cases, andepidemic_fracthe weighted mean of the reconstructed epidemic flag. DefaultFALSE(run_MOSAIC()enables it for the posterior ensemble; never for the medoid). RAM/payload is linear inlength(trajectory_channels).- trajectory_channels
Character vector of
model$resultschannels to capture whencapture_trajectories = TRUE. Default.MOSAIC_TRAJECTORY_CHANNELS_DEFAULT(the comprehensive set). The documented RAM lever – shorten it to reduce capture cost.- trajectory_n_lines
Integer. Number of uniform-thinned member trajectories retained per location for the spaghetti display. Default 150.
- trajectory_scratch_dir
Character or
NULL. Directory for the per-sim trajectory-channel scratch spill (stream-to-disk capture). WhenNULLand capturing, a temporary directory is created. When provided, the CALLER owns its lifecycle (it is not auto-deleted) – used byrun_MOSAIC()to reduce over the optimized subset post-optimization.- reduce_trajectories
Logical. When
TRUE(default) the trajectory reduction runs here over all members and is attached as$trajectories. WhenFALSE, the channels are spilled to scratch but NOT reduced; the scratch handle is returned in$trajectory_scratchso the caller can reduce over a final (e.g. optimized) subset without re-simulating.- deaths_integration
Optional run-level setup from
run_MOSAIC()(control$likelihood$.deaths_integration). When supplied, each member's deaths are redrawn after its simulation from the reported CFR's posterior given that member's path – the same integration calibration scores with – so predicted deaths, true deaths and forecast years carry the calibrated CFR; the ensemble also returnscfr_posterior. WhenNULL(default) deaths are the engine's, drawn at the config'smu_jt– for configs sampled from the priors that is the PRIOR reported CFR, not the calibrated one. To reproduce a run's calibrated deaths post hoc, passreadRDS("<dir_output>/2_calibration/deaths_integration.rds").- observation_model
Optional observation model for the posterior predictive: a list with
k_cases(weekly negative binomial size per location, or one for all;Inf= Poisson) and optionallyweek_offset(0-6 days after Monday on which each location's reporting weeks start; default 0), or the table of<dir_output>/2_calibration/diagnostics/nb_dispersion.csv(its cases rows, matched by location).run_MOSAIC()supplies the dispersion its likelihood scored with. The cases draw is this weekly negative binomial under either cases scoring rule. Under the defaultcontrol$likelihood$cases_scoring = "daily", whose per-day cells at the same \(k\) imply a weekly variance of about \(C + C^2/(7k)\) for a weekly total \(C\) (the predictive uses \(C + C^2/k\)), the intervals are therefore wider than the likelihood implies; under"weekly"they match it. The deaths dispersion is read fromdeaths_integration. WhenNULL(default) no observation noise is drawn andcases_array/deaths_arrayare the engine draws.- verbose
Logical. Print progress messages. Default
TRUE.
Value
S3 object of class "mosaic_ensemble" containing:
- cases_mean
Matrix (n_locations x n_time_points) of weighted mean cases, from the engine draws (the exact predictive mean).
- cases_median
Matrix of weighted median cases: the median engine trajectory.
- deaths_mean
Matrix of weighted mean deaths (engine draws).
- deaths_median
Matrix of weighted median deaths (engine draws).
- ci_bounds
List with
$casesand$deaths, each a list of interval pairs with$lowerand$uppermatrices: quantiles of the observation-level draws (of the engine draws when noobservation_modelis given).- predictive_median
List with
$casesand$deaths: the 0.5 quantile of the same draws asci_bounds, the median a proper interval score (WIS) pairs with those intervals, and thepredicted_medianof the prediction CSVsrun_MOSAIC()writes. Equal to*_medianwhen no observation noise was drawn.- obs_cases
Observed cases matrix from config.
- obs_deaths
Observed deaths matrix from config.
- cases_array
4-D array (n_locations x n_time_points x n_param_sets x n_stoch) of OBSERVATION-level draws (the engine draws when no
observation_modelis given).- deaths_array
4-D array of OBSERVATION-level deaths when the deaths received quasi-Poisson overdispersion (an
observation_modeland adeaths_integrationwith some \(\phi_j > 1\)); otherwise the engine deaths, identical todeaths_engine_array(nodeaths_integration, every \(\phi_j \le 1\), or noobservation_model).- cases_engine_array
4-D array of the ENGINE-level reported cases (the member trajectories before observation noise), same dimensions. The medoid, R_eff, trajectory and implied-CFR consumers read these.
- deaths_engine_array
4-D array of engine-level reported deaths (after the post-hoc CFR redraw when
deaths_integrationis supplied).- observation_model
List recording the observation noise applied:
cases(logical, whether cases received NB noise),deaths(logical, whether deaths received quasi-Poisson overdispersion: only where somephi_deaths > 1; at \(\phi = 1\) the engine deaths, binomial draws around the expected deaths, already are the observation-level deaths),k_cases,week_offsetandphi_deaths(per location,NULLwhen not applied).- parameter_weights
Normalized weight vector.
- seeds
Integer vector of per-member simulation seeds, aligned with the parameter dimension of the arrays (member
i<->seeds[i]). Bound to the parameter set that produced each member so consumers (e.g. medoid selection) need not rely on positional alignment with an external vector.- n_param_sets
Number of parameter sets.
- n_simulations_per_config
Stochastic runs per parameter set.
- n_successful
Number of successful simulations.
- location_names
Character vector of location names.
- n_locations
Number of locations (rows of the central matrices).
- n_time_points
Number of time steps (columns of the central matrices).
- date_start
Simulation start date.
- date_stop
Simulation end date.
- envelope_quantiles
Quantiles used for CI envelopes.
- spatial_hazard_ensemble
Element-wise median over successful members of the engine's
spatial_hazard(locations x time), orNULL.- coupling_ensemble
Element-wise median of the engine's
couplingmatrix (locations x locations), orNULL.- pi_ij_ensemble
Element-wise median of the engine's
pi_ijmobility matrix (locations x locations), orNULL.- trajectories
mosaic_trajectoriesobject whencapture_trajectories = TRUEandreduce_trajectories = TRUE(seecapture_trajectories), elseNULL.- trajectory_scratch
Scratch-spill handle when
capture_trajectories = TRUEandreduce_trajectories = FALSE, for a caller-side reduce over a final subset; elseNULL.- artifact_mask
List recording the engine-artifact masking spec for downstream scoring:
$cases_warmup(integer, leading cases timesteps to exclude),$deaths_final(logical, exclude the final deaths timestep), and$score_idx_cases/$score_idx_deaths(integer, 1-based per-channel scored-window start; columns before are dropped). The central/quantile/array fields above are RAW (unmasked); this spec is the contract scoring sites use to drop artifact positions.- cfr_posterior
When
deaths_integrationis supplied: a data frame with one row per location and calendar year –location,year,cfr_median,cfr_lower,cfr_upper(the weighted median and 95% interval over members of each member's mean daily reported CFR in that year) andprior_cfr(the priormu_jt's mean over the same days). Conditional on each member's modelled cases.NULLotherwise.- forecast_shift
When
deaths_integrationis supplied: one value per location, the weighted mean over members of the posterior-mode logit CFR deviation for the location's latest observed year (NAfor a location with no forecast years).run_MOSAIC()centres forecast years on it.NULLotherwise.
See also
plot_model_ensemble to render plots from this object.