
Approximate Bayesian Computation for Stochastic Model
Source:R/abc_stochastic_model.R
abc_stochastic_model.RdRuns an Approximate Bayesian Computation (ABC) procedure using a stochastic model and a set of prior parameter draws.
Usage
abc_stochastic_model(
sm,
priors,
target,
stat_func,
states = NULL,
tol = 0.1,
method = "rejection",
seed = 123
)
# S3 method for class 'abc_stochastic_model'
plot(x, ...)Arguments
- sm
An object of class
stochastic_model.- priors
A data frame or
tibble()of prior parameter values. Each row should correspond to one simulation, and columns should match the parameter names expected byupdate_parameters().- target
A named vector or data frame of observed summary statistics to match against simulated summary statistics.
- stat_func
A function that computes summary statistics from a simulation result. Should accept a model object and return a named numeric vector or
tibble()row.- states
Optional. A data frame or
tibble()of initial states. Each row corresponds to a complete model state e.g. for an SIR model each row has three columns- tol
A numeric tolerance (between 0 and 1) for the ABC rejection algorithm. Defaults to 0.1.
- method
A character string indicating the ABC method to use. Defaults to
"rejection". Passed directly toabc::abc().- seed
An integer specifying the random seed for reproducibility. Defaults to 123.
- x
An object of class
abc_stochastic_model.- ...
list of additional plot arguments.
param- iftypeis "prior_posterior" then parameter to plotstate- iftypeis "simulations" then state to plot
Value
An object of class "abc_stochastic_model" containing:
- model
The original stochastic model.
- fit
An object returned by
abc::abc()representing the ABC posterior.- priors
The input prior values used for simulations.
- posteriors
The output posterior values used for simulations.
- prior_predictive
The summary statistics defined in
stat_funcdrawn from the prior distribution.- state
Final state of model.
A ggplot object
Details
This function performs ABC by:
Sampling parameter combinations from
priorsUpdating the
stochastic_modelwith each sampled parameter setRunning the model and computing summary statistics via
stat_funcComparing simulated statistics to
targetusing theabc::abc()function
Parallelization is handled using furrr::future_map(), allowing reproducible simulation across parameter sets using a fixed seed.
Examples
reactions <- list(
infection = list(
transition = c("I" = +1),
rate = function(x,p,t){ p$beta })
)
example_scenario <- list(
params = list(beta = 0.1),
initial_states = list(I = 0),
sim_args = list(T = 10)
)
sm <- stochastic_model(reactions,example_scenario)
priors <- expand.grid(beta = seq(0.1, 0.5, length.out = 10))
target <- c(total_infected = 50)
abc_result <- abc_stochastic_model(
sm,
priors = priors,
target = target,
stat_func = function(sim) data.frame(total_infected = sim[nrow(sim), "I"]),
tol = 0.2
)