rbayesianhierarchical-bayesiannimble

How to set up a Half-Normal Prior in NIMBLE?


Can you give me example to write half-normal prior using Nimble package in R?

For example, if I want my precision variable "tau.b" to follow a Half-Normal(0, 2.5), will it be correct if I put it as below?

for (k in 1:nvar) {b[k] ~ dnorm(mu.b[k],tau.b[k])}

#HALF NORMAL prior for the precision of b[]

tau.b ~ dnorm(0, 2.5)

tau.b ~ dconstraint(tau.b>0)

Thanks in advance!

PS: I have tried to read the Nimble manual page 53, but it is still not clear to me of how to apply it.


Solution

  • I think your method (setting tau.b ~ dnorm(...) and then using constraint_data ~ dconstraint(tau.b > 0) should work (the manual specifies that you should set constraint_data to 1 in the data provided to the model), but might be overkill/less efficient than specifying a truncated distribution, as

    tau.b[k] ~ T(dnorm(0, sd = 2.5), 0, )
    

    Leaving the last argument blank declares that there is only lower truncation at 0, with no truncation at the upper limit.

    Why don't you try them both on a toy example and see how it goes?

    (It's not clear to me whether you want a single joint prior on the precision or a single joint prior — the former seems more likely, but you refer to both tau.b[k] and tau.b in your example ...)

    The manual does say that methods that rely on automatic differentiation won't work with truncation; my guess is that dconstraint() would break AD as well.


    See Section 5.2.7.1, "Truncation" (this is on p. 52, not 53)

    Either of the following forms,

       x ~ dnorm(0, sd = 10) T(0, a)
       x ~ T(dnorm(0, sd = 10), 0, a)
    

    declares that x follows a normal distribution between 0 and a (inclusive of 0 and a). Either boundary may be omitted or may be another node, such as a in this example. The first form is compatible with JAGS, but in NIMBLE it can only be used when reading code from a text file. When writing model code in R, the second version must be used. Truncation means the possible values of x are limited a priori, hence the probability density of x must be normalized[8]. In this example it would be the normal probability density divided by its integral from 0 to a. Like JAGS, NIMBLE also provides I as a synonym for T to accommodate older BUGS code, but T is preferred because it disambiguates multiple usages of I in BUGS.