我看到以下警告。有没有人知道为什么会出现这样的警告,尽管看起来合身似乎可以正常工作?有什么方法可以使优化工作更好,使其不会产生这些警告?
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)
请您参考如下方法:
问题是fitdistr
不限制形状和比例为正。
library(MASS)
set.seed(0)
x <- rbeta(1000, shape1=1, shape2=1)
f1 <- fitdistr(x, dbeta, list(shape1=1,shape2=1))
如果优化算法在获得不在边界上的可行解的过程中尝试一些不可行的参数值,这通常不是问题,但我同意最好尽量避免警告。
您可以自己指定下限:
...: 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))
(没有警告)。答案并不完全相同,但它们非常接近(这是从数值优化结果中可以预料到的)。
all.equal(coef(f1),coef(f2),tol=1e-6)