R:mle()错误的问题:非有限的有限差分值[2]

时间:2017-09-20 01:42:09

标签: r statistics estimation mle

我有一个简单的x, y data.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))

enter image description here

我编写了一个计算负对数似然的函数,该函数将传递给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)

这里出了什么问题?

1 个答案:

答案 0 :(得分:2)

你想做什么?据我所知,你有一个96值&#34; risk&#34;的数据集,你希望它与weibull一致。请注意&#34;天&#34;如果是这样的话,根本不相关。你有一个无序的值向量。

上图是误导性的。您为风险值计算dweibull()。该图表明dweibull(risk)大致等于风险。这是一个与威布尔相当不同的主张,给定的参数非常适合。

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

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

现在回到最后一个问题:由于分布不合适,优化器会遇到数值奇点。我不太了解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优化器。这通常比基于单个总和可能性的优化更稳定。您还应该认真考虑编写分析衍生物进行估算。

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