Title: | Implement the AMIS Algorithm for Infectious Disease Models |
---|---|
Description: | Implements the Adaptive Multiple Importance Sampling (AMIS) algorithm, as described by Retkute et al. (2021, <doi:10.1214/21-AOAS1486>), to estimate key epidemiological parameters by combining outputs from a geostatistical model of infectious diseases (such as prevalence, incidence, or relative risk) with a disease transmission model. Utilising the resulting posterior distributions, the package enables forward projections at the local level. |
Authors: | Evandro Konzen [aut] , Renata Retkute [aut] , Raiha Browning [aut] , Thilbault Lestang [aut], Simon Spencer [aut, cre] , University of Warwick [cph], Oxford Research Software Engineering [cph] |
Maintainer: | Simon Spencer <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2025-01-21 08:47:14 UTC |
Source: | https://github.com/drsimonspencer/amisforinfectiousdiseases-dev |
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 )
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
has weights determined by how far the effective sample size for location
is from the target
:
If (default), the simple average of individual weights will be calculated.
If
, more weight will be assigned to locations with low ESS.
A list of class amis
. If the algorithm completed iterations,
it simulated a total of
n_samples
, and therefore the list returned by amis()
will contain:
seeds
An -length vector with the simulation seeds that were used.
param
An matrix with the
-dimensional transmission model parameters simulated by the algorithm.
simulated_prevalences
An matrix with the simulated prevalences, where
is the number of timepoints.
weight_matrix
An , where
is the number of locations.
likelihoods
A array with the likelihood of observing a simulated prevalence in each location at each time.
ess
An -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 matrix with with the ESS for each location after each iteration.
prior_density
An -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. 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)
# 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)
Calculate summaries of weighted statistics
calculate_summaries( x, what = "prev", time = 1, locations = NULL, alpha = 0.05, exceedance_prob_threshold = 0.35 )
calculate_summaries( x, what = "prev", time = 1, locations = NULL, alpha = 0.05, exceedance_prob_threshold = 0.35 )
x |
The output from the function |
what |
What statistic should be calculated the summaries from.
It must be either |
time |
Time point. Only used if |
locations |
Integer vector or location names identifying locations where summaries should be calculated for. If not specified, summary statistics of all locations will be provided. |
alpha |
Numeric value between 0 and 1. Calculations are for the |
exceedance_prob_threshold |
Numeric value. Default to |
For illustrative examples, see amis()
.
A list with mean, median, and quantiles of the weighted distribution.
For description of AMIS parameters, see argument amis_params
in amis()
.
default_amis_params()
default_amis_params()
List containing the default AMIS parameters.
plot.Mclust()
Wrapper function for plot.Mclust()
plot_mixture_components( x, what = "uncertainty", iteration = NULL, datapoints = "proposed", main = NULL, xlim = NULL, ylim = NULL, ... )
plot_mixture_components( x, what = "uncertainty", iteration = NULL, datapoints = "proposed", main = NULL, xlim = NULL, ylim = NULL, ... )
x |
The output from the function |
what |
A string specifying the type of plot requested:
|
iteration |
Integer indicating which iteration the plot should be about.
If |
datapoints |
A string specifying what the datapoints should represent in the plot of classification uncertainty:
|
main |
Title of the plot. If |
xlim |
The x limits of the plots. Default to |
ylim |
The y limits of the plots. Default to |
... |
Other arguments to match the |
For illustrative examples, see amis()
.
A plot for model-based clustering results.
amis()
Plot histogram or credible interval of weighted distributions given a model fitted by amis()
## S3 method for class 'amis' plot( x, what = "prev", type = "hist", locations = 1, time = 1, measure_central = "mean", order_locations_by = NULL, display_location_names = FALSE, alpha = 0.05, breaks = 500, cex = 1, lwd = 1, xlim = NULL, main = NULL, xlab = NULL, ylab = NULL, ... )
## S3 method for class 'amis' plot( x, what = "prev", type = "hist", locations = 1, time = 1, measure_central = "mean", order_locations_by = NULL, display_location_names = FALSE, alpha = 0.05, breaks = 500, cex = 1, lwd = 1, xlim = NULL, main = NULL, xlab = NULL, ylab = NULL, ... )
x |
The output from the function |
what |
What posterior distribution should be plotted.
It can be |
type |
Type of plot. It can be |
locations |
Integer vector or location names identifying locations the plots are made for. Default to |
time |
Integer index identifying the timepoint. Default to |
measure_central |
Measure of central tendency for credible interval plots.
It can be |
order_locations_by |
How the credible intervals of multiple locations should be ordered.
If |
display_location_names |
Logical indicating whether location names are to be shown or not
in credible interval plots. Default to |
alpha |
Numeric value between 0 and 1 indicating the endpoints of the
credible intervals, which are evaluated at (alpha/2, 1-alpha/2)% quantiles.
Default ( |
breaks |
Argument passed to |
cex |
Argument passed to plots of credible intervals.
Default to |
lwd |
Argument passed to plots of credible intervals.
Default to |
xlim |
The x limits of the plots. For for credible intervals of multiple
statistics (i.e. |
main |
Title for the plot. |
xlab |
Lable for the x axis. |
ylab |
Lable for the y axis. |
... |
Other graphical parameters passed to |
For illustrative examples, see amis()
.
A plot.
amis
Print method for object of class amis
## S3 method for class 'amis' print(x, ...)
## S3 method for class 'amis' print(x, ...)
x |
The output from the function |
... |
Other arguments to match the generic |
For illustrative examples, see amis()
.
Brief description of data and model specifications used to run amis()
.
amis()
Sample parameters from their weighted distributions given a model fitted by amis()
sample_parameters(x, n_samples = 200, locations = 1)
sample_parameters(x, n_samples = 200, locations = 1)
x |
The output from the function |
n_samples |
Number of samples to draw. Default to |
locations |
Integer identifying the locations. Default to |
For illustrative examples, see amis()
.
Matrix with parameter values and corresponding prevalences for each location.
amis
Summary method for object of class amis
## S3 method for class 'amis' summary(object, ...)
## S3 method for class 'amis' summary(object, ...)
object |
The output from the function |
... |
Other arguments to match the generic |
For illustrative examples, see amis()
.
Summary statistics of the fitted model.