ll <- function(x) sum(dnorm(x, log = TRUE))
test <- createBayesianSetup(ll, prior = NULL, priorSampler = NULL, lower = -10, upper = 10)
str(test)
test$prior$density(0)
test$likelihood$density(c(1,1))
test$likelihood$density(1)
test$posterior$density(1)
test$posterior$density(1, returnAll = TRUE)
test$likelihood$density(matrix(rep(1,4), nrow = 2))
#test$posterior$density(matrix(rep(1,4), nrow = 2), returnAll = TRUE)
test$likelihood$density(matrix(rep(1,4), nrow = 4))
## Example of how to use parallelization using the VSEM model
# Note that the parallelization produces overhead and is not always
# speeding things up. In this example, due to the small
# computational cost of the VSEM the parallelization is
# most likely to reduce the speed of the sampler.
# Creating reference data
PAR <- VSEMcreatePAR(1:1000)
refPars <- VSEMgetDefaults()
refPars[12,] <- c(0.2, 0.001, 1)
rownames(refPars)[12] <- "error-sd"
referenceData <- VSEM(refPars$best[1:11], PAR)
obs = apply(referenceData, 2, function(x) x + rnorm(length(x),
sd = abs(x) * refPars$best[12]))
# Selecting parameters
parSel = c(1:6, 12)
## Builidng the likelihood function
likelihood <- function(x, sum = TRUE){
parSel = c(1:6, 12)
x = createMixWithDefaults(x, refPars$best, parSel)
predicted <- VSEM(x[1:11], PAR)
diff = c(predicted[,1:3] - obs[,1:3])
llValues = dnorm(diff, sd = max(abs(c(predicted[,1:3])),0.0001) * x[12], log = TRUE)
if (sum == F) return(llValues)
else return(sum(llValues))
}
# Prior
prior <- createUniformPrior(lower = refPars$lower[parSel], upper = refPars$upper[parSel])
####
## Definition of the packages and objects that are exported to the cluster.
# These are the objects that are used in the likelihood function.
opts <- list(packages = list("BayesianTools"), variables = list("refPars", "obs", "PAR" ),
dlls = NULL)
# Create Bayesian Setup
BSVSEM <- createBayesianSetup(likelihood, prior, best = refPars$best[parSel],
names = rownames(refPars)[parSel], parallel = 2,
parallelOptions = opts)
## The bayesianSetup can now be used in the runMCMC function.
# Note that not all samplers can make use of parallel
# computing.
# Remove the Bayesian Setup and close the cluster
stopParallel(BSVSEM)
rm(BSVSEM)
Run the code above in your browser using DataLab