rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores()) 
pooledDso <- stan_model(file = "Pooled_Model_for_Loss_Reserving.stan",
                        model_name = "GrowthCurve")
fit <- sampling(pooledDso, iter=5500, warmup=500,
                thin=2, cores=2, chains=4, seed=1037025002,
                data = list(N = nrow(dat),
                            cum=dat$cum,
                            dev=dat$dev,
                            new_N=nrow(newdat),
                            new_dev=newdat$dev))
print(fit, pars=c("ult", "omega", "theta", "sigma"),
      probs=c(0.5, 0.75, 0.95))
# Inference for Stan model: GrowthCurve.
# 4 chains, each with iter=5500; warmup=500; thin=2; 
# post-warmup draws per chain=2500, total post-warmup draws=10000.
# 
#          mean se_mean     sd     50%     75%     95% n_eff Rhat
# ult   4756.83    3.65 244.35 4732.59 4900.01 5206.47  4481    1
# omega    1.35    0.00   0.07    1.35    1.40    1.47  5201    1
# theta   41.71    0.05   3.56   41.30   43.78   48.13  4208    1
# sigma    6.81    0.01   0.70    6.75    7.25    8.03  6251    1
# 
# Samples were drawn using NUTS(diag_e) at Tue Nov  3 06:41:45 2015.
# For each parameter, n_eff is a crude measure of effective sample size,
# and Rhat is the potential scale reduction factor on split chains (at 
# convergence, Rhat=1).
rstan::traceplot(fit, pars=c("ult", "omega", "theta", "sigma"))
stan_dens(fit, pars=c("ult", "omega", "theta", "sigma"), 
          fill="skyblue")