Learn R Programming

RBaM (version 1.1.1)

logPosterior_BaM: BaM log-posterior

Description

Log-posterior engine for a model available in BaM.

Usage

logPosterior_BaM(
  parvector,
  X,
  Yobs,
  Yu,
  lpfunk,
  llfunk,
  mod,
  Ysim = NULL,
  logLikelihood_engine = logLikelihood_BaM,
  llargs = NULL
)

Value

A list with the following components:

logPosterior

numeric, the unnormalized posterior log-pdf.

logPrior

numeric, the prior log-pdf.

logLikelihood

numeric, the log-likelihood.

Ysim

data frame, the model-simulated values.

Arguments

parvector

numeric vector, parameter vector, including thetas (model parameters) and gammas (structural errors parameters).

X

data frame, model inputs.

Yobs

data frame, corresponding observed values.

Yu

data frame, measurement uncertainties (standard deviations), same dimensions as Yobs.

lpfunk

function, function computing the log-prior density of parvector.

llfunk

function, function computing the log-likelihood given Ysim, see e.g. llfunk_iid_Gaussian.

mod

model object, the model to be calibrated.

Ysim

data frame, model-simulated values. When NULL (default), the model is run to provide simulations. When a non-NULL data frame is provided, it is used as pre-computed simulations, and the model is hence not run. This is useful to speed-up some MCMC strategies.

logLikelihood_engine

function, engine function used to compute the log-likelihood, see e.g. logLikelihood_BaM. Unlike functions llfunk, which computes the log-likelihood from model-simulated values Ysim (see e.g. llfunk_iid_Gaussian, logLikelihood_engine computes the log-likelihood from (parameters + inputs X + model mod).

llargs

object, any other arguments to be passed to llfunk.

Examples

Run this code
# Single-control rating curve model - see https://github.com/BaM-tools/RBaM
# Parameters are activation stage k, coefficient a and exponent c
k=parameter(name='k',init=-0.5)
a=parameter(name='a',init=100)
c=parameter(name='c',init=1.6)
# Define control matrix: columns are controls, rows are stage ranges.
controlMatrix=matrix(1,nrow=1,ncol=1)
# Stitch it all together into a model object
M=model(ID='BaRatin',
        nX=1,nY=1, # number of input/output variables
        par=list(k,a,c), # list of model parameters
        xtra=xtraModelInfo(object=controlMatrix)) # use xtraModelInfo() to pass the control matrix
# Define calibration data
X=SauzeGaugings['H']
Yobs=SauzeGaugings['Q']
Yu=SauzeGaugings['uQ']
# Define the parameter vector (model parameters + structural error parameters)
parvector=c(RBaM::getInitPar(M$par),c(1,0.1))
# Define prior function - here for instance just an informative prior on the third parameter
myPrior <-function(parvector){dnorm(parvector[3],1.6,0.1,log=TRUE)}
# Compute log-likelihood
logPosterior_BaM(parvector=parvector,X=X,Yobs=Yobs,Yu=Yu,
                 lpfunk=myPrior,llfunk=llfunk_iLinear_Gaussian,mod=M)

Run the code above in your browser using DataLab