Search code examples
rmatlabstatisticsprobabilitygamma-distribution

equivalent of MATLAB function gaminv()


I have been struggling for quite a while trying to find an R equivalent to MATLAB's gaminv function. To my knowledge, there are no packages/functions in R that do exactly what `gaminv does. I am not sure how to code it up myself either. Any guidance/advice would be greatly appreciated.


Solution

  • It would help to give us a link to the documentation:

    x = gaminv(p,a,b) returns the icdf [inverse cumulative distribution function] of the gamma distribution with shape parameter a and the scale parameter b, evaluated at the values in p.

    The inverse cumulative distribution function is also called the quantile function: these functions are denoted as q<distname> in R. In particular, qgamma is the quantile function of the Gamma distribution: the definition from ?qgamma states

    qgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE, log.p = FALSE)

    In particular, be careful that the R function is parameterized with the rate by default: if you specify qgamma(0.5, shape=2, 2) you'll get the value of the function with rate 2 (scale 1/2). If you want the scale parameterization you need qgamma(0.5, shape=2, scale=2).

    I've confirmed using an arbitrary example that gaminv(0.5,2,2) (in Octave, since I don't have Matlab) gives the same answer as qgamma(0.5,2,scale=2).