Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

(Optim) Error: (list) object cannot be coerced to type 'double'

$
0
0

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)


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>