Fitdistr () with dbeta: "In densfun (x, parm [1], parm [2], ...): NaNs fired"

I see the following warnings. Does anyone know why such warnings exist even though the fit works fine? Is there a way to improve the optimization so that it does 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) 
+5
source share
1 answer

The problem is that fitdistr does not limit the shape and scale to positive.

 library(MASS) set.seed(0) x <- rbeta(1000, shape1=1, shape2=1) f1 <- fitdistr(x, dbeta, list(shape1=1,shape2=1)) 

As a rule, it’s not a problem if the optimization algorithm tries to get some invalid parameter values ​​on the way to a possible solution that is not on the border, but I agree that it is better to avoid warnings where possible.

You can define the lower bounds yourself:

...: Additional parameters, both for "densfun" and for "optim". In particular, it can be used to indicate boundaries through β€œlower” or β€œupper” or both.

 f2 <- fitdistr(x, dbeta, list(shape1=1,shape2=1), lower=c(0,0)) 

(no warnings). The answers are not exactly identical, but they are very close (this should be expected from the results of numerical optimization).

 all.equal(coef(f1),coef(f2),tol=1e-6) 
+4
source

Source: https://habr.com/ru/post/1261515/


All Articles