In a larger optimization task, i'm stuck at a point where I need to input a list into optim
. However, when within the list there is a vector, I get an error
(list) object cannot be coerced to type 'double'
Below there's an reproducible example and the error source is in the butter
line. Removing it the code works, but what I really want to is a list like init_param
fun = function(param_list){
x2 = param_list[["x2"]]
bread = param_list[["bread"]]
x3 = param_list[["x3"]]
butter = param_list[["butter"]]
sum(
(bread - 3)^2, (x2-4)^2, (x3-125)^2, (butter[1])^2, (butter[2])^2
)
}
init_param = list(x3 = -1, bread = 50, x2 = 120, butter = c(1,2))
optim(par = init_param, fn = fun)