Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into mcmc
Browse files Browse the repository at this point in the history
  • Loading branch information
TahminaMojumder committed Nov 10, 2023
2 parents 9e8a2a0 + 1436e76 commit ff6c128
Show file tree
Hide file tree
Showing 16 changed files with 338 additions and 217 deletions.
23 changes: 23 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.Rd text
*.Rmd text
*.R text
*.r text
*.Rproj text
*.[Rr]md linguist-detectable
*.yml text


# Denote all files that are truly binary and should not be modified.
*.Rdata binary
*.RData binary
*.rda binary
*.rdb binary
*.rds binary
*.png binary
*.jpg binary
*.Rdx binary
8 changes: 4 additions & 4 deletions BayesianTools/CRAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ This file contains CRAN submission info / communication

### Submission 1

This is minor update should re-instate BT to CRAN, after it was removed due to HTML validation problems connected to the recent switch to HTML5 for documentation pages in R 4.2.0, see also https://github.com/florianhartig/BayesianTools/issues/240.
This is a minor update that should bring BT back to CRAN after it was removed due to HTML validation problems associated with the recent switch to HTML5 for documentation pages in R 4.2.0, see also https://github.com/florianhartig/BayesianTools/issues/240.

I had tried to upload a fix to CRAN, which, however, was not accepted due to detritus problems under Windows dev, which, after a long search, turned up to be caused by some stray parallel notes https://github.com/florianhartig/BayesianTools/issues/244
I had tried to upload a fix to CRAN, but it was not accepted due to detritus problems under Windows dev, which after a long search turned out to be caused by some stray parallel notes https://github.com/florianhartig/BayesianTools/issues/244.

In any case, I hope this will re-instate the BT package to CRAN, sorry for the delay in fixing this.
In any case, I hope this will get the BT package back into CRAN, sorry for the delay in fixing this.

See NEWS for other changes.

This release was tested without apparent problems under
This release has been tested without apparent problems on

* local MAC OS 13.1, R 4.2.1 Apple
* http://win-builder.r-project.org/ - oldrelease / devel / release
Expand Down
2 changes: 1 addition & 1 deletion BayesianTools/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Title: General-Purpose MCMC and SMC Samplers and Tools for Bayesian Statistics
Version: 0.1.8
Date: 2023-01-30
Authors@R: c(person("Florian", "Hartig", email = "florian.hartig@biologie.uni-regensburg.de", role = c("aut", "cre"), comment=c(ORCID="0000-0002-6255-9059")), person("Francesco", "Minunno", role = c("aut")), person("Stefan", " Paul", role = c("aut") ), person("David", "Cameron", role = "ctb"), person("Tankred", "Ott", role = "ctb"), person("Maximilian", "Pichler", role = "ctb"))
Description: General-purpose MCMC and SMC samplers, as well as plot and
Description: General-purpose MCMC and SMC samplers, as well as plots and
diagnostic functions for Bayesian statistics, with a particular focus on
calibrating complex system models. Implemented samplers include various
Metropolis MCMC variants (including adaptive and/or delayed rejection MH), the
Expand Down
16 changes: 5 additions & 11 deletions BayesianTools/R/BayesianSetupGenerateParallel.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#' Factory to generate a parallel executor of an existing function
#'
#' @author Florian Hartig
#' @param fun function to be changed to parallel execution
#' @param parallel should a parallel R cluster be used or not. If set to T, cores will be detected automatically and n-1 of the available n cores of the machine will be used. Alternatively, you can set the number of cores used by hand
#' @param parallelOptions list containing three lists. First "packages" determines the R packages necessary to run the likelihood function. Second "variables" the objects in the global environment needed to run the likelihood function and third "dlls" the DLLs needed to run the likelihood function (see Details).
#' @note Can also be used to make functions compatible with library sensitivity
#' @details For parallelization, option T means that an automatic parallelization via R is attempted, or "external", in which case it is assumed that the likelihood is already parallelized. In this case it needs to accept a matrix with parameters as columns.
#' Further you can specify the packages, objects and DLLs that are exported to the cluster.
#' By default a copy of your workspace is exported. However, depending on your workspace this can be very inefficient.
#'
#' Alternatively you can specify the environments and packages in the likelihood function (e.g. BayesianTools::VSEM() instead of VSEM()).
#' @param parallel should a parallel R cluster be used? If set to T, the operating system will automatically detect the available cores and n-1 of the available n cores will be used. Alternatively, you can manually set the number of cores to be used
#' @param parallelOptions a list containing three lists. \itemize{\item First, "packages": determines the R packages required to run the likelihood function. \item Second, "variables": the objects in the global environment needed to run the likelihood function. \item Third, "dlls": the DLLs needed to run the likelihood function (see Details).}
#' @note can be used to make functions compatible with library sensitivity
#' @details For parallelization, if option T is selected, an automatic parallelization is tried via R. Alternatively, "external" can be selected on the assumption that the likelihood has already been parallelized. In the latter case, a matrix with parameters as columns must be accepted. You can also specify which packages, objects and DLLs are exported to the cluster. By default, a copy of your workspace is exported, but depending on your workspace, this can be inefficient. As an alternative, you can specify the environments and packages in the likelihood function (e.g. BayesianTools::VSEM() instead of VSEM()).
#' @export
#' @example /inst/examples/generateParallelExecuter.R

