A wrapper around
bridge_sampler that automatically
computes likelihood part dependent on the prior distribution
and prepares parameter samples. log_posterior
must
specify a function that takes two arguments - a named list
of samples from the prior distributions and the data, and returns
log likelihood of the model part.
JAGS_bridgesampling(
fit,
log_posterior,
data = NULL,
prior_list = NULL,
formula_list = NULL,
formula_data_list = NULL,
formula_prior_list = NULL,
add_parameters = NULL,
add_bounds = NULL,
maxiter = 10000,
silent = TRUE,
...
)
model fitted with either runjags posterior samples obtained with rjags-package
function that takes a named list of samples, the data,
and additional list of parameters passed as ...
as input and
returns the log of the unnormalized posterior density of the model part
list containing data to fit the model (not including data for the formulas)
named list of prior distribution
(names correspond to the parameter names) of parameters not specified within the
formula_list
named list of formulas to be added to the model (names correspond to the parameter name created by each of the formula)
named list of data frames containing data for each formula (names of the lists correspond to the parameter name created by each of the formula)
named list of named lists of prior distributions
(names of the lists correspond to the parameter name created by each of the formula and
the names of the prior distribution correspond to the parameter names) of parameters specified
within the formula
vector of additional parameter names that should be used
in bridgesampling but were not specified in the prior_list
list with two name vectors ("lb"
and "up"
)
containing lower and upper bounds of the additional parameters that were not
specified in the prior_list
maximum number of iterations for the bridge_sampler
whether the progress should be printed, defaults to TRUE
additional argument to the bridge_sampler
and log_posterior
function
JAGS_bridgesampling
returns an object of class 'bridge'.
if (FALSE) {
# simulate data
set.seed(1)
data <- list(
x = rnorm(10),
N = 10
)
data$x
# define priors
priors_list <- list(mu = prior("normal", list(0, 1)))
# define likelihood for the data
model_syntax <-
"model{
for(i in 1:N){
x[i] ~ dnorm(mu, 1)
}
}"
# fit the models
fit <- JAGS_fit(model_syntax, data, priors_list)
# define log posterior for bridge sampling
log_posterior <- function(parameters, data){
sum(dnorm(data$x, parameters$mu, 1, log = TRUE))
}
# get marginal likelihoods
marglik <- JAGS_bridgesampling(fit, log_posterior, data, priors_list)
}