R: trouble with mle() error: non-finite finite-difference value [2]

R: trouble with mle() error: non-finite finite-difference value [2]

我有一个简单的x, ydata.frame.

mydata <- data.frame(days = 1:96, risk = c(5e-09, 5e-09, 5e-09, 1e-08, 4e-08, 6e-08, 9e-08, 1.5e-07, 4.2e-07, 
                                           7.2e-07, 1.02e-06, 1.32e-06, 1.66e-06, 2.19e-06, 2.76e-06, 3.32e-06, 
                                           3.89e-06, 4.55e-06, 5.8e-06, 7.16e-06, 8.51e-06, 9.85e-06, 1.138e-05, 
                                           1.396e-05, 1.672e-05, 1.947e-05, 2.222e-05, 2.521e-05, 2.968e-05, 
                                           3.439e-05, 3.909e-05, 4.378e-05, 4.894e-05, 5.697e-05, 6.546e-05, 
                                           7.392e-05, 8.236e-05, 9.16e-05, 0.00010573, 0.00012063, 0.00013547, 
                                           0.00015025, 0.00016642, 0.00019127, 0.00021743, 0.00024343, 0.00026924, 
                                           0.00029818, 0.00034681, 0.00039832, 0.00044932, 0.00049976, 0.0005451, 
                                           0.00056293, 0.00057586, 0.00058838, 0.0006005, 0.00061562, 0.00065079, 
                                           0.00068845, 0.00072508, 0.00076062, 0.00079763, 0.00084886, 0.00090081, 
                                           0.0009507, 0.00099844, 0.00104427, 0.00108948, 0.00113175, 0.00117056, 
                                           0.00120576, 0.00123701, 0.00126253, 0.00128269, 0.00129757, 0.00130716, 
                                           0.00131291, 0.00132079, 0.0013216, 0.00131392, 0.00129806, 0.00127247, 
                                           0.00122689, 0.00117065, 0.00110696, 0.00103735, 0.00095951, 0.00085668, 
                                           0.0007517, 0.00065083, 0.000556, 0.0004669, 0.00037675, 0.00029625, 
                                           0.00093289))

从下图来看,我认为 Weibull(3, 0.155) 非常适合我的数据。

plot(1:96, dweibull(mydata$risk, shape = 3, scale = 0.155), type = "l", xlab = "days", ylab = "risk")
lines(mydata, type = "l", col = "grey")
legend("topleft", c("Data", "Estimate"), col = c("black", "grey"), lty = c(1, 1))

我编写了一个计算负对数似然的函数,它将被传递到 mle

estimate <- function(kappa, lambda){
  -sum(dweibull(mydata$y, shape = kappa, scale = lambda, log = TRUE))
}

我调用 mle,提供我的初始参数估计,并得到以下错误。

> mle(estimate, start = list(kappa = 3, lambda = 0.155))
Error in optim(start, f, method = method, hessian = TRUE, ...) : 
  non-finite finite-difference value [2]
In addition: There were 50 or more warnings (use warnings() to see the first 50)

这里出了什么问题?

你想做什么?据我所知,您有一个包含 "risk" 的 96 个值的数据集,并且您想使用 weibull 拟合它的分布。请注意,如果是这种情况,"days" 根本不相关。您有一个无序的值向量。

上图具有误导性。您计算 dweibull() 的风险值。图中表示dweibull(risk)大致等于风险。这与 weibull 的说法完全不同,给定的参数很合适。

例如,这是您的数据分布: hist(mydata$risk, breaks=15) 而您的参数在相关范围内的威布尔密度如下所示:curve((function(x) dweibull(x, shape=3, scale=0.155))(x), 0, 0.0014)

因此这些分布非常不同。我会说你的经验分布是均匀的加上零质量,而不是 weibull。

现在解决您的最后一个问题:由于分布拟合不佳,优化器会遇到数值奇点。我不太了解 mle(),但稍微调整一下 maxLik::maxLik() 就会显示问题:

estimate <- function(par){
   Kappa <- par[1]
   Lambda <- par[2]
   dweibull(mydata$risk, shape = Kappa, scale = Lambda, log = TRUE)
}
summary(maxLik::maxLik(estimate, start=c(Kappa=3, Lambda=0.155), method="BHHH"))

给你

--------------------------------------------
Maximum Likelihood estimation
BHHH maximisation, 43 iterations
Return code 2: successive function values within tolerance limit
Log-Likelihood: 682.743 
2  free parameters
Estimates:
        Estimate Std. error t value Pr(> t)    
Kappa  0.4849129  0.0473720  10.236 < 2e-16 ***
Lambda 0.0002953  0.0001028   2.873 0.00407 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
--------------------------------------------

请注意,我做了一项重大更改:从对数似然中删除 sum,并使用 BHHH 优化器。这通常比基于单个总和可能性的优化更稳定。您还应该认真考虑为估计编写解析导数。

您可以检查分布现在看起来更相似了。