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

Error reading sampler diagnostics from a previously saved CmdStanFit object when fixed_param is TRUE #1032

Closed
wlandau opened this issue Nov 13, 2024 · 3 comments · Fixed by #1033
Labels
bug Something isn't working

Comments

@wlandau
Copy link
Collaborator

wlandau commented Nov 13, 2024

It appears that if a CmdStanFit object is created with fixed_param = TRUE and saved to disk, the retrieved object cannot load sampler diagnostics. (I'm not sure it makes sense to look at diagnostics if the MCMC did not move, but a user reported this at ropensci/stantargets#68.)

cmdstanr::cmdstan_version()
#> [1] "2.35.0"
  
packageVersion("cmdstanr")
#> [1] '0.8.1.9000'

packageDescription("cmdstanr")$GithubSHA1
#> [1] "a9e253616511327c53de9946d816177587ad181e"

callr::r(function() {
  library(cmdstanr)
  lines <- "
    data {
    int <lower = 1> n;
      vector[n] x;
      vector[n] y;
      real true_beta;
    }
    parameters {
      real beta;
    }
    model {
      y ~ normal(x * beta, 1);
      beta ~ normal(0, 1);
    }
    generated quantities {
      real beta_sim = normal_rng(0, 1);
      vector[n] y_sim;
      for (i in 1:n) {
        y_sim[i] = normal_rng(x[i] * beta, 1);
      }
    }
  "
  writeLines(lines, "x.stan")
  model <- cmdstan_model("x.stan")
  generate_data <- function(n = 10) {
    true_beta <- stats::rnorm(n = 1, mean = 0, sd = 1)
    x <- seq(from = -1, to = 1, length.out = n)
    y <- stats::rnorm(n, x * true_beta, 1)
    list(n = n, x = x, y = y, true_beta = true_beta)
  }
  fit <- model$sample(data = generate_data(), fixed_param = TRUE)
  fit$save_object("fit.rds")
  invisible()
})
#> NULL

fit <- readRDS("fit.rds")
fit$sampler_diagnostics()
#> Error in read_cmdstan_csv(files = self$output_files(include_failed = FALSE), : Assertion on 'files' failed: File does not exist: '/var/folders/4v/vh7xp8553lsbl49svl48g7p00000gp/T/RtmpM4WhNk/x-202411131414-1-6d70de.csv'.

Created on 2024-11-13 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.0 (2024-04-24)
#>  os       macOS 15.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Indiana/Indianapolis
#>  date     2024-11-13
#>  pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package        * version    date (UTC) lib source
#>  abind            1.4-8      2024-09-12 [1] CRAN (R 4.4.1)
#>  backports        1.5.0      2024-05-23 [1] CRAN (R 4.4.0)
#>  callr            3.7.6      2024-03-25 [1] CRAN (R 4.4.0)
#>  checkmate        2.3.2      2024-07-29 [1] CRAN (R 4.4.0)
#>  cli              3.6.3      2024-06-21 [1] CRAN (R 4.4.0)
#>  cmdstanr         0.8.1.9000 2024-11-13 [1] Github (stan-dev/cmdstanr@a9e2536)
#>  digest           0.6.37     2024-08-19 [1] CRAN (R 4.4.1)
#>  distributional   0.5.0      2024-09-17 [1] CRAN (R 4.4.1)
#>  evaluate         1.0.1      2024-10-10 [1] CRAN (R 4.4.1)
#>  fansi            1.0.6      2023-12-08 [1] CRAN (R 4.4.0)
#>  fastmap          1.2.0      2024-05-15 [1] CRAN (R 4.4.0)
#>  fs               1.6.4      2024-04-25 [1] CRAN (R 4.4.0)
#>  generics         0.1.3      2022-07-05 [1] CRAN (R 4.4.0)
#>  glue             1.8.0      2024-09-30 [1] CRAN (R 4.4.0)
#>  htmltools        0.5.8.1    2024-04-04 [1] CRAN (R 4.4.0)
#>  knitr            1.48       2024-07-07 [1] CRAN (R 4.4.0)
#>  lifecycle        1.0.4      2023-11-07 [1] CRAN (R 4.4.0)
#>  magrittr         2.0.3      2022-03-30 [1] CRAN (R 4.4.0)
#>  pillar           1.9.0      2023-03-22 [1] CRAN (R 4.4.0)
#>  pkgconfig        2.0.3      2019-09-22 [1] CRAN (R 4.4.0)
#>  posterior        1.6.0      2024-07-03 [1] CRAN (R 4.4.0)
#>  processx         3.8.4      2024-03-16 [1] CRAN (R 4.4.0)
#>  ps               1.8.1      2024-10-28 [1] CRAN (R 4.4.1)
#>  R6               2.5.1      2021-08-19 [1] CRAN (R 4.4.0)
#>  reprex           2.1.1      2024-07-06 [1] CRAN (R 4.4.0)
#>  rlang            1.1.4      2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown        2.28       2024-08-17 [1] CRAN (R 4.4.0)
#>  rstudioapi       0.16.0     2024-03-24 [1] CRAN (R 4.4.0)
#>  sessioninfo      1.2.2      2021-12-06 [1] CRAN (R 4.4.0)
#>  tensorA          0.36.2.1   2023-12-13 [1] CRAN (R 4.4.0)
#>  tibble           3.2.1      2023-03-20 [1] CRAN (R 4.4.0)
#>  utf8             1.2.4      2023-10-22 [1] CRAN (R 4.4.0)
#>  vctrs            0.6.5      2023-12-01 [1] CRAN (R 4.4.0)
#>  withr            3.0.2      2024-10-28 [1] CRAN (R 4.4.1)
#>  xfun             0.49       2024-10-31 [1] CRAN (R 4.4.1)
#>  yaml             2.3.10     2024-07-26 [1] CRAN (R 4.4.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@mitzimorris
Copy link
Member

the fixed param sampler doesn't emit any diagnostics - there is nothing to report.

cf: src/stan/mcmc/fixed_param_sampler.hpp.

@jgabry
Copy link
Member

jgabry commented Nov 13, 2024

Yeah, like @mitzimorris said, there are no diagnostics in this case. I'll make a PR with a better error message and ensure the behavior is the same regardless of whether or not the object is a previously saved object. Thanks for reporting this.

@jgabry
Copy link
Member

jgabry commented Nov 13, 2024

Should be fixed by #1033

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants