Error when running a random-intercept Wiener model: Exception: no more scalars to read (found before start of program)

I am trying to fit a random-intercept only Wiener model, following @Henrik_Singmann’s blog post but am running into the error Exception: no more scalars to read (found before start of program).

As far as I can tell, this is related to the inits since without them I don’t get this error (I instead get an error that I should use inits 🙃).

(Note the bogus data and sample_prior = "only" are for the rep-ex only - I get the same error with real data and sample_prior = "no".)

library(brms)

fs_data <- data.frame(
  ID = 1:62,
  RT = rlnorm(62, sdlog = 0.2),
  ACC = sample(0:1, 62, TRUE)
)

formula <- bf(RT | dec(ACC) ~ 1 + (1 | p | ID),
              bs ~ 1 + (1 | p | ID),
              ndt ~ 1 + (1 | p | ID),
              bias ~ 1 + (1 | p | ID))

wiener_fam <- wiener(
  link = "identity",
  link_bs = "identity", 
  link_ndt = "identity", 
  link_bias = "identity"
)

prior <- 
  set_prior("normal(0.28, 5)", class = "Intercept") + 
  set_prior("normal(0.31, 0.1)", class = "Intercept", dpar = "bs") + 
  set_prior("normal(0.27, 0.1)", class = "Intercept", dpar = "ndt") + 
  set_prior("normal(0.5, 0.2)", class = "Intercept", dpar = "bias")

tmp_dat <- make_standata(formula, 
                         family = wiener_fam,
                         data = fs_data, 
                         prior = prior)

initfun <- function() list(
  Intercept = 0.28,
  Intercept_bs = 0.3,
  Intercept_ndt = 0.3,
  Intercept_bias = 0.5,
  sd_1 = rep(0.7, tmp_dat$M_1),
  z_1 = matrix(0.05, tmp_dat$M_1, tmp_dat$N_1),
  L_1 = diag(tmp_dat$M_1)
)

set.seed(16112020) # selecting a seed until it runs
mod_random <- brm(formula,
                  data = fs_data,
                  family = wiener(),
                  sample_prior = "only", 
                  prior = prior, 
                  chains = 2, iter = 2000, warmup = 500,
                  cores = 2,
                  inits = initfun,
                  backend = "cmdstanr")
#> Compiling Stan program...
#> Start sampling
#> Running MCMC with 2 parallel chains...
#> 
#> Chain 1 Unrecoverable error evaluating the log probability at the initial value. 
#> Chain 1 Exception: no more scalars to read (found before start of program) 
#> Chain 1 Exception: no more scalars to read (found before start of program)
#> Chain 2 Unrecoverable error evaluating the log probability at the initial value. 
#> Chain 2 Exception: no more scalars to read (found before start of program) 
#> Chain 2 Exception: no more scalars to read (found before start of program)
#> Warning: Chain 1 finished unexpectedly!
#> 
#> Warning: Chain 2 finished unexpectedly!
#> 
#> Warning: Use read_cmdstan_csv() to read the results of the failed chains.
#> Error in cmdstanr::read_cmdstan_csv(out$output_files(), variables = "",  : 
#>   Assertion on 'files' failed: No file provided.
#> In addition: Warning messages:
#> 1: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
#>  
#> 2: No chains finished successfully. Unable to retrieve the fit.
  • Operating System: Win11
  • brms Version: 2.16.3
  • cmdstanr Version: 0.4.0

Cannot replicate the problem, runs for me:

> mod_random <- brm(formula,
+                   data = fs_data,
+                   family = wiener(),
+                   sample_prior = "only", 
+                   prior = prior, 
+                   chains = 2, iter = 2000, warmup = 500,
+                   cores = 2,
+                   inits = initfun,
+                   backend = "cmdstanr")
Compiling Stan program...
Start sampling
Running MCMC with 2 parallel chains...

