stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 267 forks source link

'n_kept not found' error during rstan::read_stan_csv(fit$output_files()) #1143

Closed michaldanaj closed 2 weeks ago

michaldanaj commented 3 weeks ago

Summary:

'n_kept not found' error during rstan::read_stan_csv(fit$output_files()).

Instead of fixing it, is it possible to change original code to make it work?

Description:

I try to run this code, but I get an error

Error in rstan::read_stan_csv(fit$output_files()) :
  object 'n_kept' not found

It has already been mentioned in #1133, but please help me circumvent it.

Reproducible Steps:

To reproduce, I installed fresh conda environment with cmdstan, cmdstanr and rstan installed as adviced in getting started.

My code to reproduce:

  library(cmdstanr)
  set_cmdstan_path('~/.conda/envs/stan/bin/cmdstan')
  library(rstan)

  file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
  mod <- cmdstan_model(file)

  data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))

  fit <- mod$sample(
    data = data_list,
    seed = 123,
    chains = 4,
    parallel_chains = 4,
    refresh = 500 # print update every 500 iters
  )

  stanfit <- rstan::read_stan_csv(fit$output_files())

Current Output:

Error in rstan::read_stan_csv(fit$output_files()) :
  object 'n_kept' not found

Expected Output:

No error.

RStan Version:

2.35.0.9000

R Version:

R version 4.4.1 (2024-06-14)

rstan Version:

rstan version 2.35.0.9000 (Stan version 2.35.0)

cmdstanr Version:

This is cmdstanr version 0.8.1

Operating System:

Arch Linux Linux arch 6.11.6-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 01 Nov 2024 03:30:41 +0000 x86_64 GNU/Linux

jgabry commented 2 weeks ago

Yeah unfortunately read_stan_csv isn't working with the latest cmdstan csv files. We recommend using the provided functionality with cmdstanr to read the results into R instead of using cmdstanr to fit models and rstan to read the results. Maybe the code from the Stan forum post I linked to in this comment will help. It gives you a way to make an object with similar structure to what you need I think. Sorry for the hassle.

jgabry commented 2 weeks ago

in addition to my previous comment, another option could be to run an older version of cmdstan. I think it might just be as of 2.35.0 that this is an issue, so you could try installing e.g. 2.34.0. cmdstanr::install_cmdstan() will let you specify version = "2.34.0". That might work with the code you're trying to run.

katrinabrock commented 2 weeks ago

@michaldanaj I wrote a more thorough guide to avoiding this issue here: https://github.com/stan-dev/rstan/issues/1133#issuecomment-2466698446

@jgabry I think we can close this issue since it's a dupe of #1133 so we can put any discussion about it there.

michaldanaj commented 2 weeks ago

Thank you, @jgabry and @katrinabrock.

General solution from here worked for me.

jgabry commented 2 weeks ago

Great, thank you!