1

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.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
DellasZi
  • 13
  • 3

2 Answers2

3

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).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Ben, thank you so much! It worked! And apologies for not providing a link to the documentation. Quick note: I think you meant qgamma(0.5,2,scale=2) not gamma(0.5,2,scale=2) in the last parapgraph. Thanks again! – DellasZi Jun 06 '20 at 18:14
  • no problem, just a suggestion for future questions. – Ben Bolker Jun 06 '20 at 18:50
-1

You can try start with the inverse cumulative function,if this is not exactly what you are looking for, I suggest studying the entire theoretical basis of cumulative inversion and trying to apply it in a non-functional language first, then integrate it into R, or simply read what is written in the original MatLab function and try to reproduce in the R

luke cross
  • 321
  • 1
  • 2
  • 19