我应该让以下函数从起始值0开始运行(以测试运行),但是在我应用optim()的情况下,出现错误消息“无法将类型'closure'强制转换为类型'double'的向量”“ < / p>
这是我的代码:
log.likelihood(params, x, y)
logistic.fit(xtrain, ytrain)
logistic.predict(xtest, fit)
log.likelihood = function(params, x, y) {
x = cbind(rep(1, nrow(x)), x)
Bx.sum = params %*% t(x)
t1 = sum((1-y)*Bx.sum) # 1st term
t2 = sum(log(1+sapply(-Bx.sum, exp, simplify=T)))
likelihood = -(t1+t2)
return(-likelihood)
}
optim(params = 0, x = xtrain, y = ytrain, log.likelihood, data, method='BFGS')