我看到以下警告.有没有人知道为什么有这样的警告,尽管适合似乎工作正常?有没有办法让优化工作更好,以免产生这些警告?
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)
Run Code Online (Sandbox Code Playgroud)
问题是fitdistr不会将形状和规模限制为积极的.
library(MASS)
set.seed(0)
x <- rbeta(1000, shape1=1, shape2=1)
f1 <- fitdistr(x, dbeta, list(shape1=1,shape2=1))
Run Code Online (Sandbox Code Playgroud)
这通常不是一个问题,如果优化算法试图在途中对一个可行的解决方案,这不是在边界上的一些不可行的参数值,但我同意,最好尽量避免警告尽可能.
您可以自己指定下限:
...:"densfun"或"optim"的附加参数.特别是,它可以用于通过'lower'或'upper'或两者来指定边界.
f2 <- fitdistr(x, dbeta, list(shape1=1,shape2=1),
lower=c(0,0))
Run Code Online (Sandbox Code Playgroud)
(没有警告).答案并不完全相同,但它们非常接近(这可以从数值优化结果中得到预期).
all.equal(coef(f1),coef(f2),tol=1e-6)
Run Code Online (Sandbox Code Playgroud)