Chain 1 Iteration:    1 / 2000 [  0%]  (Warmup) 
Chain 2 Iteration:    1 / 2000 [  0%]  (Warmup) 
Chain 1 Iteration:  100 / 2000 [  5%]  (Warmup) 
Chain 1 Iteration:  200 / 2000 [ 10%]  (Warmup) 
Chain 1 Iteration:  300 / 2000 [ 15%]  (Warmup) 
Chain 1 Iteration:  400 / 2000 [ 20%]  (Warmup) 
Chain 1 Iteration:  500 / 2000 [ 25%]  (Warmup) 
Chain 1 Iteration:  501 / 2000 [ 25%]  (Sampling) 
Chain 2 Iteration:  100 / 2000 [  5%]  (Warmup) 
Chain 2 Iteration:  200 / 2000 [ 10%]  (Warmup) 
Chain 2 Iteration:  300 / 2000 [ 15%]  (Warmup) 
Chain 2 Iteration:  400 / 2000 [ 20%]  (Warmup) 
Chain 2 Iteration:  500 / 2000 [ 25%]  (Warmup) 
Chain 2 Iteration:  501 / 2000 [ 25%]  (Sampling) 
Chain 1 Iteration:  600 / 2000 [ 30%]  (Sampling) 
Chain 2 Iteration:  600 / 2000 [ 30%]  (Sampling) 
Chain 1 Iteration:  700 / 2000 [ 35%]  (Sampling) 
Chain 2 Iteration:  700 / 2000 [ 35%]  (Sampling) 
Chain 1 Iteration:  800 / 2000 [ 40%]  (Sampling) 
Chain 2 Iteration:  800 / 2000 [ 40%]  (Sampling) 
Chain 1 Iteration:  900 / 2000 [ 45%]  (Sampling) 
Chain 2 Iteration:  900 / 2000 [ 45%]  (Sampling) 
Chain 1 Iteration: 1000 / 2000 [ 50%]  (Sampling) 
Chain 2 Iteration: 1000 / 2000 [ 50%]  (Sampling) 
Chain 1 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
Chain 2 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
Chain 1 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
Chain 2 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
Chain 1 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
Chain 2 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
Chain 1 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
Chain 2 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
Chain 1 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
Chain 2 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
Chain 1 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
Chain 2 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
Chain 1 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
Chain 2 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
Chain 2 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
Chain 1 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
Chain 1 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
Chain 2 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
Chain 1 Iteration: 2000 / 2000 [100%]  (Sampling) 
Chain 2 Iteration: 2000 / 2000 [100%]  (Sampling) 
Chain 1 finished in 3.7 seconds.
Chain 2 finished in 3.6 seconds.

Both chains finished successfully.
Mean chain execution time: 3.6 seconds.
Total execution time: 3.9 seconds.
> mod_random
 Family: wiener 
  Links: mu = identity; bs = log; ndt = log; bias = logit 
Formula: RT | dec(ACC) ~ 1 + (1 | p | ID) 
         bs ~ 1 + (1 | p | ID)
         ndt ~ 1 + (1 | p | ID)
         bias ~ 1 + (1 | p | ID)
   Data: fs_data (Number of observations: 62) 
  Draws: 2 chains, each with iter = 1500; warmup = 0; thin = 1;
         total post-warmup draws = 3000

Group-Level Effects: 
~ID (Number of levels: 62) 
                                  Estimate Est.Error l-95% CI
sd(Intercept)                         2.75      3.07     0.08
sd(bs_Intercept)                      2.76      3.06     0.08
sd(ndt_Intercept)                     2.81      3.15     0.10
sd(bias_Intercept)                    2.78      3.00     0.11
cor(Intercept,bs_Intercept)           0.01      0.44    -0.80
cor(Intercept,ndt_Intercept)         -0.00      0.42    -0.78
cor(bs_Intercept,ndt_Intercept)      -0.00      0.45    -0.81
cor(Intercept,bias_Intercept)        -0.01      0.45    -0.81
cor(bs_Intercept,bias_Intercept)      0.01      0.45    -0.80
cor(ndt_Intercept,bias_Intercept)    -0.01      0.45    -0.81
                                  u-95% CI Rhat Bulk_ESS
sd(Intercept)                        10.74 1.00     4139
sd(bs_Intercept)                     10.54 1.00     4094
sd(ndt_Intercept)                    10.58 1.00     5308
sd(bias_Intercept)                   11.17 1.00     4426
cor(Intercept,bs_Intercept)           0.81 1.00     7402
cor(Intercept,ndt_Intercept)          0.77 1.00     5852
cor(bs_Intercept,ndt_Intercept)       0.80 1.01     4474
cor(Intercept,bias_Intercept)         0.81 1.00     6278
cor(bs_Intercept,bias_Intercept)      0.82 1.00     3872
cor(ndt_Intercept,bias_Intercept)     0.81 1.00     2267
                                  Tail_ESS
