优化函数参数丢失 [英] optim function argument missing
本文介绍了优化函数参数丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码. kum.loglik
函数返回负对数似然性,并接受两个参数a和b.我需要找到a和b,以使用优化函数最小化此函数. (n1,n2,n3是预先指定的,并传递给了优化函数.
This is my code. The kum.loglik
function returns negative loglikelihood and takes two arguments a and b. I need to find a and b that minimize this function using optim function. (n1,n2,n3 is pre-specified and passed to optim function.
kum.loglik = function(a, b, n1, n2, n3) {
loglik = n1*log(b*beta(1+2/a,b)) + n2 * log(b*beta(1+2/a,b)-2*b*beta(1+1/a,b)+1) +
n3 * log(b*beta(1+1/a,b)-b*beta(1+2/a,b))
return(-loglik)
}
optim(par=c(1,1), kum.loglik, method="L-BFGS-B",
n1=n1, n2=n2, n3=n3,
control=list(ndeps=c(5e-4,5e-4)))
此代码应该可以正常工作,但是会显示错误消息
This code should work well but it gives error message
Error in b * beta(1 + 2/a, b) : 'b' is missing
此代码有什么问题?
推荐答案
问题出在(直接来自优化帮助):
The problem is (straight from the optim help):
fn: A function to be minimized (or maximized), with first
argument the vector of parameters over which minimization is
to take place.
您的kum.loglik
函数需要采用向量v
,您可以从中提取参数,例如:
Your kum.loglik
function needs to take a vector v
which you pull the parameters out of, e.g.:
kum.loglik=function(v) { a = v[1]; b = v[2]; ...}
这篇关于优化函数参数丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文