View source: R/glm_logml_npp.R
glm.logml.npp | R Documentation |
Uses bridge sampling to estimate the logarithm of the marginal likelihood of a GLM under the normalized power prior (NPP).
glm.logml.npp(post.samples, bridge.args = NULL)
post.samples |
output from |
bridge.args |
a |
The function returns a list
with the following objects
"NPP"
the estimated logarithm of the marginal likelihood
an object of class bridge
or bridge_list
containing the output from using bridgesampling::bridge_sampler()
to compute the logarithm of the marginal likelihood of the normalized power prior (NPP)
Duan, Y., Ye, K., and Smith, E. P. (2005). Evaluating water quality using power priors to incorporate historical information. Environmetrics, 17(1), 95–106.
Gronau, Q. F., Singmann, H., and Wagenmakers, E.-J. (2020). bridgesampling: An r package for estimating normalizing constants. Journal of Statistical Software, 92(10).
if(requireNamespace("parallel")){
data(actg019)
data(actg036)
## take subset for speed purposes
actg019 = actg019[1:100, ]
actg036 = actg036[1:50, ]
library(parallel)
ncores = 2
data.list = list(data = actg019, histdata = actg036)
formula = cd4 ~ treatment + age + race
family = poisson()
a0 = seq(0, 1, length.out = 11)
if (instantiate::stan_cmdstan_exists()) {
## call created function
## wrapper to obtain log normalizing constant in parallel package
logncfun = function(a0, ...){
hdbayes::glm.npp.lognc(
formula = formula, family = family, a0 = a0, histdata = data.list[[2]],
...
)
}
cl = makeCluster(ncores)
clusterSetRNGStream(cl, 123)
clusterExport(cl, varlist = c('formula', 'family', 'data.list'))
a0.lognc = parLapply(
cl = cl, X = a0, fun = logncfun, iter_warmup = 500,
iter_sampling = 1000, chains = 1, refresh = 0
)
stopCluster(cl)
a0.lognc = data.frame( do.call(rbind, a0.lognc) )
## sample from normalized power prior
d.npp = glm.npp(
formula = formula,
family = family,
data.list = data.list,
a0.lognc = a0.lognc$a0,
lognc = matrix(a0.lognc$lognc, ncol = 1),
chains = 1, iter_warmup = 500, iter_sampling = 1000,
refresh = 0
)
glm.logml.npp(
post.samples = d.npp,
bridge.args = list(silent = TRUE)
)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.