Search code examples
rrandomgamma-distribution

Sampling from an inverse gamma distribution in R


In order to sample from an inverse gamma distribution in R, is the following the correct way to do it:

#I want to sample an inverse-gamma(a,b)

a = 4
b = 9

x = 1/rgamma(1,a,b)

Solution

  • Although @Dason and @Stephane already commented that your approach is valid, there are several packages in R that do this (found googling for r inverse gamma:

    See also the wikipedia page for the gamma distribution and the inverse gamma distribution for the probability density function of both distributions:

    enter image description here

    for the gamma distribution versus:

    enter image description here

    for the inverse gamma.