mohsen hs mohsen hs - 20 days ago 5
R Question

Is there a function in R which finds z if I know y in gamma(z)=y

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

gamma(z)=y

Thanks

Answer

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
Comments