[R] Optim seems not to work properly in foreach

Simon Zehnder szehnder at uni-bonn.de
Mon Jun 3 19:37:59 CEST 2013


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



More information about the R-help mailing list