Error in readRDS(file.rds) : error reading from connection Calls: stan_model -> is -> readRDS Execution halted

When I fit a bunch of simulated data using exactly the same r code and stan model, I sometimes get the error

Error in readRDS(file.rds) : error reading from connection
Calls: stan_model -> is -> readRDS
Execution halted

Out of 100 simulations, 11 got this error.

I get this even if I read-in the data prior of loading rstan.

E.g.,

A <- readRDS(“A.RDS”) #correlation matrix
stan_d <- list(A = A, …)

library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
m_mod <- stan_model(“model.stan”)
m_fit <- sampling(object = m_mod, data = stan_d, chains = 1, iter = 2000, control = list(adapt_delta = 0.99,max_treedepth=15), pars = c(…), init_r = .1)

I understand that my question is not optimal as I’m not posting a reproducible example.

1 Like

A.RDS is just a plain R matrix and you are getting an error reading it? If so, your filesystem may be going bad.

Yes, A is of R matrix class. What usually works is to copy the r code from a simulation run that started properly (even if the r code is identical). This is on a linux (UGE) cluster.

OK. It is less likely that your harddisk is going bad and more likely that your cluster has some sort of network file system. And maybe there was some noise during the sync or the timestamps are messed up or something leading to the readRDS error. But if it is also happening with plain R matrices that have been serialized to the disk, I am not sure the same error for serialized Stan models is indicative of a problem with RStan.

Have you resolved this issue yet? I got the exact same issue when I was running my code on the cluster. I don’t think it’s caused by the readRDS function. When I ran the readRDS function separately, it worked fine. But whenever I called the stan_model function on the cluster, the same error occurred.

Not really. For those simulations I got this error for I just took the .qsub file (in my case) from some other simulation that worked and tweaked that to contain the relevant information, and weirdly it worked. As the information in the .qsub files were identical, it may be something with the cores/nodes being assigned to the job.

Not sure if the issue has been resolved, but I ran into something very similar lately. I’m also using a cluster and received very similar (if not the same) error messages. When debugging, I figured that the error (in my case) had nothing to do with reading the data file, because when I tried importing my data as data.R file (that is without any .rds commands in my script), I was still getting that error, which supports @bgoodri response. When I run a model (e.g. model.stan), it automatically creates a copy of the model syntax and saves it as model.rds in my home repository. In my case, the solution was to delete that model.rds file, but I’m not sure of the underlying details of it.

I have had this error as well running stan on a cluster. My solution was just to set rstan_options(auto_write = FALSE)