kingaa / pomp

R package for statistical inference using partially observed Markov processes
https://kingaa.github.io/pomp
GNU General Public License v3.0
110 stars 26 forks source link

Error I'm getting for pfilter function #151

Closed kingaa closed 3 years ago

kingaa commented 3 years ago

Originally posted by MiguelFudolig June 24, 2021

I am trying to use POMP to perform iterated filtering to get an MLE for the parameters of a modified SIR model that I have developed. I am currently following the short course on iterated filtering (http://kingaa.github.io/short-course/mif/mif.html) and I have been getting the following error:

> (fixed_params <- with(flu2008,c(N=2500, rho=0.9, rhonew=0.9,p=0.5,gamma=1,gammanew=1)))
       N      rho   rhonew        p    gamma gammanew 
  2500.0      0.9      0.9      0.5      1.0      1.0 
> library(foreach)
> library(doParallel)
> registerDoParallel()
> stew(file="pf1.rda",{
+   t_pf <- system.time(
+     pf <- foreach(i=1:10,.packages="pomp",
+                   .options.multicore=list(set.seed=TRUE),
+                   .export=c("sir","fixed_params")
+     ) %dopar% {
+       pfilter(sir,params=c(Beta=5,Betanew=2,fixed_params),Np=5000)
+     }
+   )
+   n_pf <- getDoParWorkers()
+ },seed=625904618,kind="L'Ecuyer")
Error in { : 
  task 1 failed - "'pfilter' is undefined for 'data' of class 'function'."
Timing stopped at: 0 0 0.69
Timing stopped at: 0.09 0 0.78

I was successful in using the pfilter function outside of the parallel computation loop, but I keep getting the error " task 1 failed - "'pfilter' is undefined for 'data' of class 'function'." I also got the same error for the mif2 function. Is there something I am missing? Thank you very much!

kingaa commented 3 years ago

@MiguelFudolig: I've moved this to Issues, where it seems to belong better.

It seems that the parallel workers are not seeing the object you defined as sir, but instead are finding a function of the same name, perhaps the one defined in the pomp package itself. The easiest fix would be to give the object you create some other name. Alternatively, you could create it in the parallel block.

I am curious: do you get the same error if you execute the code outside the stew call? E.g.,

library(foreach)
library(doParallel)
registerDoParallel()

pf <- foreach(i=1:10,.packages="pomp",
  .export=c("sir","fixed_params")
) %dopar% {
  pfilter(sir,params=c(Beta=5,Betanew=2,fixed_params),Np=5000)
}
MiguelFudolig commented 3 years ago

Replacing the pomp object name worked perfectly. The pfilter function now works. Thank you very much!