amis | R Documentation |
This implements the AMIS algorithm as described in Retkute et al. (2021). Each iteration of the algorithm produces a set of parameters from a proposal distribution (the prior in the first iteration). For each parameter set, a simulation is run from the transmission model. Then, each preceding simulation is weighted at each location according to the distribution of prevalences (or likelihood function) at that location. A Gaussian mixture model is then fitted to the parameter samples with weights averaged over the active locations (ie locations that have yet to reach the effective sample size target). This Gaussian mixture informs the proposal for the next iteration. The algorithm continues until every location has reached the ESS target, or the maximum number of iterations is reached.
amis(
prevalence_map,
transmission_model,
prior,
amis_params = default_amis_params(),
seed = NULL,
output_dir = NULL,
initial_amis_vals = NULL
)
prevalence_map |
For a single timepoint,
The location names are inherited from |
transmission_model |
A function taking arguments:
This function must return an |
prior |
A list containing the functions
The only argument of |
amis_params |
A list containing control parameters for the AMIS algorithm
(
Uniform kernel is the default method for the density estimator of the likelihood.
If |
seed |
Optional single value interpreted as an integer.
It is the seed for the random number generator for the AMIS algorithm. This is not the same as
the |
output_dir |
A string specifying the local directory where to save outputs
after each iteration of the algorithm. At the end of the string,
use the correct path separator for your machine's operating system.
If the directory is specified, the outputs will be saved in a file called ‘amis_output.rds’.
Default to |
initial_amis_vals |
Optional list of intermittent outputs from a
previous run (where at least one iteration was successful). These outputs can
be saved by specifying the directory |
The average weight of parameter vectors for the set of active locations at iteration i
\left(A_i\right)
has weights determined by how far the effective sample size for location l
\left(\text{ESS}_l^i\right)
is from the target \left(\text{ESS}^R\right)
:
\bar{w}_j^i =
\frac{\sum_{l \in A_i} \left(\text{ESS}^R-\text{ESS}_l^i\right)^q \hat{w}_{lj}^i }{
\sum_{l \in A_i} \left(\text{ESS}^R-\text{ESS}_l^i\right)^q} , \qquad q \in [0,1].
If q=0
(default), the simple average of individual weights will be calculated.
If q>0
, more weight will be assigned to locations with low ESS.
A list of class amis
. If the algorithm completed I
iterations,
it simulated a total of N = I \times
n_samples
, and therefore the list returned by amis()
will contain:
seeds
An N
-length vector with the simulation seeds that were used.
param
An N \times d
matrix with the d
-dimensional transmission model parameters simulated by the algorithm.
simulated_prevalences
An N \times T
matrix with the simulated prevalences, where T
is the number of timepoints.
weight_matrix
An N \times L
, where L
is the number of locations.
likelihoods
A T \times L \times N
array with the likelihood of observing a simulated prevalence in each location at each time.
ess
An L
-length vector with the final effective sample size (ESS) for each location.
prevalence_map
List with the prevalence map supplied by the user.
locations_with_no_data
Vector indicating which locations have no data at any time point.
components
A list of the mixture components of all iterations, containing:
G
: number of components in each iteration;
probs
: the mixture weights;
Mean
: the locations of the components;
Sigma
: the covariance matrices of the components.
components_per_iteration
A list with the mixture components at each iteration.
This object is used in plot_mixture_components()
.
ess_per_iteration
An L \times I
matrix with with the ESS for each location after each iteration.
prior_density
An N
-length vector with the density function evaluated at the simulated parameter values.
amis_params
List supplied by the user.
evidence
A list containing an estimate of the log model evidence and corresponding log variance of this estimate for both the full likelihood model (product over all locations), and for each location individually.
Retkute, R., Touloupou, P., Basanez, M. G., Hollingsworth, T. D., Spencer, S. E. (2021). Integrating geostatistical maps and infectious disease transmission models using adaptive multiple importance sampling. The Annals of Applied Statistics, 15(4), 1980-1998. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1214/21-AOAS1486")}.
# Define simple "transmission" model where prevalence equals first parameter
transmission_model_identity <- function(seeds, parameters, n_tims=1) {
return(matrix(parameters[,1], ncol=1))
}
# Generate samples for prevalence map with 3 locations given by B(2,1), B(1,1)=Uniform, B(1,2).
set.seed(123)
L <- 3 # Number of locations
M <- 500 # Number of map samples
prevalence_map <- matrix(NA, L, M)
for (l in 1:L) {
prevalence_map[l,] <- rbeta(M, max(1,l-1), max(1,3-l))
}
rownames(prevalence_map) <- c("Here","There","Somewhere else")
# Define 2D exponential prior
rprior <- function(n) {
params <- matrix(NA, n, 2)
colnames(params) <- c("a","b")
params[,1] <- rexp(n)
params[,2] <- rexp(n)
return(params)
}
dprior <- function(x, log=FALSE) {
if (log) {
return(sum(dexp(x, log=TRUE)))
} else {
return(prod(dexp(x)))
}
}
prior <- list(rprior=rprior,dprior=dprior)
# Run AMIS with default control parameters
amis_params <- default_amis_params()
output <- amis(prevalence_map, transmission_model_identity, prior, amis_params, seed=1)
print(output)
summary(output)
original_par <- par(no.readonly = TRUE)
par(cex.lab=1.5, cex.main=1.5, mar=c(5,4.5,4,2)+0.1)
par(mfrow=c(1,2))
plot_mixture_components(output, what = "uncertainty", cex=3)
plot_mixture_components(output, what = "density", nlevels=200)
par(mfrow=c(3,3))
plot(output, what = "a", type="hist", locations=1:L, breaks=100)
plot(output, what = "b", type="hist", locations=1:L, breaks=100)
plot(output, what = "prev", type="hist", locations=1:L, time=1, breaks=100)
par(mar=c(5,7.5,4,2)+0.1)
par(mfrow=c(1,3))
plot(output, what=c("a","b","prev"), type="CI", locations=1:L, ylab=NA,
cex=3, lwd=3, measure_central="median", display_location_names=TRUE)
calculate_summaries(output, what="prev", locations=1:L, alpha=0.05)
# Generate new samples from the weighted posterior distributions
new_samples <- sample_parameters(output, n_samples = 200, locations = "Here")
head(new_samples)
plot_hist <- function(column_name){
hist(new_samples[, column_name], xlab=column_name, main=paste("Histogram of", column_name))
}
par(mfrow=c(1,3))
plot_hist("a")
plot_hist("b")
plot_hist("prevalence")
par(original_par)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.