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 thrown with variational algorithms #1650

Closed
marcus-waldman opened this issue Apr 27, 2024 · 6 comments
Closed

Error thrown with variational algorithms #1650

marcus-waldman opened this issue Apr 27, 2024 · 6 comments
Labels
Milestone

Comments

@marcus-waldman
Copy link

Versions: R (4.4.0), brms (2.21.0), cmdstandr (0.7.1), Rtools43, Windows 11 Pro

I'm not sure if this is an issue that only I am having or if others are as well, but I get an error thrown if I fit any of the variational algorithms. For example,

 brm(
  count ~ zBase * Trt + (1|patient),
  data = epilepsy, family = poisson(),
  prior = prior(normal(0, 10), class = b) +
    prior(cauchy(0, 2), class = sd), 
  backend = "cmdstanr", 
  algorithm = "laplace", 
  draws = 1
)

results in the following error thrown:

Error in `[.data.frame`(diagnostics[[i]], rstan_diagn_order) : 
  undefined columns selected

Note that the estimation algorithm appeared to proceed normally:

Initial log joint probability = -6966.6 
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
Error evaluating model log probability: Non-finite gradient. 
      99       -652.23     0.0197727       18.9707           1           1      132    
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
     199      -650.988     0.0146375        7.2476           1           1      242    
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
     299      -650.797     0.0126656       2.16591           1           1      350    
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
     399      -650.687   0.000298552      0.171569      0.5594      0.5594      458    
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes  
     400      -650.687   0.000290087     0.0601614           1           1      459    
Optimization terminated normally:  
  Convergence detected: relative gradient magnitude is below tolerance 
Finished in  0.2 seconds.
Calculating Hessian 
Calculating inverse of Cholesky factor 
Generating draws 
iteration: 0 
Finished in  0.1 seconds.

I showed the code setting algorithm = laplace , but the exception is also thrown if I set to algorithm = pathfinder or algorithm = meanfield, as well.

If I use algorithm = sampling, no error is thrown.

All 4 chains finished successfully.
Mean chain execution time: 3.2 seconds.
Total execution time: 13.4 seconds.

 Family: poisson 
  Links: mu = log 
Formula: count ~ zBase * Trt + (1 | patient) 
   Data: epilepsy (Number of observations: 236) 
  Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
         total post-warmup draws = 4000

Multilevel Hyperparameters:
~patient (Number of levels: 59) 
              Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sd(Intercept)     0.59      0.07     0.47     0.74 1.00      872     1709

Regression Coefficients:
           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept      1.78      0.12     1.55     2.01 1.00      670     1381
zBase          0.70      0.12     0.46     0.94 1.01      740     1173
Trt1          -0.28      0.16    -0.58     0.03 1.00      732     1410
zBase:Trt1     0.03      0.16    -0.29     0.34 1.00      855     1411

Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).

Originally posted by @marcus-waldman in #1591 (comment)

@paul-buerkner
Copy link
Owner

Thanks for reporting this issue! Anybody else seeing this? I cannot reproduce it unfortunately. Which version of rstan do you have installed?

@lunafazio
Copy link
Contributor

Yes, managed to reproduce. I think it might actually be related to the version of R itself.

On 4.3.1:

diagnostics <- as.data.frame(matrix(nrow = 1, ncol = 0))
diagnostics[1:3]
# 1 NULL NULL NULL

On 4.4.0:

diagnostics <- as.data.frame(matrix(nrow = 1, ncol = 0))
diagnostics[1:3]
# Error in `[.data.frame`(diagnostics, 1:3) : undefined columns selected

The problem object is created here and the error is triggered when attempting to subset by rstan_diagn_order, here.

I traced the difference in behavior to this line of [.data.frame, which seems to produce a different output in R 4.4.0, which triggers the error two lines later. Couldn't figure out what it is that NextMethod calls, but I suppose this is enough information to figure out a workaround.

@paul-buerkner
Copy link
Owner

Thank you! This is very helpful!

paul-buerkner added a commit that referenced this issue Apr 30, 2024
@paul-buerkner
Copy link
Owner

I think I fixed it but I currently don't have an R 4.4.0 install to test it myself. Can one of you quickly check it out?

@lunafazio
Copy link
Contributor

It works now!

@paul-buerkner
Copy link
Owner

Great! Thank you!

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

No branches or pull requests

3 participants