Hi,
I would like to add some simple features to some future release of Rstan (there are workarounds of course).
1. I have been struggling with neg_binomial_2_log_rng for a long time. While the chains converge during warmup the error is thrown up because the parameter for the underlying poisson_rng is too large.
Ben suggested the following excellent workaround:
if (is_inf(theta))
gamma_temp <- nu;
else
gamma_temp <- gamma_rng(theta, theta / nu);
if (gamma_temp < poisson_max)
neg_bin_rng <- poisson_rng(gamma_temp);
else
neg_bin_rng <- normal_rng(gamma_temp, sqrt(gamma_temp));
Another alternative is to allow generated quantities block to be executed only during sampling.
2. It is nice to see the progress of parallel chains not only in Internet Explorer but also as text file. Below is the workaround I came with by using previous postings. The output of all chains is redirected to one outfile.
par.stan <- function (file, code, data, warmup, n.draws, chains, pars,
init, outfile)
{
# compile C++ code
sm <- stan (file = file, model_code = code, data = data, chains = 1,
iter = 0)
CL <- makeCluster (chains, outfile = outfile)
clusterExport (cl = CL, c ("data", "pars", "warmup", "n.draws"))
sflist <- parLapply (CL, 1 : chains, fun = function (cid) {
require (rstan)
stan (fit = sm, data = data, chains = 1, warmup = warmup,
iter = n.draws + warmup, pars = pars, init = init,
chain_id = cid)
})
stopCluster (CL)
if (chains == 1) {
return (sflist [[1]])
} else {
return (sflist2stanfit (sflist))
}
}
stanmod <- function (chain, file, sm, data, warmup, n.draws, pars, init) {
if (length (init) == 1) {
if (init == "random") {
init <- "random"
} else {
init <- list (init [[chain]])
}
} else {
init <- list (init [[chain]])
}
sampling (sm, data = data, pars = pars, chains = 1,
iter = warmup + n.draws, warmup = warmup,
init = init,
# diagnostic_file = diagnostic_file,
# sample_file = sample_file,
verbose = TRUE, refresh = max ((warmup + n.draws) / 10, 1),
chain_id = chain)
}
Thanks,
Linas