[R] 'optim' and extra argument to the objective function

Laurent Gautier laurent at cbs.dtu.dk
Mon Oct 20 20:10:23 CEST 2003


Hello,


I'd like to use optim, and give extra arguments to the objective
function. The man page says that the '...' should let one do it,
but I have a hard time to understand how.

Example:

x <- 1:10
y <- rnorm(10)
cost.f <- function(par, x, y) {
 A <- par[1]
 cost <- sum( (log(A*x) - log(y))^2)
 return(cost)
}

optim(3, cost.f, x, y)
## returns:
Error in pmatch(x, table, duplicates.ok) : 
	argument is not of mode character
## uh, uh... may the problem is with the argument matching ?
optim(3, cost.f, method="BFGS", x, y)
## returns:
Error in log(A * x) : Argument "x" is missing, with no default
In addition: Warning message: 
bounds can only be used with method L-BFGS-B in: optim(3, cost.f, method = "BFGS", x, y) 

Any suggestion ?


L.


PS: this was done with both R-1.7.1-patched and R-1.8.0-patched for Linux.




More information about the R-help mailing list