Re: [R] Optim seems not to work properly in foreach
Hi Ilai, after you sent this message I tried your code as well and it worked. As a result, I reconsidered the code written by me and of course also found the error in my function simulateEKOP. So for other people assuming errors or ill behavior in base functions: Forget it: these functions are tested this often, that the probabilitty is almost 1, the error is in your own code. Thanks for the help Simon On Jun 3, 2013, at 10:53 PM, ilai ke...@math.montana.edu wrote: On Mon, Jun 3, 2013 at 11:37 AM, Simon Zehnder szehn...@uni-bonn.de ... [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 __ R-help@r-project.org 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]] __ R-help@r-project.org 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.
[R] Optim seems not to work properly in foreach
Hi guys, I am working now for several years in R and I would say I manage things pretty easily, but with the foreach loop I have my problems. I call for a simulation a double foreach loop and this works fine. Inside the second loop (which I plan to parallelize later on) I call Rs optim-function: simulateML - function(sim.it = 250, input.path, output.path, T = 390, ncore = 2) { ## load packages ## library(mmstruct) library(doMC) library(foreach) ## read input parameters ## input - read.csv(input.path, header = FALSE) ## create container to store results ## results - data.frame(NA, nrow = NROW(input) * sim.it, ncol = 12) ## initialize the parallel backend ## registerDoMC(ncore) result.list - foreach(i = 1:2) %do% { input - as.matrix(input) input.row - input[i, ] list - foreach(i = 1:2, .combine = rbind, .packages = c(mmstruct, stats)) %do% { data - simulateEKOP(size = input.row[1], alpha = input.row[2], epsilon = input.row[1], delta = input.row[4], mu = input.row[5], T = T) data - data[,4] ## create start values ## tmp - mean(data)/T startpar - c(0, tmp * 0.75/2, tmp * 0.25/2) ## set options for optimization ## optim_fn- computeKokotLik optim_method- L-BFGS-B optim_lower - c(-1e+7, 0, 0) optim_upper - rep(1e+7, 3) optim_fnscale - -1 optim_maxit - 200 optim_ctrl - list(fnscale = optim_fnscale, maxit = optim_maxit) ## start optimization ## res - optim(par = startpar, fn = optim_fn, data = data, T = T, methodLik = approx, method = optim_method, lower = optim_lower, upper = optim_upper, control = optim_ctrl, hessian = TRUE) res$par } print(list) } } 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? Best Simon __ R-help@r-project.org 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.
Re: [R] Optim seems not to work properly in foreach
On Mon, Jun 3, 2013 at 11:37 AM, Simon Zehnder szehn...@uni-bonn.de ... [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 __ R-help@r-project.org 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]] __ R-help@r-project.org 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.