I see the following warnings. Does anyone know why there are such warnings despite the fact that the fit seems to work OK? Is there any way to make the optimization work better so that it will not generate these warnings?
R> library(MASS)
R> set.seed(0)
R> x=rbeta(1000, shape1=1, shape2=1)
R> fitdistr(x, dbeta, list(shape1=1,shape2=1))
shape1 shape2
1.00959537 0.99603351
(0.04183720) (0.04116276)
Warning messages:
1: In densfun(x, parm[1], parm[2], ...) : NaNs produced
2: In densfun(x, parm[1], parm[2], ...) : NaNs produced
R> x=rbeta(1000, shape1=10, shape2=10)
R> fitdistr(x, dbeta, list(shape1=1,shape2=1))
shape1 shape2
8.5038157 8.5794416
(0.3749814) (0.3784147)
The problem is that fitdistr
does not constrain the shape and scale to be positive.
library(MASS)
set.seed(0)
x <- rbeta(1000, shape1=1, shape2=1)
f1 <- fitdistr(x, dbeta, list(shape1=1,shape2=1))
It is usually not a problem if the optimization algorithm tries some infeasible parameter values on the way to a feasible solution that's not on the boundary, but I agree it's better to try to avoid warnings wherever possible.
You can specify lower bounds yourself:
...: Additional parameters, either for ‘densfun’ or for ‘optim’. In particular, it can be used to specify bounds via ‘lower’ or ‘upper’ or both.
f2 <- fitdistr(x, dbeta, list(shape1=1,shape2=1),
lower=c(0,0))
(no warnings). The answers aren't exactly identical, but they're very close (this is to be expected from numerical optimization results).
all.equal(coef(f1),coef(f2),tol=1e-6)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With