Skip to content

Commit

Permalink
version 0.3-0
Browse files Browse the repository at this point in the history
  • Loading branch information
quentingronau authored and cran-robot committed Oct 22, 2017
1 parent 6ebdee2 commit b59ffd0
Show file tree
Hide file tree
Showing 43 changed files with 1,368 additions and 409 deletions.
39 changes: 21 additions & 18 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
Package: bridgesampling
Type: Package
Title: Bridge Sampling for Marginal Likelihoods and Bayes Factors
Version: 0.2-2
Version: 0.3-0
Authors@R: c(person(given="Quentin F.", family="Gronau", role=c("aut", "cre"),
email="[email protected]"), person(given="Henrik",
family="Singmann", role="aut"), person(given="Jonathan J.",
family="Forster", role="ctb"), person(given="Eric-Jan",
family="Wagenmakers", role="ths"), person(family="The JASP Team",
role="ctb"), person("Jiqiang", "Guo", role = "ctb"),
person("Jonah", "Gabry", role = "ctb"), person("Ben", "Goodrich",
role = c("ctb")))
email="[email protected]",
comment=c(ORCID="0000-0001-5510-6943")),
person(given="Henrik", family="Singmann", role="aut",
comment=c(ORCID="0000-0002-4842-3657")),
person(given="Jonathan J.", family="Forster", role="ctb"),
person(given="Eric-Jan", family="Wagenmakers", role="ths"),
person(family="The JASP Team", role="ctb"),
person("Jiqiang", "Guo", role = "ctb"),
person("Jonah", "Gabry", role = "ctb"),
person("Ben", "Goodrich", role = c("ctb")))
Depends: R (>= 3.0.0)
Imports: mvtnorm, Matrix, Brobdingnag, stringr, coda, parallel, scales,
utils, methods
Suggests: testthat, Rcpp, RcppEigen, R2jags, knitr, rmarkdown, R.rsp,
BayesFactor, rstan
Description: Provides functions for estimating marginal likelihoods, Bayes factors,
posterior model probabilities, and normalizing constants in general,
via different versions of bridge sampling (Meng & Wong, 1996,
<http://www3.stat.sinica.edu.tw/statistica/j6n4/j6n43/j6n43.htm>).
Suggests: testthat, Rcpp, RcppEigen, R2jags, rjags, runjags, knitr,
rmarkdown, R.rsp, BayesFactor, rstan, rstanarm
Description: Provides functions for estimating marginal likelihoods, Bayes
factors, posterior model probabilities, and normalizing constants in general,
via different versions of bridge sampling (Meng & Wong, 1996, <http://
www3.stat.sinica.edu.tw/statistica/j6n4/j6n43/j6n43.htm>).
License: GPL (>= 2)
LazyData: true
RoxygenNote: 6.0.1
VignetteBuilder: knitr, R.rsp
URL: https://github.com/quentingronau/bridgesampling
NeedsCompilation: no
Packaged: 2017-06-26 20:31:41 UTC; henrik
Author: Quentin F. Gronau [aut, cre],
Henrik Singmann [aut],
Packaged: 2017-10-20 09:30:18 UTC; henrik
Author: Quentin F. Gronau [aut, cre] (<https://orcid.org/0000-0001-5510-6943>),
Henrik Singmann [aut] (<https://orcid.org/0000-0002-4842-3657>),
Jonathan J. Forster [ctb],
Eric-Jan Wagenmakers [ths],
The JASP Team [ctb],
Expand All @@ -36,4 +39,4 @@ Author: Quentin F. Gronau [aut, cre],
Ben Goodrich [ctb]
Maintainer: Quentin F. Gronau <[email protected]>
Repository: CRAN
Date/Publication: 2017-06-27 08:24:15 UTC
Date/Publication: 2017-10-22 16:49:11 UTC
73 changes: 42 additions & 31 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
f5259cfd58446175f8bcd473b42a17ef *DESCRIPTION
8776b13c36d924b08d9123fdcb6bfcb7 *NAMESPACE
0eca8898aa45e38e33119bc29f0f6538 *DESCRIPTION
79fe5c8f5d7771077ae88677dc703c89 *NAMESPACE
9692a5558e0d69b54fe59315629d9e1d *NEWS
b50ec4889914e7582895c3d554a9e9fc *R/bf.R
9647e2d2c7ae46c2e5ea7ca2b55f1ed6 *R/bridge_sampler.R
054e94b0f2716e1c5c5712aa3dfd6670 *R/bridge_sampler_internals.R
5f748d1773e16401cbd7f3dd094eb1fd *R/bridge_sampler_normal.R
4e365054c8dfb7baa10d3a9e0bed72d8 *R/bridge_sampler_warp3.R
7f74b0d5732fa9ea15c7acc4c62bded6 *R/error_measures.R
ca0a36cd5a1d61c1ae95b8dc6aa39977 *R/bf.R
6ea626aa11d122470af9beacfe7ff447 *R/bridge_sampler.R
dfe66bd9723bf0a9a53e8f446f19a8b8 *R/bridge_sampler_internals.R
f11c28c68bf3fe3e920933683f05ce6c *R/bridge_sampler_normal.R
372ac2fae93318e481731c2007975efd *R/bridge_sampler_warp3.R
74c06891251ab150f40a98ccf5becb24 *R/error_measures.R
2e58141f97bc1ef12d8dac3559bec8ab *R/ier-data.R
29219953f00413bc62ec09c800e52a82 *R/logml.R
4c7f13d7280bc6532b16e896d6a98a49 *R/post_prob.R
0c7824143f10e6324c16c6eb575cd4c2 *build/vignette.rds
3b4e0cf58cabafc10438f7b271e24e24 *inst/doc/bridgesampling_example_jags.R
3d4dd3c4a26925aad838414d420fac20 *inst/doc/bridgesampling_example_jags.Rmd
f42e55757a6c828df1e8ec0113a1699d *inst/doc/bridgesampling_example_jags.html
9b3d9d31efec49adbf4c2022e653709a *R/post_prob.R
bc97086ae1bb25b528cfc88646ccc4cf *R/turtles-data.R
966b71a33bae77823284342886b35c2e *build/vignette.rds
0c51ed14d5576619c9f4c39aae27732a *data/ier.rda
0fe9eaacba1bdb2098f8fdde83f65c32 *data/turtles.rda
b423c0a78db587b8b3c2ab32a4b0c831 *inst/doc/bridgesampling_example_jags.R
0d44dcb0a849bb62392dcc770fa86eb6 *inst/doc/bridgesampling_example_jags.Rmd
2260db3153c55551c1cc679cb95e80bd *inst/doc/bridgesampling_example_jags.html
53835bfe1959e3d047710615c4fefeef *inst/doc/bridgesampling_example_stan.R
f6ee3c63d15f414dfbda31614335912c *inst/doc/bridgesampling_example_stan.Rmd
da771e30ee48025cd7b687a84e31a916 *inst/doc/bridgesampling_example_stan.html
38bb607f580d009d491469696f6abc1c *inst/doc/bridgesampling_stan_ttest.R
268c3d203c62fe38b99203b135dab09c *inst/doc/bridgesampling_stan_ttest.Rmd
b2d98dddd7670164cbcf914ca012abef *inst/doc/bridgesampling_stan_ttest.html
f92f2238d522938e94589d5b3e3928ce *inst/doc/bridgesampling_tutorial.pdf
c7ff881f12de59a82894f7282305d82e *inst/doc/bridgesampling_example_stan.html
5af550d2ef1d4328d07c0127a05bda19 *inst/doc/bridgesampling_paper.pdf
cae5f6b91b56445d6d8185ee6d9bd5cd *inst/doc/bridgesampling_paper.pdf.asis
ac6a46f89e285f5674c6a00f8ae8ee25 *inst/doc/bridgesampling_stan_ttest.R
86336d310d665c7001be2b0a832e723c *inst/doc/bridgesampling_stan_ttest.Rmd
43a8f58c0d8d4474283a0b48d5e3f359 *inst/doc/bridgesampling_stan_ttest.html
b05e71afec129b7e02e02978eb679d5b *inst/doc/bridgesampling_tutorial.pdf
0975f714b581ed709f0a23ae6cb5cb4d *inst/doc/bridgesampling_tutorial.pdf.asis
18f418f029251e655d0972e4856616e5 *man/bf.Rd
5f208595e2470e1e3fec94a5cdb1a784 *man/bridge_sampler.Rd
354147533f54d3c409795a272730b21e *man/error_measures.Rd
012b07ba0a9a95b138f776a5f28cc798 *man/bf.Rd
7d9d10e2a484dd21f361a5bf6f8644bb *man/bridge_sampler.Rd
ff7e388e306429278ceeb62d0459f0fc *man/error_measures.Rd
9ef82c96e8210238a41fd7f312944b99 *man/ier.Rd
89434ad340ebd3a1c54c6004e3050c87 *man/logml.Rd
f686f21d77c18716603c23768e829568 *man/post_prob.Rd
044d385495df7f4ec5f18daeeba81586 *man/post_prob.Rd
d7259e27e5fc8c60a848c047bf2aac42 *man/turtles.Rd
9d667770b4162f1cc0ffb5c78385fbac *tests/testthat.R
98ccba6751861e2ccb351eca05dba7c9 *tests/testthat/test-bf_print_method.R
591c457ef4698679065c69c4792c588d *tests/testthat/test-bridge_sampler_normal.R
1a1152b3a489c791a0cd87ef6a0db3f3 *tests/testthat/test-bridge_sampler_normal_Rcpp.R
14a56f0ffebe30b2218969306199b073 *tests/testthat/test-bridge_sampler_normal_Rcpp_parallel.R
1905b17e96f690d5e030cff86d9a8330 *tests/testthat/test-bridge_sampler_normal_parallel.R
fefe19c39a4fe131b15334aae8891a14 *tests/testthat/test-bf_print_method.R
591c457ef4698679065c69c4792c588d *tests/testthat/test-bridge_sampler.R
1a1152b3a489c791a0cd87ef6a0db3f3 *tests/testthat/test-bridge_sampler_Rcpp.R
14a56f0ffebe30b2218969306199b073 *tests/testthat/test-bridge_sampler_Rcpp_parallel.R
e2fbd39be6fb31325ce321f8eb538d1c *tests/testthat/test-bridge_sampler_mcmc.list.R
1905b17e96f690d5e030cff86d9a8330 *tests/testthat/test-bridge_sampler_parallel.R
1766960472cb0d775ffe291127d5686b *tests/testthat/test-bridge_sampler_print_method.R
73f79e1aa83a59a1c2d9df90e038656c *tests/testthat/test-post_prob.R
66a3fec0b90161b82b5176c938c85ded *tests/testthat/test-stan_bridge_sampler_basic.R
dfd2998bc5aeaa7b30fc96192155fbbb *tests/testthat/test-stan_bridge_sampler_bugs.R
b7225937c6f82db5b2ed9e9be4ed8331 *tests/testthat/test-stan_bridge_sampler_basic.R
0508d17f1cc088a689dc525d7732f781 *tests/testthat/test-stan_bridge_sampler_bugs.R
8d44f6e353cbaf3ac5498132f6766f87 *tests/testthat/test-stanreg_bridge_sampler_basic.R
6e8d98c9521d5c6c0e7fad8fdfc5d801 *tests/testthat/unnormalized_normal_density.cpp
ef671e9eb9ca452eae9d3a91202514ce *tests/testthat/unnormalized_normal_density_mu.cpp
3d4dd3c4a26925aad838414d420fac20 *vignettes/bridgesampling_example_jags.Rmd
0d44dcb0a849bb62392dcc770fa86eb6 *vignettes/bridgesampling_example_jags.Rmd
f6ee3c63d15f414dfbda31614335912c *vignettes/bridgesampling_example_stan.Rmd
268c3d203c62fe38b99203b135dab09c *vignettes/bridgesampling_stan_ttest.Rmd
cae5f6b91b56445d6d8185ee6d9bd5cd *vignettes/bridgesampling_paper.pdf.asis
86336d310d665c7001be2b0a832e723c *vignettes/bridgesampling_stan_ttest.Rmd
0975f714b581ed709f0a23ae6cb5cb4d *vignettes/bridgesampling_tutorial.pdf.asis
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Generated by roxygen2: do not edit by hand

S3method(bayes_factor,default)
S3method(bf,bridge)
S3method(bf,bridge_list)
S3method(bf,default)
S3method(bridge_sampler,matrix)
S3method(bridge_sampler,mcmc.list)
S3method(bridge_sampler,rjags)
S3method(bridge_sampler,runjags)
S3method(bridge_sampler,stanfit)
S3method(bridge_sampler,stanreg)
S3method(logml,bridge)
S3method(logml,bridge_list)
S3method(post_prob,bridge)
Expand All @@ -15,6 +20,7 @@ S3method(print,bf_bridge_list)
S3method(print,bf_default)
S3method(print,bridge)
S3method(print,bridge_list)
export(bayes_factor)
export(bf)
export(bridge_sampler)
export(error_measures)
Expand All @@ -33,3 +39,4 @@ importFrom(stats,pnorm)
importFrom(stats,qnorm)
importFrom(stats,var)
importFrom(stringr,str_sub)
importFrom(utils,read.csv)
43 changes: 30 additions & 13 deletions R/bf.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#" Generic function that computes Bayes factor(s) from marginal likelihoods.
#" Generic function that computes Bayes factor(s) from marginal likelihoods. \code{bayes_factor()} is simply an (S3 generic) alias for \code{bf()}.
#' @export
#' @title Bayes Factor(s) from Marginal Likelihoods
#' @param x1 Object of class \code{"bridge"} or \code{"bridge_list"} as returned from \code{\link{bridge_sampler}}. Additionally, the default method assumes that \code{x1} is a single numeric log marginal likelihood (e.g., from \code{\link{logml}}) and will throw an error otherwise.
#' @param x2 Object of class \code{"bridge"} or \code{"bridge_list"} as returned from \code{\link{bridge_sampler}}. Additionally, the default method assumes that \code{x2} is a single numeric log marginal likelihood (e.g., from \code{\link{logml}}) and will throw an error otherwise.
#' @param log Boolean. If \code{TRUE}, the function returns the log of the Bayes factor. Default is \code{FALSE}.
#' @param ... currently not used here, but can be used by other methods.
#' @details Computes the Bayes factor (Kass & Raftery, 1995) in favor of the model associated with \code{x1} over the model associated with \code{x2}.
#' @return For the default method returns a list of class \code{"bf_default"} with components:
#' \itemize{
Expand All @@ -26,14 +27,28 @@
#' \item \code{log}: Boolean which indicates whether \code{bf} corresponds to the log Bayes factor.
#' }
#' @author Quentin F. Gronau
#" @note For examples, see \code{\link{bridge_sampler}} and the accompanying vignette: \cr \code{vignette("bridgesampling_example")}
#" @note For examples, see \code{\link{bridge_sampler}} and the accompanying vignettes: \cr \code{vignette("bridgesampling_example_jags")} \cr \code{vignette("bridgesampling_example_stan")}
#' @references
#" Kass, R. E., & Raftery, A. E. (1995). Bayes factors. \emph{Journal of the American Statistical Association}, 90(430), 773-795. \url{http://dx.doi.org/10.1080/01621459.1995.10476572}
#" Kass, R. E., & Raftery, A. E. (1995). Bayes factors. \emph{Journal of the American Statistical Association, 90(430)}, 773-795. \url{http://dx.doi.org/10.1080/01621459.1995.10476572}
#' @importFrom methods is
bf <- function(x1, x2, log = FALSE) {
bf <- function(x1, x2, log = FALSE, ...) {
UseMethod("bf", x1)
}

#' @rdname bf
#' @export
bayes_factor <- function(x1, x2, log = FALSE, ...) {
UseMethod("bayes_factor", x1)
}


#' @rdname bf
#' @export
bayes_factor.default <- function(x1, x2, log = FALSE, ...) {
bf(x1, x2, log = log, ...)
}


.bf_calc <- function(logml1, logml2, log) {
bf <- logml1 - logml2
if (! log)
Expand All @@ -43,7 +58,9 @@ bf <- function(x1, x2, log = FALSE) {

#' @rdname bf
#' @export
bf.bridge <- function(x1, x2, log = FALSE) {
bf.bridge <- function(x1, x2, log = FALSE, ...) {
#name1 <- as.character(match.call()[[2]])
#name2 <- as.character(match.call()[[3]])
bf <- .bf_calc(logml(x1), logml(x2), log = log)
out <- list(bf = bf, log = log)
class(out) <- "bf_bridge"
Expand All @@ -53,7 +70,7 @@ bf.bridge <- function(x1, x2, log = FALSE) {

#' @rdname bf
#' @export
bf.bridge_list <- function(x1, x2, log = FALSE) {
bf.bridge_list <- function(x1, x2, log = FALSE, ...) {
logml1 <- x1$logml
logml2 <- x2$logml
median1 <- median(logml1, na.rm = TRUE)
Expand All @@ -75,7 +92,7 @@ bf.bridge_list <- function(x1, x2, log = FALSE) {

#' @rdname bf
#' @export
bf.default <- function(x1, x2, log = FALSE) {
bf.default <- function(x1, x2, log = FALSE, ...) {
if (!is.numeric(c(x1, x2))) {
stop("logml values need to be numeric", call. = FALSE)
}
Expand All @@ -94,23 +111,23 @@ bf.default <- function(x1, x2, log = FALSE) {
#' @export
print.bf_bridge <- function(x, ...) {
if (x$log) {
cat("The estimated log Bayes factor is equal to: ", round(x$bf, 5), sep = "")
cat("The estimated log Bayes factor in favor of x1 over x2 is equal to: ", round(x$bf, 5), sep = "")
} else if (! x$log) {
cat("The estimated Bayes factor is equal to: ", round(x$bf, 5), sep = "")
cat("The estimated Bayes factor in favor of x1 over x2 is equal to: ", round(x$bf, 5), sep = "")
}
}

#' @method print bf_bridge_list
#' @export
print.bf_bridge_list <- function(x, na.rm = TRUE,...) {
if (x$log) {
cat("The estimated log Bayes factor (based on the medians of the log marginal likelihood estimates) is equal to: ",
cat("The estimated log Bayes factor in favor of x1 over x2 (based on the medians of the log marginal likelihood estimates) is equal to: ",
round(x$bf_median_based, 5), "\nRange of estimates: ", round(range(x$bf, na.rm=na.rm)[1], 5), " to ",
round(range(x$bf, na.rm = na.rm)[2], 5),
"\nInterquartile range: ", round(stats::IQR(x$bf, na.rm = na.rm), 5), sep = "")
if (any(is.na(x$bf))) warning(sum(is.na(x$bf))," log Bayes factor estimate(s) are NAs.", call. = FALSE)
} else if (! x$log) {
cat("The estimated Bayes factor (based on the medians of the log marginal likelihood estimates) is equal to: ",
cat("The estimated Bayes factor in favor of x1 over x2 (based on the medians of the log marginal likelihood estimates) is equal to: ",
round(x$bf_median_based, 5), "\nRange of estimates: ", round(range(x$bf, na.rm=na.rm)[1], 5), " to ",
round(range(x$bf, na.rm = na.rm)[2], 5),
"\nInterquartile range: ", round(stats::IQR(x$bf, na.rm = na.rm), 5), sep = "")
Expand All @@ -122,8 +139,8 @@ print.bf_bridge_list <- function(x, na.rm = TRUE,...) {
#' @export
print.bf_default <- function(x, ...) {
if (x$log) {
cat("The log Bayes factor is equal to: ", round(x$bf, 5), sep = "")
cat("The log Bayes factor in favor of x1 over x2 is equal to: ", round(x$bf, 5), sep = "")
} else if (! x$log) {
cat("The Bayes factor is equal to: ", round(x$bf, 5), sep = "")
cat("The Bayes factor in favor of x1 over x2 is equal to: ", round(x$bf, 5), sep = "")
}
}
Loading

0 comments on commit b59ffd0

Please sign in to comment.