我正在尝试为函数优化3个参数。当我将不同的初始猜测传递给优化时,我会得到不同的优化值,即使它返回收敛0(意味着为真)!我的成本函数是确定的。更新:下面提供的样本成本函数 - 它与if / else语句不连续
ol_cost_func <- function(coeffs, fitting_data){
fitted <- coeffs[1] + fitting_data$x1 * coeffs[2] + fitting_data$x2 * coeffs[3]
value <- ifelse(fitted > fitting_data$y, fitting_data$z - fitted, 0)
return(mean(value))
}
optim(c(-0.01, -1, -.6), ol_cost_func, fitting_data = data, control=list(fnscale=-1))
$par
[1] -0.02915648 -1.00824819 -0.55884414
$value
[1] 5.047735
$counts
function gradient
340 NA
$convergence
[1] 0
$message
NULL
optim(c(-0.01, -1, -.4), ol_cost_func, fitting_data = data, control=list(fnscale=-1))
$par
[1] -0.07138303 -1.03042150 -0.30845815
$value
[1] 6.654118
$counts
function gradient
273 NA
$convergence
[1] 0
$message
NULL