I am trying to read the output file using rstan::read_stan_csv, but I get an error.
rr = cmdstan_model("src/rethinking.stan", cpp_options = list(stan_threads = TRUE))
dat = list(
mx = sm$smx,
time = sm$time,
period = sm$period,
time_period = sm$time_period,
county = sm$county,
N = nrow(sm)
)
fit_rr = rr$sample(data = dat,
chains = 1,
iter_warmup = 1000,
iter_sampling = 1000,
threads_per_chain = 10
)
stanfit = rstan::read_stan_csv(fit_rr$output_files())
Error in numeric(iter.count) : vector size cannot be NA
In addition: Warning messages:
1: In read_csv_header(csvfiles[i]) :
NAs introduced by coercion to integer range
2: In read_csv_header(csvfiles[i]) :
NAs introduced by coercion to integer range
I’m not sure what the problem is unfortunately. Are you able to share the csv file (you should be able to upload/attach files to a post here)?
Also, is there something in particular you need the stanfit object for that you can’t do with the fit from cmdstanr? (This should still work, I’m just curious about the use case)
I can’t speak for the original poster, but I do fairly extensive pre-processing of data in R and post-processing of model output and data for a specific model. I was using rstan until cmdstan_2.23.0 was released, which produced compelling speed gains using reduce_sum for threading. It was just more convenient for me to use rstan::read_stan_csv() to get stanfit objects to pass to my post-processing code than to rewrite that portion of the code.
I may change my mind about rewriting code if I decide to abandon rstan, but I’m not yet prepared to do that.
I’ve encountered no errors using rstan::read_stan_csv() on cmdstan output so I can’t help with the OP’s problem. Sorry.
Hi @sdaza, thanks for sharing the code and data. However, when I try running your code I get
Chain 1 Unrecoverable error evaluating the log probability at the initial value.
Chain 1 Exception: array[uni, ...] index: accessing element out of range. index 1 out of range; container is empty and cannot be indexed (in '/var/folders/s0/zfzm55px2nd2v__zlw5xfj2h0000gn/T/RtmpZ3UhK6/model-1674b7789d2d4.stan', line 39, column 8 to column 165)
Thanks, now it runs for me and I can reproduce the error from read_stan_csv(). It turns out this is due to a really silly bug in one of the internal functions called by read_stan_csv(). Can you try renaming your stan program to “test.stan” instead of “rethinking.stan” and then recompile and refit? I think that should fix the problem.
The bug is that there’s a poorly constructed regular expression (written many many years ago) that looks for “thin” in the csv file header (in order to detect if the chains are being thinned) but it picks up the “thin” in “rethinking”:
To be fair to whoever wrote the regular expression originally, I don’t think they would have had reason to imagine this problem. At that time the lines of the csv header that caused the problem didn’t exist (they were added later).