sd(Intercept)                         1613
sd(bs_Intercept)                      1585
sd(ndt_Intercept)                     1950
sd(bias_Intercept)                    1714
cor(Intercept,bs_Intercept)           1877
cor(Intercept,ndt_Intercept)          2077
cor(bs_Intercept,ndt_Intercept)       2409
cor(Intercept,bias_Intercept)         1911
cor(bs_Intercept,bias_Intercept)      2253
cor(ndt_Intercept,bias_Intercept)     2293

Population-Level Effects: 
               Estimate Est.Error l-95% CI u-95% CI Rhat
Intercept          0.30      4.87    -9.32    10.02 1.00
bs_Intercept       0.31      0.10     0.12     0.51 1.00
ndt_Intercept      0.27      0.10     0.07     0.47 1.00
bias_Intercept     0.50      0.20     0.11     0.91 1.00
               Bulk_ESS Tail_ESS
Intercept          7811     2145
bs_Intercept       6697     2206
ndt_Intercept      6495     2043
bias_Intercept     6885     2219

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).

However note that you probably want to change family = wiener() to family = wiener_fam

Session info:

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] brms_2.16.3 Rcpp_1.0.7 

loaded via a namespace (and not attached):
  [1] nlme_3.1-153         matrixStats_0.61.0   xts_0.12.1           threejs_0.3.3       
  [5] rstan_2.26.4         tensorA_0.36.2       tools_4.1.2          backports_1.4.1     
  [9] utf8_1.2.2           R6_2.5.1             DT_0.20              mgcv_1.8-38         
 [13] projpred_2.0.2       DBI_1.1.2            colorspace_2.0-2     tidyselect_1.1.1    
 [17] gridExtra_2.3        prettyunits_1.1.1    processx_3.5.2       Brobdingnag_1.2-6   
 [21] emmeans_1.7.1-1      curl_4.3.2           compiler_4.1.2       cli_3.1.0           
 [25] shinyjs_2.1.0        sandwich_3.0-1       colourpicker_1.1.1   posterior_1.1.0     
 [29] scales_1.1.1         dygraphs_1.1.1.6     checkmate_2.0.0      mvtnorm_1.1-3       
 [33] ggridges_0.5.3       callr_3.7.0          stringr_1.4.0        digest_0.6.29       
 [37] StanHeaders_2.26.4   minqa_1.2.4          base64enc_0.1-3      pkgconfig_2.0.3     
 [41] htmltools_0.5.2      lme4_1.1-27.1        fastmap_1.1.0        htmlwidgets_1.5.4   
 [45] rlang_0.4.12         shiny_1.7.1          farver_2.1.0         generics_0.1.1      
 [49] zoo_1.8-9            jsonlite_1.7.2       crosstalk_1.2.0      gtools_3.9.2        
 [53] dplyr_1.0.7          distributional_0.2.2 inline_0.3.19        magrittr_2.0.1      
 [57] loo_2.4.1            bayesplot_1.8.1      Matrix_1.4-0         munsell_0.5.0       
 [61] fansi_0.5.0          abind_1.4-5          lifecycle_1.0.1      stringi_1.7.6       
 [65] multcomp_1.4-17      MASS_7.3-54          pkgbuild_1.3.1       plyr_1.8.6          
 [69] grid_4.1.2           parallel_4.1.2       promises_1.2.0.1     crayon_1.4.2        
 [73] miniUI_0.1.1.1       lattice_0.20-45      splines_4.1.2        knitr_1.37          
 [77] ps_1.6.0             pillar_1.6.4         igraph_1.2.10        boot_1.3-28         
 [81] markdown_1.1         estimability_1.3     shinystan_2.5.0      reshape2_1.4.4      
 [85] codetools_0.2-18     stats4_4.1.2         rstantools_2.1.1     glue_1.6.0          
 [89] V8_4.0.0             data.table_1.14.2    RcppParallel_5.1.4   nloptr_1.2.2.3      
 [93] vctrs_0.3.8          httpuv_1.6.4         gtable_0.3.0         purrr_0.3.4         
 [97] assertthat_0.2.1     ggplot2_3.3.5        xfun_0.29            mime_0.12           
[101] xtable_1.8-4         coda_0.19-4          later_1.3.0          rsconnect_0.8.25    
[105] survival_3.2-13      tibble_3.1.6         shinythemes_1.2.0    gamm4_0.2-6         
[109] cmdstanr_0.4.0       TH.data_1.1-0        ellipsis_0.3.2       bridgesampling_1.1-2

I tried this on another machine and also got it to work… very weird.
Oh well 🤷‍♂️