right distribution with bic value

Dear all,
I tried to compare the bic values after fitting different distributions

``````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

Your handling of the `tryCatch`is not correct. See my example of the `myfit` variable where I use the result of the `tryCatch` block inside the block itself.
You are comparing the results of various fits. First make sure each individual fit is handled correctly.

For this particular dataset (indeed a strange one) a log transformation could be done before the fit (?)

``````library(fitdistrplus)

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

myfit = tryCatch ({
x = 3
y = myfit
},
error = function(e) {
-1
})

myfit
#> [1] -1

fitdist(data, distr = "gamma")
#> <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, ...): non-finite finite-difference value [2]>
#> Error in fitdist(data, distr = "gamma"): the function mle failed to estimate the parameters,
#>                 with the error code 100

fitdist(log(data), distr = "gamma")
#> Fitting of the distribution ' gamma ' by maximum likelihood
#> Parameters:
#>        estimate Std. Error
#> shape 1064.6688 328.472126
#> rate    29.4622   9.091826
``````

Created on 2020-07-07 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.