我是R的新手,我的优化问题.
d <- c(0, 9.017, -9.017, 0, 9.017, 0, -8.579, -7.849, 0, 0, -7.849,
-9.017, 0, -7.849, -7.849, 0, 0, 0, 8.579, 1.168, 8.579, 8.579,
-7.849, 0.729, 8.579, 9.017, 0, -0.438)
x <- c(0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0,
1, 1, 0, 1, 1, 1, 0, 1)
log.like<-function (sigma){
theta = pnorm(d,mean=0,sd=sigma)
logl = sum(log((theta ^ x) * ((1-theta)^(1-x))))
-logl
}
optim(0,fn=log.like,method="L-BFGS-B",lower=0,upper=1)
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:
Error in optim(0, fn = log.like, method = "L-BFGS-B", lower = 0, upper = 1) :
L-BFGS-B needs finite values of 'fn'
Run Code Online (Sandbox Code Playgroud)
> log.like(0)
[1] Inf
Run Code Online (Sandbox Code Playgroud)
您使用0作为起始值,但为您的函数提供无限值.这就是函数抱怨的原因.选择一个实际适当的起始值,它应该工作正常.我不明白你为什么要为参数设置上限1.您可能也希望增加它.