No problem:
library("rstan")
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
schools <- '
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
real eta[J];
}
transformed parameters {
real theta[J];
for (j in 1:J)
theta[j] = mu + tau * eta[j];
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
'
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))
tstmod <- stan_model(model_code = schools)
fit <- sampling(tstmod, data = schools_dat, iter = 1000, chains = 4, verbose = TRUE)
> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 1).
> Gradient evaluation took 0.000116 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 1.16 seconds.
> Adjust your expectations accordingly!
> Iteration: 1 / 1000 [ 0%] (Warmup)
> Iteration: 100 / 1000 [ 10%] (Warmup)
> Iteration: 200 / 1000 [ 20%] (Warmup)
> Iteration: 300 / 1000 [ 30%] (Warmup)
> Iteration: 400 / 1000 [ 40%] (Warmup)
> Iteration: 500 / 1000 [ 50%] (Warmup)
> Iteration: 501 / 1000 [ 50%] (Sampling)
> Iteration: 600 / 1000 [ 60%] (Sampling)
> Iteration: 700 / 1000 [ 70%] (Sampling)
> Iteration: 800 / 1000 [ 80%] (Sampling)
> Iteration: 900 / 1000 [ 90%] (Sampling)
> Iteration: 1000 / 1000 [100%] (Sampling)
> Elapsed Time: 0.032092 seconds (Warm-up)
> > 0.013621 seconds (Sampling)
> > 0.045713 seconds (Total)
> Error in sendMaster(try(eval(expr, env), silent = TRUE)) :
> write error, closing pipe to the master
> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 2).
> Gradient evaluation took 0.000118 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 1.18 seconds.
> Adjust your expectations accordingly!
> Iteration: 1 / 1000 [ 0%] (Warmup)
> Iteration: 100 / 1000 [ 10%] (Warmup)
> Iteration: 200 / 1000 [ 20%] (Warmup)
> Iteration: 300 / 1000 [ 30%] (Warmup)
> Iteration: 400 / 1000 [ 40%] (Warmup)
> Iteration: 500 / 1000 [ 50%] (Warmup)
> Iteration: 501 / 1000 [ 50%] (Sampling)
> Iteration: 600 / 1000 [ 60%] (Sampling)
> Iteration: 700 / 1000 [ 70%] (Sampling)
> Iteration: 800 / 1000 [ 80%] (Sampling)
> Iteration: 900 / 1000 [ 90%] (Sampling)
> Iteration: 1000 / 1000 [100%] (Sampling)
> Elapsed Time: 0.03342 seconds (Warm-up)
> > 0.01713 seconds (Sampling)
> > 0.05055 seconds (Total)
> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 3).
> Gradient evaluation took 5.5e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.55 seconds.
> Adjust your expectations accordingly!
> Iteration: 1 / 1000 [ 0%] (Warmup)
> Iteration: 100 / 1000 [ 10%] (Warmup)
> Iteration: 200 / 1000 [ 20%] (Warmup)
> Iteration: 300 / 1000 [ 30%] (Warmup)
> Iteration: 400 / 1000 [ 40%] (Warmup)
> Iteration: 500 / 1000 [ 50%] (Warmup)
> Iteration: 501 / 1000 [ 50%] (Sampling)
> Iteration: 600 / 1000 [ 60%] (Sampling)
> Iteration: 700 / 1000 [ 70%] (Sampling)
> Iteration: 800 / 1000 [ 80%] (Sampling)
> Iteration: 900 / 1000 [ 90%] (Sampling)
> Iteration: 1000 / 1000 [100%] (Sampling)
> Elapsed Time: 0.025148 seconds (Warm-up)
> > 0.019178 seconds (Sampling)
> > 0.044326 seconds (Total)
> CHECKING DATA AND PREPROCESSING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> COMPILING MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> STARTING SAMPLER FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW.
> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 4).
> Gradient evaluation took 2.8e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.28 seconds.
> Adjust your expectations accordingly!
> Iteration: 1 / 1000 [ 0%] (Warmup)
> Iteration: 100 / 1000 [ 10%] (Warmup)
> Iteration: 200 / 1000 [ 20%] (Warmup)
> Iteration: 300 / 1000 [ 30%] (Warmup)
> Iteration: 400 / 1000 [ 40%] (Warmup)
> Iteration: 500 / 1000 [ 50%] (Warmup)
> Iteration: 501 / 1000 [ 50%] (Sampling)
> Iteration: 600 / 1000 [ 60%] (Sampling)
> Iteration: 700 / 1000 [ 70%] (Sampling)
> Iteration: 800 / 1000 [ 80%] (Sampling)
> Iteration: 900 / 1000 [ 90%] (Sampling)
> Iteration: 1000 / 1000 [100%] (Sampling)
> Elapsed Time: 0.033636 seconds (Warm-up)
> > 0.018256 seconds (Sampling)
> > 0.051892 seconds (Total)
> Error in FUN(X[[i]], ...) :
> trying to get slot "mode" from an object of a basic class ("NULL") with no slots
Adding the shinystan package ‘fixes’ my problem:
library('shinystan')
fit <- sampling(tstmod, data = schools_dat, iter = 1000, chains = 4)
> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 1).
> Gradient evaluation took 1.4e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.14 seconds.
> Adjust your expectations accordingly!
> Iteration: 1 / 1000 [ 0%] (Warmup)
> Iteration: 100 / 1000 [ 10%] (Warmup)
> Iteration: 200 / 1000 [ 20%] (Warmup)
> Iteration: 300 / 1000 [ 30%] (Warmup)
> Iteration: 400 / 1000 [ 40%] (Warmup)
> Iteration: 500 / 1000 [ 50%] (Warmup)
> Iteration: 501 / 1000 [ 50%] (Sampling)
> Iteration: 600 / 1000 [ 60%] (Sampling)
> Iteration: 700 / 1000 [ 70%] (Sampling)
> Iteration: 800 / 1000 [ 80%] (Sampling)
> Iteration: 900 / 1000 [ 90%] (Sampling)
> Iteration: 1000 / 1000 [100%] (Sampling)
> Elapsed Time: 0.01924 seconds (Warm-up)
> 0.012036 seconds (Sampling)
> 0.031276 seconds (Total)
> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 2).
> Gradient evaluation took 1.3e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.13 seconds.
> Adjust your expectations accordingly!
> Iteration: 1 / 1000 [ 0%] (Warmup)
> Iteration: 100 / 1000 [ 10%] (Warmup)
> Iteration: 200 / 1000 [ 20%] (Warmup)
> Iteration: 300 / 1000 [ 30%] (Warmup)
> Iteration: 400 / 1000 [ 40%] (Warmup)
> Iteration: 500 / 1000 [ 50%] (Warmup)
> Iteration: 501 / 1000 [ 50%] (Sampling)
> Iteration: 600 / 1000 [ 60%] (Sampling)
> Iteration: 700 / 1000 [ 70%] (Sampling)
> Iteration: 800 / 1000 [ 80%] (Sampling)
> Iteration: 900 / 1000 [ 90%] (Sampling)
> Iteration: 1000 / 1000 [100%] (Sampling)
> Elapsed Time: 0.023172 seconds (Warm-up)
> 0.01713 seconds (Sampling)
> 0.040302 seconds (Total)
> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 3).
> Gradient evaluation took 1.4e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.14 seconds.
> Adjust your expectations accordingly!
> Iteration: 1 / 1000 [ 0%] (Warmup)
> Iteration: 100 / 1000 [ 10%] (Warmup)
> Iteration: 200 / 1000 [ 20%] (Warmup)
> Iteration: 300 / 1000 [ 30%] (Warmup)
> Iteration: 400 / 1000 [ 40%] (Warmup)
> Iteration: 500 / 1000 [ 50%] (Warmup)
> Iteration: 501 / 1000 [ 50%] (Sampling)
> Iteration: 600 / 1000 [ 60%] (Sampling)
> Iteration: 700 / 1000 [ 70%] (Sampling)
> Iteration: 800 / 1000 [ 80%] (Sampling)
> Iteration: 900 / 1000 [ 90%] (Sampling)
> Iteration: 1000 / 1000 [100%] (Sampling)
> Elapsed Time: 0.023496 seconds (Warm-up)
> 0.015783 seconds (Sampling)
> 0.039279 seconds (Total)
> SAMPLING FOR MODEL '9c2009747cc2401f4a9818c1e89252e6' NOW (CHAIN 4).
> Gradient evaluation took 4.1e-05 seconds
> 1000 transitions using 10 leapfrog steps per transition would take 0.41 seconds.
> Adjust your expectations accordingly!
> Iteration: 1 / 1000 [ 0%] (Warmup)
> Iteration: 100 / 1000 [ 10%] (Warmup)
> Iteration: 200 / 1000 [ 20%] (Warmup)
> Iteration: 300 / 1000 [ 30%] (Warmup)
> Iteration: 400 / 1000 [ 40%] (Warmup)
> Iteration: 500 / 1000 [ 50%] (Warmup)
> Iteration: 501 / 1000 [ 50%] (Sampling)
> Iteration: 600 / 1000 [ 60%] (Sampling)
> Iteration: 700 / 1000 [ 70%] (Sampling)
> Iteration: 800 / 1000 [ 80%] (Sampling)
> Iteration: 900 / 1000 [ 90%] (Sampling)
> Iteration: 1000 / 1000 [100%] (Sampling)
> Elapsed Time: 0.034431 seconds (Warm-up)
> 0.014595 seconds (Sampling)
> 0.049026 seconds (Total)
I can call fit now, no problem
> print(fit)
Inference for Stan model: 9c2009747cc2401f4a9818c1e89252e6.
4 chains, each with iter=1000; warmup=500; thin=1;
post-warmup draws per chain=500, total post-warmup draws=2000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
mu 7.86 0.15 5.01 -2.03 4.62 7.69 11.02 18.42 1188 1
tau 6.63 0.17 5.11 0.31 2.73 5.59 9.29 19.63 907 1
eta[1] 0.41 0.02 0.91 -1.45 -0.20 0.46 1.03 2.11 2000 1
eta[2] 0.04 0.02 0.86 -1.59 -0.55 0.02 0.61 1.80 1885 1
eta[3] -0.20 0.02 0.91 -1.89 -0.83 -0.20 0.44 1.56 2000 1
etc....