it’s a simple issue about optim() function on R. I am trying to find the mimimum of a function depending on a loop’s variable.
f<-function(x,i){
i*x
}
M<-matrix(0,10,1)
for (i in 1:5) {
M[i,1]<-optim(c(1),f,lower=-2,upper=2,method="L-BFGS-B")
}
M
R answers me :
Erreur dans i * x : 'i' est manquant
I really don’t understand how to deal with this one….
You have to pass
ito theoptimcall:…and
optimreturns a list, so you need to pick which element in the list you want to store inM. I assume you wantpar:UPDATE:
As Justin pointed out in the comments, the more R-like way to do this would be: