Say I have a normal distribution parameterized with a mean ($\mu$) and precision ($\tau = 1/\sigma^2)$. In JAGS, I would specify a prior for $\tau$ as tau ~ dgamma(0.01, 0.01). Working in R, I want to randomly generate values that would follow the distribution that I've specified in JAGS.

I know that rgamma is the function for drawing values from the gamma distribution. I also know that JAGS parameterizes values differently from R.

In R, the gamma density function is parameterized as $$f(x)= 1/(s^a \Gamma(a)) x^{a-1} e^{-x/s}$$ where $a$ is the shape argument in rgamma, and $s$ is the scale argument in rgamma.

In JAGS, the gamma density function is parameterized as $$\frac{\lambda^rx^{r-1}e^{-\lambda x}}{\Gamma(r)}$$ where $\lambda>0$ and $r>0$.

I think I keep screwing up the algebra with the different parameterizations, and I'm getting confused as to what values I should specify for the arguments of rgamma in R.

I think the correct answer is rgamma(n=1, shape=0.01, rate=1/0.01); is this the correct way to generate a single value from the same ~ dgamma(0.01, 0.01) distribution I specified in JAGS? The greater idea here is to pick a reasonable distribution/ function in R for generating precision values.


1 Answer 1


The gamma density function in R,

$$f_\mathrm R(x)= 1/(s^a \Gamma(a)) x^{a-1} e^{-x/s},$$

where $a$ is the shape parameter and $s$ is the scale parameter, corresponds to the the dgamma function in JAGS,

$$f_\mathrm{JAGS}(x) = \frac{\lambda^rx^{r-1}e^{-\lambda x}}{\Gamma(r)},$$

where $r$ is the r paramater and $\lambda$ is the lambda parameter,

when we set

$$a = r, s = \frac{1}{\lambda}.$$

If you use these parameters in the R formula, you can see the correspondence:

$$f_\mathrm R(x)= 1\bigg/\left(\left(\frac{1}{\lambda}\right)^r \Gamma(r)\right) x^{r-1} e^{-\lambda x}$$

$$= \frac{\lambda^rx^{r-1}e^{-\lambda x}}{\Gamma(r)}$$

$$= f_\mathrm{JAGS}(x).$$

Hence, the equivalent to a random variable distributed ~ dgamma(0.01, 0.01) in JAGS is rgamma(n = 1, shape = 0.01, scale = 1 / 0.01) in R. The latter one is equivalent to rgamma(n = 1, shape = 0.01, rate = 0.01).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.