19 thoughts on “The BayesianTools R package with general-purpose MCMC and SMC samplers for Bayesian statistics

    • It’s the processmodel you want to fit, and your observed data that you want to use to calibrate this model. As written in the text, this is pseudocode, it’s not supposed to run, you have to adjust this to your specific problem.


  1. Dear Florian Hartig,

    Thank you for your informations and package. I have been using this BayesianTools for estimating Posterior distribution of non-communicable disease model. I have a question “How can I turn these parameter chains into outcome chains”? Do you have an example code in your package if this is possible directly from the chains or if we have to rerun the model with parameters from the chains.

    Best regards,


  2. Is the lower plot using plotTimeSeriesResults()? I had trouble getting this to work because I do not have an observation at every time step, and it didn’t seem to like NA. Does the error function have to return a vector of absolute errors corresponding to the “mean”?


    • Yes. I had thought NAs should work, but maybe it doesn’t – I have filed this as a question at https://github.com/florianhartig/BayesianTools/issues/102

      Thanks for the hint with the error function, it’s really not clear in the help. I updated the help to

      @param error function with signature f(mean, par) that generates observations with error (error = stochasticity according to what is assumed in the likelihood) from mean model predictions. Par is a vector from the matrix with the parameter samples (full length). f needs to know which of these parameters are parameters of the error function. See example in \code{\link{VSEM}}


  3. Thanks for this fantastic tool!
    I’m trying to calibrate my ecosystem model and in order to speed things up, I am calculating a cost function within the model. I wrote a wrapper to execute the model from within R (model is in Fortran) and am passing a single parameter (which is to be calibrated) through a system call from R to execute the model (parameter value is read from standard input). A single cost value is returned and read into R from the standard output. My aim was that this replaces having to calculate the log-likelihood as is described here. However, it doesn’t seem to work and I don’t understand why.
    The general question is: Can one use any function that is to be maximised within runMCMC()?
    I did:
    negcost_function <- function( par ){
    ## this executes the model and reads the cost that is returned through the standard output (very last line)
    cost = system( paste0("echo fcover ", sprintf( "%f", par ), " | ./runcmodel_simsuite | tail -n 1"), intern = TRUE)
    ## return the negative of the cost!
    setup <- createBayesianSetup( negcost_function, lower = 1, upper = 20 )
    settings <- list( iterations = 1000, message = FALSE )
    out <- runMCMC( bayesianSetup = setup, sampler = "DEzs", settings = settings )

    Liked by 1 person

    • yes, in principle this should work for any function that is to be maximized … what’s the error? Do you really have only 1 parameter, as your lower/upper suggests? if so, why use MCMC? For more technical questions, possibly better to use issues ob the BT github repo. best f

      Liked by 1 person

  4. Dear Florian,
    Thank you very much for this fantastic package. I love it. Here, I have a quick question about the proposal function used by runMCMC (sampler = “Metropolis”). I am wondering whether I can specify the proposal function by myself. If so, how? For a parameter P, for example, I want to use this proposal function: P_new = P_old + r * (P_max – P_min) / D. How can I incorporate this function into “runMCMC”? How about different proposal functions for different parameters?
    I had briefly checked the code of runMCMC as well as Metropolis, but cannot work out how the proposal is generated. I appreciate any details about this.


  5. Dear Florian,

    I just want to say thank you for this fantastic Bayesian tool. I’ve been using it for estimating posterior distributions of parameters for complex civil engineering models.


  6. Dear Florian

    Thank you for this good job!

    I want to use BayesianTool package to make sensitivity analysis of a Bayesian Model.
    Have you some research or example to do such analysis.


  7. Hi, Thanks gor such great tutorial. I have been trying to use this “Bayesiantools” package to estimate water column depth and benthic albedo from water leaving radiance as an input. I have a forward model which is used to calculate the estimated observations of water leaving radiance in order to generate the likelihood function. I also have created a set of prior for the parameters but when I try to run the sampling including the priors, The runMCMC function returns an error. Can you shed some light on this please?


  8. Hi Florian,

    Thank you for a fantastic package and a great tutorial. I have been using BayesianTools to fit a highly non-linear ODE model to longitudinal data. Some of the model parameters have very different magnitudes (~ 10^-8 vs ~ 10^4).

    To make the fitting easier, I was wondering if BayesianTools allows parameter transformations to log10 or log scale when defining the prior densities? Would you just add the Jacobian adjustment into the definition of the prior density itself?


    • Hello Tuulia,

      sure, just transform you parameters in whatever way you like in the likelihood / prior. So, e.g. in the likelihood write

      parT = log(par)

      and the same for the prior. How to exactly set the prior depends on your problem, so I can’t say what’s sensible from the outside.



      • Thank you so much Florian for your quick response! I was getting confused because of how parameter transformations are implemented in STAN, but that works perfectly.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s