I am running a simulation study with rstan. It ran really well until recently my rstan get crashed on my PC even with the same simulation condition, which ran just fine one week or two ago. The error message goes like:
Error in unserialize(socklist[[n]]) : error reading from connection
Error in serialize(data, node$con, xdr = FALSE) :
error writing to connection
I read a lot on the forum about similar issues, but all the solutions that may work for others does not work for me at all. Now I cannot run any condition on my work PC, which drives me crazy.
Are you calling stan or sampling from inside a loop or wrapper function or something. If so, you need to be very careful to not reload the shared object multiple times. That usually entails compiling the Stan program once on the outside and then calling sampling on it repeatedly.
Yes, I did place the Stan program inside the loop function, which is placed inside another function. This is because I want to update the data for the Stan program for each replication. There is no shared objects for each replication, or more accurately, the shared objects are hard coded and placed outside of the loop.
I just wonder if it is a Stan compiling issue or what.
I have conditions of 5 items, 10 items, and 20 items, calibrated with multilevel two-parameter logistic model and one-parameter logistic model, respectively. For both models, the 5- and 10- item conditions compiled just fine, while for the 20-item condition, it crashes 99% percent of the time with about 1% of time it does not crash.
That will cause errors (not crashes) 99% of the time. It is not necessary to put the Stan program inside a function inside a loop in order to update the data for each replication. You can compile the model once on the outside with something like
library(rstan)
sm <- stan_model("program.stan")
and then do your loop calling sampling(sm, data = ...).
Cool. Let me try. Thank you so much, Dr Goodrich. But it does crash my Stan program because it stops rstan from running with the message I had in the initial post. It does not have problems with less number of items though.
I also found that my Stan program is read in from external file, only the model fitting code is in the loop function as shown below:
fit1 <- stan(
file = paste0(path,model), # Stan program
data = data, # named list of data
seed=7030507896,
chains = 4, # number of Markov chains
warmup = 1000, # number of warmup iterations per chain
iter = 2000, # total number of iterations per chain
#thin = 10,
cores = 4, # number of cores (using 2 just for the vignette)
refresh = 1, # show progress every 'refresh' iterations
control = list(max_treedepth = 10,adapt_delta=0.85)
)
model is read in externally, only data is changing for each loop.
Actually, while I had this line enabled, I was having this error at the very beginning . After disabling this line, I’m having this same exact error right after the warmup ends. This is so weird. I tried this using the 8schools example and everything was fine there.