If you don't want the information as character, why are you printing it
rather than storing it in a matrix?
Why not something along the lines of this...
fr <- function(x) { ## Rosenbrock Banana function
on.exit(aMatrix <<- rbind(aMatrix,(cbind(x1, x2, f))))
x1 <- x[1]
x2 <- x[2]
f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
f
}
aMatrix <- NULL
ans <- optim(c(-1.2,1), fr)
aMatrix
HTH
Keith J
-----Original Message-----
"Ravi Varadhan" <[email protected]> wrote in message
news:2f9ea67ef9ae1c48a147cb41be2e15c303e...@dom-eb-mail1.win.ad.jhu.edu...
Thank you very much, Jim. That works!
I did know that I could process the character strings using regex, but was
also wondering if there was a direct way to get this.
Suppose, in the current example I would like to obtain a 3-column matrix
that contains the parameters and the function value:
fr <- function(x) { ## Rosenbrock Banana function
on.exit(print(cbind(x1, x2, f)))
x1 <- x[1]
x2 <- x[2]
f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
f
}
fvals <- capture.output(ans <- optim(c(-1.2,1), fr))
Now, I need to tweak your solution to get the 3-column matrix. It would be
nice, if there was a more direct way to get the numerical output, perhaps a
numeric option in capture.output().
Best,
Ravi.
-------------------------------------------------------
Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology School of Medicine Johns
Hopkins University
Ph. (410) 502-2619
email: [email protected]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.