The R function "optim" fails when a function requires additional
arguments, if the option "hessian=T" is specified. I am using R Version
1.0.0 on Windows 98.
Here is an example of the Rosenbrock Banana function from the optim help
example, with the function and gradient modified to take an additonal
argument. Note that the call to optim works fine unless a hessian is
requested.
## Rosenbrock Banana function, modified to take an additional
argument
fra <- function(x,a) {
x1 <- x[1]
x2 <- x[2]
a * (x2 - x1 * x1)^2 + (1 - x1)^2
}
grra <- function(x,a) {
x1 <- x[1]
x2 <- x[2]
c(-4*a * x1 * (x2 - x1 * x1) - 2 * (1 - x1), 2*a * (x2 - x1 *
x1))
}
a <- 100
xx <- c(-2.1,1)
optim(c(-1.2,1), fra, grra, method = "BFGS",a=100) # this
works
optim(c(-1.2,1), fra, grra, method = "BFGS",hessian=T,a=100) # this
doesn't
Dirk Moore
Dept. of Statistics
Temple University
dirk@sbm.temple.edu
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
optim: problem with additional arguments (PR#493)
2 messages · dirk@surfer.sbm.temple.edu, Peter Dalgaard
dirk@surfer.sbm.temple.edu writes:
The R function "optim" fails when a function requires additional arguments, if the option "hessian=T" is specified. I am using R Version 1.0.0 on Windows 98.
....
optim(c(-1.2,1), fra, grra, method = "BFGS",a=100) # this works optim(c(-1.2,1), fra, grra, method = "BFGS",hessian=T,a=100) # this doesn't
Yes. However, you can easily fix it for yourself. In optim(), the line
hess <- .Internal(optimhess(res$par, fn, gr, con))
needs to be modified along the lines of
res <- .Internal(optim(par, function(par) fn(par, ...),
function(par) gr(par, ...),
method, con, lower, upper))
(although that might get you in trouble if gr is NULL, so a permanent
fix would be slightly different.)
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._