rwinbugs

R, HPD (Highest posterior density) interval based on samples from posterior, WinBUGS


How to calculate HPD (Highest posterior density) interval from posterior samples? I have four parameters and i generate 1000 samples from posterior parameters distribution. Now How to calculate HPD in R software. I used package code But I got an error that

    HPDinterval(winbugsresult$sims.list,prob=0.05)
    Error in UseMethod("HPDinterval") : 
    no applicable method for 'HPDinterval' applied to an object of class "list"

where "winbugsresult" is a list that contains posterior samples.

I also used a vector I got following error

    HPDinterval(winbugsresult$sims.list$alpha ,prob=0.05)
    Error in UseMethod("HPDinterval") : 
    no applicable method for 'HPDinterval' applied to an object of class "c('double', 'numeric')"

I used just a random vector from normal and i got error again

    HPDinterval(rnorm(100))
    Error in UseMethod("HPDinterval") : 
    no applicable method for 'HPDinterval' applied to an object of class "c('double', 'numeric')"

Solution

  • I'm not quite sure what the structure of winbugsresult$sims.list (you could edit your answer to include str(winbugsresult$sims.list)), but converting a numeric vector or a matrix with as.mcmc() gives you an object you can pass to HPDinterval, e.g.

    library(coda)
    HPDinterval(as.mcmc(rnorm(1000)))
    HPDinterval(as.mcmc(matrix(rnorm(1000),ncol=10)))  # matrix