[R] 'optim' and extra argument to the objective function
Thomas Lumley
tlumley at u.washington.edu
Mon Oct 20 20:53:13 CEST 2003
On Mon, 20 Oct 2003, 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)
You need
optim(3, cost.f, x=x, y=y)
As always, if you don't specify a tag for an argument it gets matched
positionally, in this case to the gr and method arguments of optim().
-thomas
More information about the R-help
mailing list