if (FALSE) {
##### EXAMPLE 1 #####
#####################
set.seed(1)
## artificial example to show how to use the 'ABC_sequential' function.
## defining a simple toy model:
toy_model<-function(x){ 2 * x + 5 + rnorm(1,0,0.1) }
## define prior information
toy_prior=list(c("unif",0,1)) # a uniform prior distribution between 0 and 1
## define the targeted summary statistics
sum_stat_obs=6.5
## to perform the Beaumont et al. (2009)'s method:
##
tolerance=c(1.5,0.5)
ABC_Beaumont<-ABC_sequential(method="Beaumont", model=toy_model, prior=toy_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, tolerance_tab=tolerance)
ABC_Beaumont
## to perform the Drovandi and Pettitt (2011)'s method:
##
tolerance=0.5
c_drov=0.7
ABC_Drovandi<-ABC_sequential(method="Drovandi", model=toy_model, prior=toy_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, tolerance_tab=tolerance, c=c_drov)
ABC_Drovandi
## to perform the Del Moral et al. (2012)'s method:
##
alpha_delmo=0.5
tolerance=0.5
ABC_Delmoral<-ABC_sequential(method="Delmoral", model=toy_model, prior=toy_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, alpha=alpha_delmo, tolerance_target=tolerance)
ABC_Delmoral
## to perform the Lenormand et al. (2012)'s method:
##
pacc=0.4
ABC_Lenormand<-ABC_sequential(method="Lenormand", model=toy_model, prior=toy_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, p_acc_min=pacc)
ABC_Lenormand
##### EXAMPLE 2 #####
#####################
## this time, the model has two parameters and outputs two summary statistics.
## defining a simple toy model:
toy_model2<-function(x){ c( x[1] + x[2] + rnorm(1,0,0.1) , x[1] * x[2] + rnorm(1,0,0.1) ) }
## define prior information
toy_prior2=list(c("unif",0,1),c("normal",1,2))
# a uniform prior distribution between 0 and 1 for parameter 1, and a normal distribution
# of mean 1 and standard deviation of 2 for parameter 2.
## define the targeted summary statistics
sum_stat_obs2=c(1.5,0.5)
## to perform the Beaumont et al. (2009)'s method:
##
tolerance=c(1.5,0.5)
ABC_Beaumont<-ABC_sequential(method="Beaumont", model=toy_model2, prior=toy_prior2,
nb_simul=20, summary_stat_target=sum_stat_obs2, tolerance_tab=tolerance)
ABC_Beaumont
## to perform the Drovandi and Pettitt (2011)'s method:
##
tolerance=0.5
c_drov=0.7
ABC_Drovandi<-ABC_sequential(method="Drovandi", model=toy_model2, prior=toy_prior2,
nb_simul=20, summary_stat_target=sum_stat_obs2, tolerance_tab=tolerance, c=c_drov)
ABC_Drovandi
## to perform the Del Moral et al. (2012)'s method:
##
alpha_delmo=0.5
tolerance=0.5
ABC_Delmoral<-ABC_sequential(method="Delmoral", model=toy_model2, prior=toy_prior2,
nb_simul=20, summary_stat_target=sum_stat_obs2, alpha=alpha_delmo, tolerance_target=tolerance)
ABC_Delmoral
## to perform the Lenormand et al. (2012)'s method:
##
pacc=0.4
# Only uniform priors are supported for the method "Lenormand" (since it performs a Latin
# Hypercube sampling at the beginning):
toy_prior2=list(c("unif",0,1),c("unif",0.5,1.5))
# a uniform prior distribution between 0 and 1 for parameter 1, and a normal distribution of
# mean 1 and standard deviation of 1 for parameter 2.
ABC_Lenormand<-ABC_sequential(method="Lenormand", model=toy_model2, prior=toy_prior2,
nb_simul=20, summary_stat_target=sum_stat_obs2, p_acc_min=pacc)
ABC_Lenormand
##### EXAMPLE 3 #####
#####################
## this time, the model is a C++ function packed into a R function -- this time, the option
# 'use_seed' must be turned to TRUE.
n=10
## define prior information
trait_prior=list(c("unif",3,5),c("unif",-2.3,1.6),c("unif",-25,125),c("unif",-0.7,3.2))
trait_prior
## define the targeted summary statistics
sum_stat_obs=c(100,2.5,20,30000)
## to perform the Beaumont et al. (2009)'s method:
##
tolerance=c(8,5)
ABC_Beaumont<-ABC_sequential(method="Beaumont", model=trait_model, prior=trait_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, tolerance_tab=tolerance, use_seed=TRUE)
ABC_Beaumont
## to perform the Drovandi and Pettitt (2011)'s method:
##
tolerance=3
c_drov=0.7
ABC_Drovandi<-ABC_sequential(method="Drovandi", model=trait_model, prior=trait_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, tolerance_tab=tolerance, c=c_drov,
use_seed=TRUE)
ABC_Drovandi
## to perform the Del Moral et al. (2012)'s method:
##
alpha_delmo=0.5
tolerance=3
ABC_Delmoral<-ABC_sequential(method="Delmoral", model=trait_model, prior=trait_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, alpha=alpha_delmo,
tolerance_target=tolerance, use_seed=TRUE)
ABC_Delmoral
## to perform the Lenormand et al. (2012)'s method:
##
pacc=0.4
ABC_Lenormand<-ABC_sequential(method="Lenormand", model=trait_model, prior=trait_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, p_acc_min=pacc, use_seed=TRUE)
ABC_Lenormand
##### EXAMPLE 4 - Parallel implementations #####
################################################
## NB: the option use_seed must be turned to TRUE.
## For models already running with the option use_seed=TRUE, simply change
# the value of n_cluster:
sum_stat_obs=c(100,2.5,20,30000)
ABC_Lenormand<-ABC_sequential(method="Lenormand", model=trait_model, prior=trait_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, p_acc_min=pacc, use_seed=TRUE, n_cluster=2)
ABC_Lenormand
## For other models, change the value of n_cluster and modify the model so that the
# first parameter becomes a seed information value:
toy_model_parallel<-function(x){
set.seed(x[1])
2 * x[2] + 5 + rnorm(1,0,0.1) }
sum_stat_obs=6.5
ABC_Lenormand<-ABC_sequential(method="Lenormand", model=toy_model_parallel, prior=toy_prior,
nb_simul=20, summary_stat_target=sum_stat_obs, p_acc_min=pacc, use_seed=TRUE, n_cluster=2)
ABC_Lenormand
}
Run the code above in your browser using DataLab