【问题标题】:Optimization of optim() in R ( L-BFGS-B needs finite values of 'fn')R 中 optim() 的优化(L-BFGS-B 需要有限的 'fn' 值)
【发布时间】:2017-05-23 07:56:41
【问题描述】:

我在使用 R 中的 optim() 来解决涉及积分的可能性时遇到了一些麻烦。我收到一条错误消息,提示“优化错误(par = c(0.1, 0.1), LLL, method = "L-BFGS-B", lower = c(0, : L-BFGS-B 需要有限的 'fn 值) '"。下面是我的代码:

s1=c(1384,1,1219,1597,2106,145,87,1535,290,1752,265,588,1188,160,745,237,479,39,99,56,1503,158,916,651,1064,166,635,19,553,51,79,155,85,1196,142,108,325  
     ,135,28,422,1032,1018,128,787,1704,307,854,6,896,902)


LLL=function (par) {

  integrand1 <- function(x){ (x-s1[i]+1)*dgamma(x, shape=par[1], rate=par[2]) }
  integrand2 <- function(x){ (-x+s1[i]+1)*dgamma(x, shape=par[1],rate=par[2]) }



  likelihood = vector() 

  for(i in 1:length(s1)) {likelihood[i] = 
    log( integrate(integrand1,lower=s1[i]-1,upper=s1[i])$value+ integrate(integrand2,lower=s1[i],upper=s1[i]+1)$value )  
  }

  like= -sum(likelihood)
  return(like)

}




optim(par=c(0.1,0.1),LLL,method="L-BFGS-B", lower=c(0,0))

感谢您的帮助。

最好的,

YM

【问题讨论】:

  • 您的问题无法阅读。请使用正确的代码显示方式。

标签: r optimization nonlinear-optimization


【解决方案1】:

在您提供的参数的下限处评估的目标函数是无穷大。

LLL(c(0,0))
# [1] Inf

这就是L-BFGS-B 失败的原因。尝试不同的下限,例如c(0.001,0.001),您将得到解决方案。

optim(par=c(0.1,0.1),LLL,method="L-BFGS-B", lower=c(0.001,0.001))

$par
[1] 0.6865841 0.0010000

$value
[1] 369.5532

$counts
function gradient 
      14       14 

$convergence
[1] 0

$message
[1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"

要获得参数的 95% 置信区间,请尝试以下操作:

res <- optim(par=c(0.1,0.1),LLL,method="L-BFGS-B", lower=c(0.005,0.005), hessian=TRUE)
n <- length(s1)
res$par # solution
# [1] 1.900928 0.005000
res$par - 1.96*sqrt(diag(solve(res$hessian)))/n # lower limit for 95% confint
# [1] 1.888152372 0.004963286
res$par + 1.96*sqrt(diag(solve(res$hessian)))/n # upper limit for 95% confint
# [1] 1.913703040 0.005036714

参考这篇文章:http://www.ms.uky.edu/~mai/sta321/MLEexample.pdf

【讨论】:

  • 你打败了我的答案。我有完全相同的解决方案。
  • 它发生@Bhas :)
  • @sandipan 和 Bhas 感谢你们的回答。您还知道如何输出粗麻布矩阵,以便计算 2 个伽马参数的置信区间吗?我尝试在 optim() 中添加 "hessian=TRUE" 并得到: optim(par = c(0.1, 0.1), LLL, method = "L-BFGS-B", lower = c(0.001, : non-有限差分值 [2]
  • @Y.Ma 计算 hessian 需要计算第二个差异,使用下限计算为无穷大,我们需要为参数使用稍高的下限,更新代码。
  • 非常感谢@sandipan
猜你喜欢
  • 2017-02-06
  • 2015-07-20
  • 1970-01-01
  • 2023-04-05
  • 2021-09-29
  • 1970-01-01
  • 2021-12-18
  • 2018-11-12
  • 2017-07-14
相关资源
最近更新 更多