expression() within foreach %dopar%

35 views Asked by At

I'm trying to include an expression() in foreach() %dopar%. This expression() will be used to simulate an Ornstein-Uhlenbeck process via the Sim.DiffProc package within the foreach() %dopar% call.

However the parallel computing nodes doesn't seem to recognise the variables specified in expression() and I got the following error Error in { : task 1 failed - "object 'OUTheta' not found"

Anybody have any ideas how to solve the issue? Many thanks in advance!

My code:

library(Sim.DiffProc)
library(doSNOW)
library(foreach)


cl <- makeCluster(2)
registerDoSNOW(cl)

a = foreach(i = 1:2, .packages = c('Sim.DiffProc')) %dopar% {
  OUMu = 1
  OUTheta = 1
  OUSigma = 1
  f = expression(OUTheta * (OUMu - x))
  g = expression(OUSigma)
  sim = Sim.DiffProc::snssde1d(drift = f, diffusion = g, x0 = 0, N = 10, T = 1, method = 'euler', M = 1)
  return(sim$X)
}
0

There are 0 answers