I have a dataset [click here][1] and trying to find the shape and scale parameters of gamma distribution.
> library(fitdistrplus)
> H <- "US.txt"
> rate <- mean(H$All)
> n = length(H$Year)
> descdist(H$All, discrete = FALSE)
summary statistics
------
min: 0 max: 7
median: 1
mean: 1.69375
estimated sd: 1.449341
estimated skewness: 1.126422
[![possible distribution][2]][2]
#ratio of the variance to the mean
> round(var(H$All)/rate, 2)
[1] 1.24
#Is this unusual for a Poisson distribution?
#check by performing a Monte Carlo (MC) simulation #repeat this ( m=1000 ) times. Let ( n ) be the number of years in your data record and ( \lambda ) be the rate. For each sample you compute the ratio of the variance to the mean.
> set.seed(3042)
> ratio = numeric()
> m = 1000
for (i in 1:m) {
h = rpois(n = n, lambda = rate)
ratio[i] = var(h)/mean(h)
}
The vector ratio contains 1000 values of the ratio. So determine the proportion of ratios greater than 1.24
> sum(ratio > var(H$All)/rate)/m
0.028
Modify your MC simulation using the gamma distribution for the rate and then examine the ratio of variance to the mean from a set of Poisson counts with the variable rate. The gamma distribution describes the variability in the rate using the shape and scale parameters.
> ratio = numeric()
> set.seed(3042)
> m = 1000
for (i in 1:m) {
h = rpois(n = n, lambda = rgamma(m, shape = ???, scale = ???))
ratio[i] = var(h)/mean(h)
}
sum(ratio > var(H$All)/rate)/m
So I want to specify the shape to be ??? and the scale to be ??? so the product matches closely the long-term average count of the dataset. To find shape and scale I used below codes but received error.
> fitdistr(H$All, "gamma")
Error in stats::optim(x = c(1L, 3L, 0L, 2L, 1L, 2L, 1L, 1L, 1L, 3L, 2L, :
initial value in 'vmmin' is not finite
Tried weibull
> fitdistr(H$All, "weibull")
Error in fitdistr(H$All, "weibull") : Weibull values must be > 0
Why am I not getting shape and scale factor here? Additionally, if I do it manually
m= 1.449341
sd= 1.69375
> shape = (m/sd)^2
> scale = (sd)^2/m
> shape
[1] 0.7322216
> scale
[1] 1.979375
jelsner tutorial [1]: https://drive.google.com/file/d/1JcVXCaxp74w3OJJfsdVcVvKBE0OeB4zo/view?usp=sharing [2]: https://i.stack.imgur.com/oapPG.png