R/HDIofMCMC.R

Defines functions HDIofMCMC

HDIofMCMC = function( sampleVec , credMass=0.95 ) {
    # Computes highest density interval from a sample of representative values,
    #   estimated as shortest credible interval.
    # Arguments:
    #   sampleVec
    #     is a vector of representative values from a probability distribution.
    #   credMass
    #     is a scalar between 0 and 1, indicating the mass within the credible
    #     interval that is to be estimated.
    # Value:
    #   HDIlim is a vector containing the limits of the HDI
    sortedPts = sort(sampleVec)
    ciIdxInc = floor(credMass * length(sortedPts))
    nCIs = length(sortedPts) - ciIdxInc
    ciWidth = sapply(1:nCIs, function(i) sortedPts[i + ciIdxInc] - sortedPts[i])
    HDImin = sortedPts[ which.min(ciWidth)]
    HDImax = sortedPts[ which.min(ciWidth) + ciIdxInc]
    HDIlim = c(HDImin, HDImax)
    return(HDIlim)
}
laandrad/SCEDbayes documentation built on May 20, 2019, 7:31 p.m.