Skip to content

mismatch in number dimensions declared with Pathfinder inits #992

Closed
@StaffanBetner

Description

@StaffanBetner

Describe the bug
When I use Pathfinder to get inits I get "mismatch in number dimensions declared" as error for some models.

To Reproduce
Reprex:

library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
library(nlme)
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
library(cmdstanr)
#> This is cmdstanr version 0.8.1
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: C:/Users/STBE21/.cmdstan/cmdstan-2.35.0
#> - CmdStan version: 2.35.0
library(brms)
#> Loading required package: Rcpp
#> Loading 'brms' package (version 2.21.3). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#> 
#> Attaching package: 'brms'
#> The following object is masked from 'package:stats':
#> 
#>     ar
BodyWeight %>% 
  as_tibble() %>% 
  clean_names() %>% 
  mutate(time2=time %>% factor %>% as.numeric()) -> 
  body_weight


  
  brm(data = body_weight, formula = weight ~ diet + ar(p = 1, time = time2, gr=rat)+(1|rat), backend="cmdstanr", cores =6, chains=6, empty = T) -> 
    brm_empty
  
  cmdstanr::write_stan_file(brm_empty$model) %>% 
    cmdstan_model(cpp_options = list(stan_threads = TRUE)) -> 
    model_cmdstan
  
  model_cmdstan$pathfinder(data = make_standata(data = body_weight, 
                                                              formula = weight ~ diet + ar(p = 1, time = time2, gr=rat)+(1|rat)), 
                           init = 2, history_size = 100, 
                           num_threads = 6, 
                           num_paths = 6) ->
    pathfinder_inits
#> Path [1] :Initial log joint density = -2354013.421594 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite function evaluation. 
#> Path [4] :Initial log joint density = -2837875.019409 
#> Path [2] :Initial log joint density = -13657453.097900 
#> Path [3] :Initial log joint density = -5782846.815396 
#> Path [6] :Initial log joint density = -21086717.897784 
#> Path [5] :Initial log joint density = -117122627.277960 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite function evaluation. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite function evaluation. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite function evaluation. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite function evaluation. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Error evaluating model log probability: Non-finite gradient. 
#> Path [4] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>              88      -5.855e+02      4.938e-04   9.595e-02    1.000e+00  1.000e+00      2201 -3.697e+03 -3.697e+03                   
#> Path [4] :Best Iter: [40] ELBO (-662.093438) evaluations: (2201) 
#> Path [5] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>              99      -5.855e+02      2.429e-02   1.419e+01    1.000e+00  1.000e+00      2476 -7.844e+02 -7.844e+02                   
#> Path [5] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>             115      -5.854e+02      3.519e-04   7.368e-02    7.838e-01  7.838e-01      2876 -1.467e+03 -1.467e+03                   
#> Path [5] :Best Iter: [60] ELBO (-662.139363) evaluations: (2876) 
#> Path [3] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>              99      -1.484e+03      4.062e-02   1.668e+02    3.698e-01  3.698e-02      2476 -1.613e+03 -1.613e+03                   
#> Path [1] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>              99      -5.854e+02      4.431e-04   1.919e-01    4.818e-01  4.818e-01      2476 -9.339e+02 -9.339e+02                   
#> Path [1] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>             100      -5.854e+02      5.683e-04   8.569e-02    1.000e+00  1.000e+00      2501 -8.646e+02 -8.646e+02                   
#> Path [1] :Best Iter: [49] ELBO (-661.727387) evaluations: (2501) 
#> Path [6] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>              99      -5.864e+02      4.325e-04   6.753e+00    1.000e+00  1.000e+00      2476 -6.850e+02 -6.850e+02                   
#> Path [2] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>              99      -5.855e+02      5.448e-03   2.277e+01    1.000e+00  1.000e+00      2476 -6.847e+02 -6.847e+02                   
#> Path [2] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>             123      -5.854e+02      4.205e-04   1.956e-01    4.433e-01  4.433e-01      3076 -1.200e+03 -1.200e+03                   
#> Path [2] :Best Iter: [101] ELBO (-681.050138) evaluations: (3076) 
#> Path [6] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>             153      -5.853e+02      8.429e-04   2.050e-01    1.000e+00  1.000e+00      3826 -4.474e+03 -4.474e+03                   
#> Path [6] :Best Iter: [127] ELBO (-679.735085) evaluations: (3826) 
#> Path [3] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>             199      -1.463e+03      4.024e-02   2.453e+03    1.880e-01  1.880e-01      4976 -1.618e+03 -1.618e+03                   
#> Path [3] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>             299      -1.415e+03      2.415e-01   1.322e+05    1.698e-01  3.418e-01      7476 -1.606e+03 -1.606e+03                   
#> Path [3] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>             399      -1.314e+03      3.157e-01   1.600e+06    6.980e-01  6.980e-01      9976 -1.785e+03 -1.785e+03                   
#> Path [3] : Iter      log prob        ||dx||      ||grad||     alpha      alpha0      # evals       ELBO    Best ELBO        Notes  
#>             492      -6.218e+02      1.854e-05   3.144e+04    8.008e-01  8.008e-01     12301 -3.149e+04 -3.149e+04                   
#> Path [3] :Best Iter: [383] ELBO (-1566.436377) evaluations: (12301) 
#> Total log probability function evaluations:32631 
#> Pareto k value (1.4) is greater than 0.7. Importance resampling was not able to improve the approximation, which may indicate that the approximation itself is poor. 
#> Finished in  0.3 seconds.
  
  model_cmdstan$sample(data = make_standata(data = body_weight, 
                                            formula = weight ~ diet + ar(p = 1, time = time2, gr=rat)+(1|rat)),
                       init = pathfinder_inits,
                       threads_per_chain = 1,
                       parallel_chains  = 6,
                       chains = 6) ->
    samples
