Learn R Programming

HelpersMG (version 2026.8.24)

PPD: Posterior Predictive Distribution from mcmcComposite object

Description

Extract quantile distributions from mcmcComposite object. If FUN is used, it keeps the correlations between parameters.

Usage

PPD(
  x,
  chain = "all",
  FUN = NULL,
  probs = c(0.025, 0.975),
  xlim = NULL,
  nameparxlim = NULL,
  namepar = NULL
)

Value

A data.frame with quantiles

Arguments

x

A mcmcComposite obtained as a result of MHalgoGen() function

chain

The number of the chain in which to get parameters or "all"

FUN

The function to apply the parameters. It must be text.

probs

The probability to get quantiles

xlim

The values to apply in fun if required

nameparxlim

The name of the parameter for xlim

namepar

The name of parameters from mcmc object to be used in fun

Author

Marc Girondot marc.girondot@gmail.com

See Also

Other mcmcComposite functions: +.PriorsmcmcComposite(), MHalgoGen(), as.mcmc.mcmcComposite(), as.parameters(), merge.mcmcComposite(), plot.PriorsmcmcComposite(), plot.mcmcComposite(), setPriors(), setPriors1(), summary.mcmcComposite()

Examples

Run this code
if (FALSE) {
library(HelpersMG)
require(coda)
x <- rnorm(30, 10, 2)
dnormx <- function(data, x) {
 data <- unlist(data)
 return(-sum(dnorm(data, mean=x['mean'], sd=x['sd'], log=TRUE)))
}

parameters_mcmc <- setPriors1(Name = "mean", 
                              Density = "dnorm", Prior1 = 10, Prior2 = 2, 
                              SDProp = 1, Min = -3, Max = 100, Init = 10)
parameters_mcmc <- parameters_mcmc + setPriors1(Name = "sd", 
                              Density = "dlnorm", Prior1 = 0.5, Prior2 = 0.5, 
                              SDProp = 1, Min = 0, Max = 10, Init = 2)
                              
mcmc_run <- MHalgoGen(n.iter=10000, parameters=parameters_mcmc, data=x, 
                     likelihood=dnormx, n.chains=1, n.adapt=100, thin=1, trace=1)

# Return the 2.5% and 97.5% quantiles of the mean parameter
k <- PPD(x=mcmc_run, namepar="mean")
k <- PPD(x=mcmc_run, namepar=NULL)
k <- PPD(x=mcmc_run, namepar=c("mean", "sd"))

# Here I return the distribution of coefficient of variation as a posterior predictive distribution
k <- PPD(x=mcmc_run            , 
                 FUN="sd/mean" )

# I can generate an output based on a x variable 
k <- PPD(x=mcmc_run                   , 
                  xlim=c(1, 2)        ,
                  nameparxlim="mult"  ,
                  FUN="mult*mean"     )
}

Run the code above in your browser using DataLab