0

I'm using MSGARCH (R's packages) on windows 10

I should fitting any markov switching model many times (12.500 with a while and for loop) with this code

X = CreateSpec(variance.spec = list(model = c()), 
               distribution.spec = list(distribution = c()))

Y = FitML(data, spec = X)

How to parallelize the last function (FitML)?? I'd like to run many FitML() function for various X values

paoletinho
  • 11
  • 3
  • Welcome to SO! Please include a [minimal example](https://stackoverflow.com/help/mcve). See also https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for specific hints concerning R. – Ralf Stubner May 08 '18 at 13:10
  • When you ask how to parallelize FitML function, do you mean running a single instance of this function in parallel (which is not possible) or running many FitML() function for various X values? – Katia May 08 '18 at 13:19
  • sorry for the mistake, thanks Katia for the support. I'd like to run many FitML() function for various X values. – paoletinho May 08 '18 at 13:44

1 Answers1

0

Assuming you already have a list of X:s, let us call it Xs, then you can call FitML() on each of the elements as:

Ys <- lapply(Xs, FUN = FitML)

The above applies the function to the elements sequentially. To do the same in parallel, you can use the future.apply package part of the future ecosystem (I'm the author). The following parallelizes on your local machine and works on all operating systems:

library(future.apply)
plan(multiprocess)

Ys <- future_lapply(Xs, FUN = FitML)

If there is a random-number-generation (RNG) component to FitML(), then you need to use:

Ys <- future_lapply(Xs, FUN = FitML, future.seed = TRUE)

to make sure you use proper random numbers.

If you don't specify plan(), or specify plan(sequentially), it will run sequentially.

HenrikB
  • 6,132
  • 31
  • 34