R optim()L-BFGS-B需要'fn'的有限值-Weibull [英] R optim() L-BFGS-B needs finite values of 'fn' - Weibull

查看:1211
本文介绍了R optim()L-BFGS-B需要'fn'的有限值-Weibull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用optim()函数估计三个参数a,b0和b1.但是我总是得到错误: optim中的错误(par = c(1,1,1),fn = logweibull,方法="L-BFGS-B",: L-BFGS-B需要有限的'fn'

I try to estimate the three parameters a, b0 and b1 with the optim() function. But I always get the error: Error in optim(par = c(1, 1, 1), fn = logweibull, method = "L-BFGS-B", : L-BFGS-B needs finite values of 'fn'

t<-c(6,6,6,6,7,9,10,10,11,13,16,17,19,20,22,23,25,32,32,34,35,1,1,2,2,3,4,4,5,5,8,8,8,8,11,11,12,12,15,17,22,23)
d<-c(0,1,1,1,1,0,0,1,0,1,1,0,0,0,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
X<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

logweibull <- function (a,b0,b1) {a <- v[1];b0 <- v[2]; b1 <- v[3];
sum (d*log(t^a*exp(b0+X*b1)-t^a*exp(b0+X*b1))) + sum (d + log((a*t^(a-1))/t^a)) }

v<-c(1,1,1)

optim( par=c(1,1,1) ,fn = logweibull, method = "L-BFGS-B",lower = c(0.1, 0.1,0.1), upper = c(100, 100,100),control = list(fnscale = -1) )

你能帮我吗?你知道我做错了吗?

Can you help me? Do you know what I did wrong?

推荐答案

您也可以考虑

(1)将其他数据变量与您要估算的参数一起传递给目标函数.

(1) passing the additional data variables to the objective function along with the parameters you want to estimate.

(2)传递梯度函数(添加了梯度函数)

(2) passing the gradient function (added the gradient function)

(3)可以进一步简化原始目标函数(如下所示)

(3) the original objective function can be further simplified (as below)

logweibull <- function (v,t,d,X) {
  a <- v[1] 
  b0 <- v[2] 
  b1 <- v[3] 
  sum(d*(1+a*log(t)+b0+X*b1) - t^a*exp(b0+X*b1) + log(a/t)) # simplified function
}

grad.logweibull <- function (v,t,d,X) {
  a <- v[1] 
  b0 <- v[2] 
  b1 <- v[3] 
  c(sum(d*log(t) - t^a*log(t)*exp(b0+X*b1) + 1/a), 
    sum(d-t^a*exp(b0+X*b1)),
    sum(d*X - t^a*X*exp(b0+X*b1)))
}

optim(par=c(1,1,1), fn = logweibull, gr = grad.logweibull,
      method = "L-BFGS-B",
      lower = c(0.1, 0.1,0.1), 
      upper = c(100, 100,100),
      control = list(fnscale = -1), 
      t=t, d=d, X=X)

有输出

$par
[1] 0.2604334 0.1000000 0.1000000

$value
[1] -191.5938

$counts
function gradient 
      10       10 

$convergence
[1] 0

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

此外,下面是有和没有梯度函数(具有有限差分)的收敛性之间的比较.使用显式梯度函数,需要9次迭代才能收敛到解,而没有梯度函数(有有限差分),则需要126次迭代才能收敛.

Also, below is a comparison between the convergence of with and without gradient function (with finite difference). With an explicit gradient function it takes 9 iterations to converge to the solution, whereas without it (with finite difference), it takes 126 iterations to converge.

这篇关于R optim()L-BFGS-B需要'fn'的有限值-Weibull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