Gamma distribution parameters from mean and variance of data distribution
set.seed(31234)
x <- rgamma(100, shape=2.0, rate=11.0)
hist(x)
#print(x)
shape <- 2.0
rate <- 11.0
scale <- 1/rate
m <- mean(x)
print(shape * scale)
print(shape * 1 / rate)
v <- var(x)
print(shape * scale^2)
print(shape * (1 / rate) ^2)
print(m)
print(v)
scale <- v/m
shape <- m*m/v
rate <- 1/scale
print(shape)
print(rate)
library(fitdistrplus)
fitdist(data=x, distr="gamma", method = "mle")
fitdist(data=x, distr="gamma", method = "mme")
Note that the gamma distribution cannot be fitted when standard deviation (and then variance) is null:
scale <- v/m # is 0
shape <- m*m/v # is +Inf
rate <- 1/scale # is +Inf
And
> x <- rep(100, 1000)
> library(fitdistrplus)
> fitdist(data=x, distr="gamma", method = "mle")
<simpleError in optim(par = vstart, fn = fnobj, fix.arg = fix.arg, obs = data, gr = gradient, ddistnam = ddistname, hessian = TRUE, method = meth, lower = lower, upper = upper, ...): valeur non-finie fournie par optim>
Error in fitdist(data = x, distr = "gamma", method = "mle") :
the function mle failed to estimate the parameters,
with the error code 100
> fitdist(data=x, distr="gamma", method = "mme")
Fitting of the distribution ' gamma ' by matching moments
Parameters:
estimate
shape Inf
rate Inf
Warning message:
In dgamma(c(100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, :
production de NaN
> x <- c(rep(100, 1000), 101)
> fitdist(data=x, distr="gamma", method = "mme")
Fitting of the distribution ' gamma ' by matching moments
Parameters:
estimate
shape 10020210.2
rate 100201.1
x <- rgamma(100, shape=2.0, rate=11.0)
hist(x)
#print(x)
shape <- 2.0
rate <- 11.0
scale <- 1/rate
m <- mean(x)
print(shape * scale)
print(shape * 1 / rate)
v <- var(x)
print(shape * scale^2)
print(shape * (1 / rate) ^2)
print(m)
print(v)
scale <- v/m
shape <- m*m/v
rate <- 1/scale
print(shape)
print(rate)
library(fitdistrplus)
fitdist(data=x, distr="gamma", method = "mle")
fitdist(data=x, distr="gamma", method = "mme")
Note that the gamma distribution cannot be fitted when standard deviation (and then variance) is null:
scale <- v/m # is 0
shape <- m*m/v # is +Inf
rate <- 1/scale # is +Inf
And
> x <- rep(100, 1000)
> library(fitdistrplus)
> fitdist(data=x, distr="gamma", method = "mle")
<simpleError in optim(par = vstart, fn = fnobj, fix.arg = fix.arg, obs = data, gr = gradient, ddistnam = ddistname, hessian = TRUE, method = meth, lower = lower, upper = upper, ...): valeur non-finie fournie par optim>
Error in fitdist(data = x, distr = "gamma", method = "mle") :
the function mle failed to estimate the parameters,
with the error code 100
> fitdist(data=x, distr="gamma", method = "mme")
Fitting of the distribution ' gamma ' by matching moments
Parameters:
estimate
shape Inf
rate Inf
Warning message:
In dgamma(c(100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, :
production de NaN
> x <- c(rep(100, 1000), 101)
> fitdist(data=x, distr="gamma", method = "mme")
Fitting of the distribution ' gamma ' by matching moments
Parameters:
estimate
shape 10020210.2
rate 100201.1
Commentaires
Enregistrer un commentaire