quantile for gamma distribution

Why I got "inf" with probability=1?

qgamma(1, shape=3.5, rate = 1.5, lower.tail = TRUE, log.p = FALSE)

The qgamma function returns the value on the x axis below which the given fraction of the probability density lies. By setting the first argument to one, you are asking for the point on the x axis below which all of the probability density lies. But the gamma distribution has no upper bound. The returned value of Inf just shows that there is no value above which the probability density is exactly zero.

What question are you trying to answer by entering one as the first argument?

Thank you so much for your clarification. I was trying to get the quantiles from a given gamma distribion using a vector of probabilites.
For example:

  pp=pgamma(x, shape=shape, scale=scale)
  qgamma(pp, shape=shape, scale=scale)

I was expecting ~100 instead of "InF" in the last line of the code.

You cannot invert the calculation of pgamma(100, shape=shape, scale=scale) with qgamma because of the limits of precision in the calculation. Any number above about 60, given the shape and scale values you used, returns a pgamma of exactly 1. If you put 1 into qgamma, you get Inf. Look at the result of this modification of your code.

x=c(2,5, 60, 80, 100)
pp=pgamma(x, shape=shape, scale=scale)
format(pp, digits = 15)
qgamma(pp, shape=shape, scale=scale)

I used the format() function as a quick way to see more digits in the pp values.

1 Like

Hi @FJCC , In such case of having "inf" value. Do you think I can go ahead with

ifelse(qgamma(pp, shape=3.5, scale=1.5)=="Inf",
       qgamma(pp-0.0000001, shape=3.5, scale=1.5),
       qgamma(pp, shape=3.5, scale=1.5)

which I am trying to get the maximum quantile of a given shape and scale parameter?

Thanks again

That might be a reasonable thing to do under the right circumstances but I can't really say without knowing a lot about what you need to do. I have to assume you know if ignoring values above the 0.99999999 quantile makes sense.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.