如何通过optim()返回关闭估算值启用优化?

时间:2018-11-16 08:17:33

标签: r nonlinear-optimization mle

首先,我需要澄清的是,我已经阅读了以下帖子,但仍然无法解决我的问题:

  1. R optim() L-BFGS-B needs finite values of 'fn' - Weibull

  2. Optimization of optim() in R ( L-BFGS-B needs finite values of 'fn')

  3. R optimize multiple parameters

  4. optim function with infinite value

下面是进行仿真并进行最大似然估计的代码。

    #simulation
    #a0, a1, g1, b1 and d1 are my parameters
    #set true value of parameters to 
    #simulate a set of data with size 2000
    #x is the simulated data sets

    set.seed(5)
    a0 = 2.3; a1 = 0.05; g1 = 0.68; b1 = 
    0.09; d1 = 2.0; n=2000

    x = h = rep(0, n)

    h[1] = 6
    x[1] = rpois(1,h[1])

     for (i in 2:n) {

      h[i] = (a0 + a1 *
            (abs(x[i-1]-h[i-1])-g1*(x[i-1]- 
            h[i-1]))^d1 +
            b1 * (h[i-1]^d1))^(1/d1)
      x[i] = rpois(1,h[i])
    }

      #this is my log-likelihood function
       ll <- function(par) {
          h.n <- rep(0,n)
          a0 <- par[1]  
          a1 <- par[2] 
          g1 <- par[3]
          b1 <- par[4]
          d1 <- par[5]

          h.n[1] = x[1]
          for (i in 2:n) {

           h.n[i] = (a0 + a1 *
                 (abs(x[i-1]-h.n[i-1])-g1* 
                  (x[i-1]-h.n[i-1]))^d1 +
                  b1 * (h.n[i-1]^d1))^(1/d1)
            }
           -sum(dpois(x, h.n, log=TRUE))
            }

         #as my true value are a0 = 2.3; a1 
         #= 0.05; g1 = 0.68; b1 = 0.09; d1 
         #= 2.0 
         #I put the parscale to become 
         #c(1,0.01,0.1,0.01,1)
       ps <- c(1.0, 1e-02, 1e-01, 1e-02,1.0)

         #optimization to check whether 
         #estimate return near to the true 
         #value
         optim(par=c(0.1,0.01,0.1,0.01,0.1), 
          ll, method = "L-BFGS-B",
          lower=c(1e-6,-10,-10,-10, 1e- 6),
          control= list(maxit=1000,
          parscale=ps,trace=1)) 

然后我将得到以下结果:

> iter   10 value 3172.782149 

> iter   20 value 3172.371186 

> iter   30 value 3171.952137 

> iter   40 value 3171.525942 

> iter   50 value 3171.174571 

> iter   60 value 3171.095186 

> Error in optim(par = c(0.1, 0.01, 0.1, 0.01, 
> 0.1), ll, method = "L-BFGS-B",  :    L-BFGS-B 
> needs finite values of 'fn'

所以我尝试更改下限,并且它返回

> > optim(par=c(0.1,0.01,0.1,0.01,0.1), ll, method = "L-BFGS-B",lower=c(1e-6,1e-6,-10,1e-6,1e-6),control=list(maxit=1000,parscale=ps,trace=1))
> 
> iter   10 value 3172.782149 
> 
> iter   20 value 3172.371186 
> 
> iter   30 value 3171.952137   
>
> iter   40 value 3171.525942   
>
> iter   50 value 3171.174571 
> 
> iter   60 value 3171.095186 
> 
> iter   70 value 3171.076036 
> 
> iter   80 value 3171.044809 
> 
> iter   90 value 3171.014010 
> 
> iter  100 value 3170.991805 
> 
> iter  110 value 3170.971857 
> 
> iter  120 value 3170.954827 
> 
> iter  130 value 3170.941397 
> 
> iter  140 value 3170.925935 
> 
> iter  150 value 3170.915694  
> 
> iter  160 value 3170.904309 
> 
> iter  170 value 3170.894642

> iter  180 value 3170.887122  

> iter  190 value 3170.880802 
> 
> iter  200 value 3170.874319 
> 
> iter  210 value 3170.870006 
> 
> iter  220 value 3170.866008 
> 
> iter  230 value 3170.865497 
> 
> final  value 3170.865422  converged  
>
> $`par` [1] 3.242429e+05
> 2.691999e-04 3.896417e-01 6.174022e-04 2.626361e+01
> 
> $value [1] 3170.865
> 
> $counts function gradient 
>      291      291 
> 
> $convergence [1] 0
> 
> $message [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"

当然,估计的参数与真实值相差很远。

我该怎么做才能使估算值接近真实值?

1 个答案:

答案 0 :(得分:1)

当MLE远离真实值时,有几种可能的解释:

  1. 您没有足够的数据来获取准确的估计。尝试使用更大的样本量,看看结果是否更接近。

  2. 您对可能性进行了错误编码。这很难诊断。基本上,您只想阅读一下并检查您的编码。

    • 我对您的模型不熟悉,但是在您的情况下似乎很可能:在您的模拟中,h[1]始终为6,而x[1]是具有均值的随机值;您有可能假设h[1]等于x[1]。这不太可能是真的。
  3. 您的可能性没有唯一的最大值,因为参数无法识别。

可能还有其他人。