#> Running MCMC with 6 parallel chains, with 1 thread(s) per chain...
#> Chain 1 Unrecoverable error evaluating the log probability at the initial value.
#> Chain 1 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,16); dims found=(16) (in 'C:/Users/STBE21/AppData/Local/Temp/Rtmp08oU96/model-b0542cd44c4b.stan', line 38, column 2 to column 29)
#> Chain 2 Unrecoverable error evaluating the log probability at the initial value.
#> Chain 2 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,16); dims found=(16) (in 'C:/Users/STBE21/AppData/Local/Temp/Rtmp08oU96/model-b0542cd44c4b.stan', line 38, column 2 to column 29)
#> Chain 3 Unrecoverable error evaluating the log probability at the initial value.
#> Chain 3 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,16); dims found=(16) (in 'C:/Users/STBE21/AppData/Local/Temp/Rtmp08oU96/model-b0542cd44c4b.stan', line 38, column 2 to column 29)
#> Chain 4 Unrecoverable error evaluating the log probability at the initial value.
#> Chain 4 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,16); dims found=(16) (in 'C:/Users/STBE21/AppData/Local/Temp/Rtmp08oU96/model-b0542cd44c4b.stan', line 38, column 2 to column 29)
#> Chain 5 Unrecoverable error evaluating the log probability at the initial value.
#> Chain 5 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,16); dims found=(16) (in 'C:/Users/STBE21/AppData/Local/Temp/Rtmp08oU96/model-b0542cd44c4b.stan', line 38, column 2 to column 29)
#> Chain 6 Unrecoverable error evaluating the log probability at the initial value.
#> Chain 6 Exception: mismatch in number dimensions declared and found in context; processing stage=parameter initialization; variable name=z_1; dims declared=(1,16); dims found=(16) (in 'C:/Users/STBE21/AppData/Local/Temp/Rtmp08oU96/model-b0542cd44c4b.stan', line 38, column 2 to column 29)
#> Warning: Chain 1 finished unexpectedly!
#> Warning: Chain 2 finished unexpectedly!
#> Warning: Chain 3 finished unexpectedly!
#> Warning: Chain 4 finished unexpectedly!
#> Warning: Chain 5 finished unexpectedly!
#> Warning: Chain 6 finished unexpectedly!
#> Warning: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
#> Warning: Use read_cmdstan_csv() to read the results of the failed chains.
#> Warning: No chains finished successfully. Unable to retrieve the fit.

Created on 2024-06-07 with reprex v2.1.0

Operating system
Windows 10.

CmdStanR version number

packageVersion("cmdstanr")
[1] ‘0.8.1’

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions