What is the equivalent R function to GAMMA.INV(probability,alpha,beta) Excel function? - r

I need to find the inverse of the gamma cumulative distribution. I know there is GAMMA.INV(probability,alpha,beta) function just to do that in excel. How can I achieve this in R language?

In R, for most probability distributions,
there are four functions, called d, p, q, r,
(e.g., dnorm, pnorm, qnorm, rnorm)
giving you the density (d), cumulative distribution function (p, since the result is a probability), its inverse (q, since the result is a quantile), and r to sample from the distribution.
For the Gamma distribution, the inverse of the cumulative distribution function is therefore qgamma.

Related

How to calculate Godambe information matrix in R?

I have a likelihood function in R that I am optimizing using 'optim' and calculating the hessian matrix using hessian=T in the optim function. I want to calculate the Godambe Information matrix in R, which is defined as:
G(theta)= H(theta) J(theta)^-1 H(theta)
where J(theta) is the variability matrix and H(theta) is the sensitivity matrix.
I am not sure how to calculate these matrices in R for my likelihood function and the estimates obtained from the optim. Please help.

Is there an R function / package that can perform inverse normal distribution? [duplicate]

To plot a normal distribution curve in R we can use:
(x = seq(-4,4, length=100))
y = dnorm(x)
plot(x, y)
If dnorm calculates y as a function of x, does R have a function that calculates x as a function of y? If not what is the best way to approach this?
What dnorm() is doing is giving you a probability density function. If you integrate over that, you would have a cumulative distribution function (which is given by pnorm() in R). The inverse of the CDF is given by qnorm(); that is the standard way these things are conceptualized in statistics.
I'm not sure if the inverse of the density function is built in -- it's not used nearly as often as the inverse of the cumulative distribution function. I can't think offhand of too many situation where the inverse density function is useful. Of course, that doesn't mean there aren't any, so if you are sure this is the function you need, you could just do:
dnorminv<-function(y) sqrt(-2*log(sqrt(2*pi)*y))
plot(x, y)
points(dnorminv(y),y,pch=3)
The derivation of the inverse of the standard normal pdf is:

How do I use the pgamma() function in R to compute the CDF of a gamma distribution?

I want to compute the cumulative distribution function in R for data that follows a gamma distribution. I understood how to do this with a lognormal distribution using the equation from Wikipedia; however, the gamma equation seems more complicated and I decided to use the pgamma() function.
I'm new to this and don't understand the following:
Why do I get three different values out of pgamma, and how does it make sense that they are negative?
Am I supposed to take the log of all the quantiles, just as I used log(mean) and log(standard deviation) when doing calculations with a lognorm distribution?
How do I conceptually understand the CDF calculated by pgamma? It made sense for lognorm that I was calculating the probability that X would take a value <= x, but there is no "x" in this pgamma function.
Really appreciate the help in understanding this.
shape <- 1.35721347
scale <- 1/0.01395087
quantiles <- c(3.376354, 3.929347, 4.462594)
pgamma(quantiles, shape = shape, scale = scale, log.p = TRUE)

Inverse of the lognormal distribution

I need to find the inverse of a given lognormal distribution.
Since there is no inbuilt function in R for inverse lognormal, I need to design my own.
I have this lognormal distribution for a random variable 'x'
f_lambda <- function(x,mu,sig) {dlnorm(x, meanlog = mu, sdlog = sig,log=FALSE)}
On wikipedia it says
G(y) = 1- F(1/y)
where G(Y)n is the inverse distribution to F(X) and X= 1/Y.
But, I am confused as to how to encode F(1/y) in r and what to use to define that distribution - mu or 1/mu.
I have estimates of mu and sigma for F(x).
Thanks in advance.
In general, the quantile distribution is the inverse of a cumulative distribution. This really means:
which means that to find the inverse of the lognormal distribution you can use
qlnorm()

What does it mean to put an `rnorm` as an argument of another `rnorm` in R?

I have difficulty understanding what it means when an rnorm is used as one of the arguments of another rnorm? (I'll explain more below)
For example, below, in the first line of my R code I use an rnorm() and I call this rnorm(): mu.
mu consists of 10,000 x.
Now, let me put mu itself as the mean argument of a new rnorm() called "distribution".
My question is how mu which itself has 10,000 x be used as the mean argument of this new rnorm() called distribution?
P.S.: mean argument of any normal distribution can be a single number, and with only ONE single mean, we will have a single, complete normal. Now, how come, using 10,000 mu values still results in a single normal?
mu <- rnorm( 1e4 , 178 , 20 ) ; plot( density(mu) )
distribution <- rnorm( 1e4 , mu , 1 ) ; plot( density(distribution) )
You distribution is a conditional density. While the density you draw with plot(density(distribution)), is a marginal density.
Statistically speaking, you first have a normal random variable mu ~ N(178, 20), then another random variable y | mu ~ N(mu, 1). The plot you produce is the marginal density of y.
P(y), is mathematically an integral of joint distribution P(y | mu) * p(mu), integrating out mu.
#李哲源ZheyuanLi, ahhh! so when we use a vetor as the mean argument or sd argument of an rnorm, the single, final plot is the result of the integral, right?
It means you are sampling from the marginal distribution. The density estimate approximates the Monte Carlo integral from samples.
This kind of thing is often seen in Bayesian computation. Toy R code on Bayesian inference for mean of a normal distribution [data of snowfall amount] gives a full example, but integral is computed by numerical integration.

Resources