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

Spencer Graves spencer.graves at pdf.com
Mon Oct 20 20:48:03 CEST 2003


Have you tried the following: 

optim(3, cost.f, x=x, y=y)

      In the form given below, R interprets you call as equivalent to 
the following: 

optim(par=3, fn=cost.f, gr=x, method=y)

      Arguments will get passed via "..." when they have names that are 
different from names in the official argument list. 

hope this helps.  spencer graves

Laurent Gautier wrote:

>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.
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>  
>




More information about the R-help mailing list