-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_runMonteCarloCPPP.R
executable file
·116 lines (99 loc) · 3.95 KB
/
2_runMonteCarloCPPP.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#-----------------------------------------#
## Computational methods for fast Bayesian hierarchical model assessment via calibrated posterior p-values.
## Sally Paganin
## last update: June 2023
## R version 4.3.0 (2023-04-21) -- "Already Tomorrow"
## nimble version 0.13.2
##-----------------------------------------#
args <- R.utils::commandArgs(asValue=TRUE)
#######################
## Script arguments
#######################
## -- task (slurmID)
## -- dirExample
## -- dataPath
## -- runOriginal
## -- nCalibrationReplicates
## -- nIterMCMC
## -- returnSamples
## -- returnDiscrepancies
## -- calcDisc
#######################
## This script performs calculation of the CPPP multiple times to obtain
## monte carlo estiamtes; the script is set up to run on a cluster using slurm
#######################
library(nimble)
## Initialize discrepancy functions
discrepancyFunctions <- NULL
discrepancyFunctionsArgs <- NULL
## read CPPP code
source("CPPPfunctions/calculateCPPP.R")
## read registeredDiscrepancy
source("CPPPfunctions/registeredDiscrepancies.R")
##---------------------------------------- ##
if(is.null(args$dirExample)){
stop("provide directory path for the example to run")
} else {
dirExample <- args$dirExample
}
if(is.null(args$dataPath)){
stop("provide directory path for the data")
} else {
dataPath <- args$dataPath
}
if(is.null(args$nCalibrationReplicates)){
nCalibrationReplicates <- 1000
warning("Default to ", nCalibrationReplicates, " calibration replicates")
} else {
nCalibrationReplicates <- as.numeric(args$nCalibrationReplicates)
}
if(is.null(args$nIterMCMC)) {
nIterMCMC <- 200
warning("Default to ", nIterMCMC, " calibration replicates")
} else {
nIterMCMC <- as.numeric(args$nIterMCMC)
}
task <- as.numeric(args$task)
## Settings for MCMC
MCMCcontrol <- list(niter = nIterMCMC + nIterMCMC/10,
nburnin = nIterMCMC/10,
thin = 1)
## Flags
returnSamples <- if(is.null(args$returnSamples)) TRUE else as.logical(args$returnSamples)
returnDiscrepancies <- if(is.null(args$returnDiscrepancies)) TRUE else as.logical(args$returnDiscrepancies)
calcDisc <- if(is.null(args$calcDisc)) TRUE else as.logical(args$calcDisc)
parallel <- if(is.null(args$parallel)) FALSE else as.logical(args$parallel)
nCores <- if(is.null(args$nCores)) 1 else as.numeric(args$nCores)
## run original mcmc model?
runOriginal <- if(is.null(args$runOriginal)) FALSE else as.logical(args$runOriginal)
## Source model
source(paste0(dirExample, "/model.R"))
## Set loglkelihood as default discrepancy measure to run
if(is.null(discrepancyFunctions)){
discrepancyFunctions <- list(logLikDiscFunction)
discrepancyFunctionsArgs <- list(list(dataNames = names(data)))
} else {
## add default discrepancy (logLikelihood)
discrepancyFunctions <- append(discrepancyFunctions, logLikDiscFunction)
discrepancyFunctionsArgs[[length(discrepancyFunctionsArgs) + 1]] <- list(dataNames = names(data))
}
resDisc <- list()
set.seed(task)
## run calibration
out <- runCalibration(## this comes from model.R file
model = model,
dataNames = dataNames,
paramNames = paramNames,
origMCMCSamples = origMCMCSamples,
mcmcConfFun = mcmcConfFun,
discrepancyFunctions = discrepancyFunctions,
discrepancyFunctionsArgs = discrepancyFunctionsArgs,
## this comes from args or defaults
nCalibrationReplicates = nCalibrationReplicates,
MCMCcontrol = MCMCcontrol,
returnSamples = returnSamples,
returnDiscrepancies = returnDiscrepancies,
calcDisc = calcDisc,
parallel = parallel,
nCores = nCores)
saveRDS(out, file = paste0(dirExample, "/montecarlo/res_", task, ".rds"))