On Mon, Jun 3, 2013 at 11:37 AM, Simon Zehnder <[email protected]>
... [Some not minimal, self contained, reproducible code]...
> Data simulation and thecreation of startpar works fine, but the parameters
> in res$par are always the start parameters. If I run the same commands
> directly on the shell I get in res$par the optimized parameters - only
> inside the foreach loop optim seems not to work. What could that be?
>
> Don't know, but but this makes me doubt it has anything to do with optim
being inside foreach:
fr <- function(x) {
x1 <- x[1] ; x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
grr <- function(x) {
x1 <- x[1] ; x2 <- x[2]
c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1) , 200 * (x2 - x1 * x1))
}
library("doMC")
registerDoMC(2)
RNGkind("L'Ecuyer")
set.seed(54321)
foreach(i = 1:2) %do% {
ret <- foreach(j = 1:2) %do%{
strtpar <- c(-2,2)+rnorm(2)
optim(strtpar, fr, grr, method =
"L-BFGS-B",control=list(trace=TRUE))$par
}
ret
}
Also, wouldn't you want to register 4 cores by default if nesting 2 loops
of 2 ? (to comment on the wisdom of doing so in terms of overhead is beyond
my expertise)
HTH
> Best
>
> Simon
>
> ______________________________________________
> [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.
>
[[alternative HTML version deleted]]
______________________________________________
[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.