-1

I am looking to know if there is any function in R which finds z if I know y and

gamma(z)=y

Uniroot might be useful but not sure how I can use this.

Thanks

Mohsen Sichani
  • 1,002
  • 12
  • 33
  • 3
    Maybe not easy: http://math.stackexchange.com/questions/18362/is-there-an-inverse-gamma-gamma-1-z-function (although that question is about an inverse over the complex numbers). Also http://mathforum.org/kb/message.jspa?messageID=342551&tstart=0 , which gives a way to implement an *approximate* inverse gamma. – Ben Bolker Nov 15 '16 at 22:18

1 Answers1

3

From a mathforum post:

Let k denote the positive zero of the digamma function, approximately 1.461632 ...

c = Sqrt(2*pi)/e - Gamma(k) ...

... Leting L(x) = ln((x+c)/Sqrt(2*pi)), the inverse of my gamma approximation is

ApproxInvGamma or AIG(x) = L(x) / W(L(x) / e) + 1/2.

k <- 1.461632
cc <- sqrt(2*pi)/exp(1)-gamma(k)
L <- function(x) {
    log((x+cc)/sqrt(2*pi))
}
AIG <- function(x) {
    Lx <- L(x)
    Lx/(emdbook::lambertW(Lx*exp(-1))) + 1/2
}


par(las=1,bty="l")
curve(1-AIG(gamma(x))/x,from=2,to=20,
      ylab="relative error of approximation")

Alternatively you can use uniroot():

AIG(5)
ufun <- function(x=5) {
   uniroot(function(z) gamma(z)-x,c(1.00001,10))$root
}
ufun(5) ## 3.852341
AIG(5)  ## 3.848149
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453