Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
michaldanaj opened this issue Nov 5, 2024 · 5 comments
Closed

Comments

@michaldanaj
Copy link

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
Copy link
Member

jgabry commented Nov 8, 2024

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
Copy link
Member

jgabry commented Nov 8, 2024

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
Copy link

@michaldanaj I wrote a more thorough guide to avoiding this issue here: #1133 (comment)

@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
Copy link
Author

Thank you, @jgabry and @katrinabrock.

General solution from here worked for me.

@jgabry
Copy link
Member

jgabry commented Nov 11, 2024

Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants