abc | R Documentation |
Approximate Bayesian computation
abc(
model,
priors = NULL,
npart = NULL,
ninit = NULL,
distance = NULL,
tolerance = NULL,
...,
verbose = getOption("verbose", FALSE),
post_gen = NULL
)
## S4 method for signature 'SimInf_model'
abc(
model,
priors = NULL,
npart = NULL,
ninit = NULL,
distance = NULL,
tolerance = NULL,
...,
verbose = getOption("verbose", FALSE),
post_gen = NULL
)
model |
The |
priors |
The priors for the parameters to fit. Each prior is
specified with a formula notation, for example, |
npart |
An integer |
ninit |
Specify a positive integer (> |
distance |
A function for calculating the summary statistics
for a simulated trajectory. For each particle, the function
must determine the distance and return that information. The
first argument, |
tolerance |
A numeric matrix (number of summary statistics
|
... |
Further arguments to be passed to |
verbose |
prints diagnostic messages when |
post_gen |
An optional function that, if non-NULL, is applied
after each completed generation. The function must accept one
argument of type |
A SimInf_abc
object.
2009
\Simola2021
## Not run:
## Let us consider an SIR model in a closed population with N = 100
## individuals of whom one is initially infectious and the rest are
## susceptible. First, generate one realisation (with a specified
## seed) from the model with known parameters \code{beta = 0.16} and
## \code{gamma = 0.077}. Then, use \code{abc} to infer the (known)
## parameters from the simulated data.
model <- SIR(u0 = data.frame(S = 99, I = 1, R = 0),
tspan = 1:100,
beta = 0.16,
gamma = 0.077)
## Run the SIR model and plot the number of infectious.
set.seed(22)
infectious <- trajectory(run(model), "I")$I
plot(infectious, type = "s")
## The distance function to accept or reject a proposal. Each node
## in the simulated trajectory (contained in the 'result' object)
## represents one proposal.
distance <- function(result, ...) {
## Extract the time-series of infectious in each node as a
## data.frame.
sim <- trajectory(result, "I")
## Split the 'sim' data.frame by node and calculate the sum of the
## squared distance at each time-point for each node.
dist <- tapply(sim$I, sim$node, function(sim_infectious) {
sum((infectious - sim_infectious)^2)
})
## Return the distance for each node. Each proposal will be
## accepted or rejected depending on if the distance is less than
## the tolerance for the current generation.
dist
}
## Fit the model parameters using ABC-SMC and adaptive tolerance
## selection. The priors for the parameters are specified using a
## formula notation. Here we use a uniform distribtion for each
## parameter with lower bound = 0 and upper bound = 1. Note that we
## use a low number particles here to keep the run-time of the example
## short. In practice you would want to use many more to ensure better
## approximations.
fit <- abc(model = model,
priors = c(beta ~ uniform(0, 1), gamma ~ uniform(0, 1)),
npart = 100,
ninit = 1000,
distance = distance,
verbose = TRUE)
## Print a brief summary.
fit
## Display the ABC posterior distribution.
plot(fit)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.