#'
generateParallelExecuter <- function(fun, parallel = F, parallelOptions = list(variables = "all", packages = "all", dlls = NULL)){

if (parallel == F){
Expand All @@ -27,7 +22,6 @@ generateParallelExecuter <- function(fun, parallel = F, parallelOptions = list(v
#library(foreach)
#library(iterators)
# library(parallel)

if (parallel == T | parallel == "auto"){
cores <- parallel::detectCores() - 1
} else if (is.numeric(parallel)){
Expand Down
1 change: 1 addition & 0 deletions BayesianTools/R/BayesianTools.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @title BayesianTools
#' @name BayesianTools
#' @aliases BayesianTools-package
#' @docType package
#' @useDynLib BayesianTools, .registration = TRUE
#' @description A package with general-purpose MCMC and SMC samplers, as well as plots and diagnostic functions for Bayesian statistics
Expand Down
1 change: 1 addition & 0 deletions BayesianTools/R/classBayesianSetup.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#' Creates a standardized collection of prior, likelihood and posterior functions, including error checks etc.
#' @author Florian Hartig, Tankred Ott
#' @param likelihood log likelihood density function
Expand Down
12 changes: 7 additions & 5 deletions BayesianTools/R/classMcmcSampler.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ getSample.mcmcSampler <- function(sampler, parametersOnly = T, coda = F, start =
#' @method summary mcmcSampler
#' @author Stefan Paul
#' @export
summary.mcmcSampler <- function(object, ...){
summary.mcmcSampler <- function(object, printCorrelation = "auto", ...){
#codaChain = getSample(sampler, parametersOnly = parametersOnly, coda = T, ...)
#summary(codaChain)
#rejectionRate(sampler$codaChain)
Expand All @@ -133,8 +133,7 @@ summary.mcmcSampler <- function(object, ...){

mcmcsampler <- sampler$settings$sampler
runtime <- sampler$settings$runtime[3]
correlations <- round(cor(getSample(sampler)),3)


chain <- getSample(sampler, parametersOnly = T, coda = T, ...)
# chain <- getSample(sampler, parametersOnly = T, coda = T)
if("mcmc.list" %in% class(chain)){
Expand Down Expand Up @@ -203,8 +202,11 @@ summary.mcmcSampler <- function(object, ...){

try(cat("## DIC: ", round(DInf$DIC,3), "\n"), silent = TRUE)
cat("## Convergence" ,"\n", "Gelman Rubin multivariate psrf: ", conv, "\n","\n")
cat("## Correlations", "\n")
print(correlations)
if(printCorrelation == TRUE){
correlations <- round(cor(getSample(sampler)),3)
cat("## Correlations", "\n")
print(correlations)
}
}

#' @author Florian Hartig
Expand Down
25 changes: 24 additions & 1 deletion BayesianTools/R/mcmcRun.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,30 @@ runMCMC <- function(bayesianSetup , sampler = "DEzs", settings = NULL){
#' @param settings optional list with parameters that will be used instead of the defaults
#' @param sampler one of the samplers in \code{\link{runMCMC}}
#' @param check logical determines whether parameters should be checked for consistency
#' @details see \code{\link{runMCMC}}
#' @details
#'
#' The following settings can be used for all MCMCs:
#'
#' startValue (no default) start values for the MCMC. Note that DE family samplers require a matrix of #' start values. If startvalues are not provided, they are sampled from the prior.
#'
#' iterations (10000) the MCMC iterations
#'
#' burnin (0) burnin
#'
#' thin (1) thinning while sampling
#'
#' consoleUpdates (100) update frequency for console updates
#'
#' parallel (NULL) whether parallelization is to be used
#'
#' message (TRUE) if progress messages are to be printed
#'
#' nrChains (1) the number of independent MCMC chains to be run. Note that this is not controlling the #' internal number of chains in population MCMCs such as DE, so if you run nrChains = 3 with a DEzs #' #' startValue that is a 4xparameter matrix (= 4 internal chains), you will run independent DEzs runs #' #' with 4 internal chains each.
#'
#' For more details, see \code{\link{runMCMC}}
#'
#'
#' @example inst/examples/mcmcRun.R
#' @export
applySettingsDefault<-function(settings=NULL, sampler = "DEzs", check = FALSE){

Expand Down
4 changes: 2 additions & 2 deletions BayesianTools/R/plotMarginals.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ marginalPlotDensity <- function(posteriorMat, priorMat = NULL, xrange = NULL, co
mtext('Marginal parameter uncertainty', outer = TRUE, cex = 1.5)

} else {
mfrow <- if (nPar < 16) getPanels(nPar) else c(4,4)
mfrow <- getPanels(nPar)

op <- par(mfrow = mfrow, mar = c(4.5, 4, 5, 3), oma=c(3, 1.5, 2, 0), xpd=TRUE)
on.exit(par(op))
Expand Down Expand Up @@ -282,7 +282,7 @@ marginalPlotViolin <- function(posteriorMat, priorMat = NULL, xrange = NULL, col
mtext('Marginal parameter uncertainty', outer = TRUE, cex = 1.5)

} else {
mfrow <- if (nPar < 16) getPanels(nPar) else c(4,4)
mfrow <- getPanels(nPar)

op <- par(mfrow = mfrow, mar = c(4.5, 4.5, 5, 3), oma=c(3, 0, 2, 0), xpd=TRUE)

Expand Down
7 changes: 5 additions & 2 deletions BayesianTools/inst/examples/mcmcRun.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ ll <- generateTestDensityMultiNormal(sigma = "no correlation")
## is the recommended way of using the runMCMC() function.
bayesianSetup <- createBayesianSetup(likelihood = ll, lower = rep(-10, 3), upper = rep(10, 3))

## Finally we can run the sampler and have a look
settings = list(iterations = 1000, adapt = FALSE)
## Finally we can run the sampler. To get possible settings
## for a sampler, see help or run applySettingsDefault(sampler = "Metropolis")
settings = list(iterations = 1000, adapt = FALSE) #
out <- runMCMC(bayesianSetup = bayesianSetup, sampler = "Metropolis", settings = settings)

## out is of class bayesianOutput. There are various standard functions
Expand All @@ -21,3 +22,5 @@ summary(out)
# for plotting and analysis

codaObject = getSample(out, start = 500, coda = TRUE)


1 change: 1 addition & 0 deletions BayesianTools/man/BayesianTools.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 47 additions & 1 deletion BayesianTools/man/applySettingsDefault.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions BayesianTools/man/generateParallelExecuter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions BayesianTools/man/runMCMC.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ff6c128

Please sign in to comment.