if (FALSE) {
library(rstan)
scode <- "
data {
int N;
}
parameters {
array[N] real y1;
array[N] real y2;
}
model {
y1 ~ normal(0, 1);
y2 ~ double_exponential(0, 2);
}
"
seed <- 123 # or any other integer
foo_data <- list(N = 2)
foo <- stan(model_code = scode, data = foo_data, chains = 1, iter = 1)
f1 <- stan(fit = foo, data = foo_data, chains = 1, seed = seed, chain_id = 1)
f2 <- stan(fit = foo, data = foo_data, chains = 2, seed = seed, chain_id = 2:3)
f12 <- sflist2stanfit(list(f1, f2))
## parallel stan call for unix-like OS
library(parallel)
if (.Platform$OS.type == "unix") {
sflist1 <-
mclapply(1:4, mc.cores = 2,
function(i) stan(fit = foo, data = foo_data, seed = seed,
chains = 1, chain_id = i, refresh = -1))
f3 <- sflist2stanfit(sflist1)
}
if (.Platform$OS.type == "windows") { # also works on non-Windows
CL <- makeCluster(2)
clusterExport(cl = CL, c("foo_data", "foo", "seed"))
sflist1 <- parLapply(CL, 1:4, fun = function(cid) {
require(rstan)
stan(fit = foo, data = foo_data, chains = 1,
iter = 2000, seed = seed, chain_id = cid)
})
fit <- sflist2stanfit(sflist1)
print(fit)
stopCluster(CL)
} # end example for Windows
}
Run the code above in your browser using DataLab