编译多个stan模型然后循环运行会导致Rcpp错误

时间:2019-08-06 17:55:33

标签: stan rstan

我正在运行一个仿真研究,其中我正在分析多种斯坦模型下的数据。有3种不同的stan模型。这些模型需要一些时间才能运行,因此以效率为名,我将代码设置如下:

...
      rt <- stanc("stan_mod1.stan")
      sm1 <- stan_model( stanc_ret = rt, verbose = FALSE )

      rt <- stanc("stan_mod2.stan")
      sm2 <- stan_model( stanc_ret = rt, verbose = FALSE )


for(i in 1:x){
 ...
     fit1 <- sampling(sm1, data = datenstan,
                      chains = 2, iter = 1000,
                      warmup = 500)

     fit2 <- sampling(sm2, data = datenstan,
                      chains = 2, iter = 1000,
                      warmup = 500)
...
}

这会导致以下Rcpp错误:

模块中的错误(模块,mustStart = TRUE):   无法初始化模块指针:FUN(X [[i]],...)中的错误:软件包C:/ ....中没有这样的符号_rcpp_module_boot_stan_fit4model39b451ff2aca_sem_non_spatial_final_mod。

为避免这种情况,我被迫这样做:

for(i in 1:x){
 ...
     rt <- stanc("stan_mod1.stan")
     sm1 <- stan_model( stanc_ret = rt, verbose = FALSE )

     fit1 <- sampling(sm1, data = datenstan,
                      chains = 2, iter = 1000,
                      warmup = 500)

     rt <- stanc("stan_mod2.stan")
     sm2 <- stan_model( stanc_ret = rt, verbose = FALSE )

     fit2 <- sampling(sm2, data = datenstan,
                      chains = 2, iter = 1000,
                      warmup = 500)
...
}

但是编译时间使事情变慢了。

我希望能够运行模型而不必每次都重新编译它们。这是我的系统出现错误吗(由于Rcpp警告,我想是这样)还是这是设计使然?

我正在使用Windows 10计算机进行此模拟,并且正在使用R 3.6 Rstan版本2.19.2。

0 个答案:

没有答案