Inverse of the lognormal distribution - r

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

Related

How to use the gamma distribution equation

I am using R to fit a GLM with Gamma distribution (link inverse). I would like to use the equation of the model to get other values of my predictors, knowing the response value. I know that the equation of a gamma distribution with link inverse is 1/μ = b0 + b1x1i, can you confirm that I should substitute to μ the mean of my response value?

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)

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.

R : How to obtain the fitting values from distribution fit?

I fit gamma distribution on empirical distribution function using the $fitdist$ function:
fit = fitdist(data=empdistr,distr="gamma")
I then use the $denscomp$ function to compare data to fitted values:
dc = denscomp(fit)
But I would like to extract from $fit$ or from $dc$ the actual fitted values, i.e. the points of the gamma density (with the fitted parameters) which are displayed in the $denscomp$ function.
Does anybody have an idea of how I can do that.
Thanks in advance!
Use dgamma to predict the density for a given quantile:
dgamma(x, coef(fit)[1], coef(fit)[2])

Resources