Dear all,
I tried to compare the bic values after fitting different distributions
The aim is, to get the best distribution with its optimal parameters into the variable "distribution"
data<-c(7.361111e+15, 6.859306e+15, 6.859306e+15, 6.859306e+15, 6.859306e+15, 6.859306e+15, 6.859306e+15, 6.859306e+15, 1.410417e+16,1.410417e+16, 1.410417e+16, 1.410417e+16, 1.410417e+16, 1.410417e+16, 1.410417e+16, 9.289236e+14, 9.289236e+14, 9.289236e+14, 9.289236e+14, 9.289236e+14 9.289236e+14)
set.seed(24)
gammabic <- -1
normbic <- -1
lognormalbic <- -1
weibullbic <- -1
Here is the fitting.
I have some datasets, which are not suitable for some distributions, which makes the tryCatch necessary
fit.gamma<- tryCatch(
{
fitdist(data, distr = "gamma", method = "mle", lower = c(0, 0), start = list(scale = 1, shape = 1))
gammabic<-fit.gamma$bic
},
error = function(e){
-1
})
fit.norm<- tryCatch(
{
fitdist(data,"norm")
normbic<- fit.norm$bic
},
error = function(e){
-1
})
fit.weibull<- tryCatch(
{
fitdist(data, distr = "weibull", method = "mle", lower = c(0, 0))
weibullbic <- fit.weibull$bic
},
error = function(e){
-1
})
fit.lognormal<- tryCatch(
{
fitdist(data, distr = "lnorm", method = "mle", lower = c(0, 0))
lognormalbic <- fit.lognormal$bic
},
error = function(e){
-1
})
Next I calculate the optimal bic value
optimal <- min(lognormalbic, weibullbic, normbic, gammabic)
Next Iwant to calculate the distribution for the optimal bic value
if(optimal == lognormalbic){
print("Lognormal")
distribution <- rlnorm(length(data), meanlog = fit.lognormal$meanlog, sdlog= fit.lognormal$sdlog)
summary(fit.lognormal)
}else if (optimal == weibullbic){
print("Weibull")
distribution <- rweibull(fit.weibull$n, shape = fit.weibull$shape, scale = fit.weibull$scale)
summary(fit.weibull)
}else if (optimal == normbic){
print("Normal")
distribution <- rnorm(fit.norm$n, mean = fit.norm$mean, sd = fit.norm$sd)
summary(fit.norm)
}else if (optimal == gammabic){
print("Gamma")
distribution <- rgamma(fit.gamma$n, scale = fit.gamma$scale, shape = fit.gamma$shape)
summary(fit.gamma) }
unfortunately this does not work.
Distributions, which are not fitting the chosen datasets are selected and then errors occur, because there is no bic calculated.
Can you please give me an hint, where I went wrong?
I am not sure were the problem is caused and why.
From my point of view it is perfectly ok and running, but the resulting Distribution is never what I was expecting.
Thank you very much in advance