Getting "Error in file(con, "r"): cannot open the connection" with cmdstanr backend

When I run cmdstan_example() it fails the first time with “Error in file(con, “r”): cannot open the connection,” but works as expected if run the same command a second time (on the second try it also prints the message “Model executable is up to date!”).

If I start a clean session and try to fit a model with brms, it runs as expected with “rstan” as the backend, but fails with “cmdstanr” as the backend, throwing the same error as above. The error continues to occur if I re-run the model with the “cmdstanr” backend.

Below are reprexes showing the errors I’m getting, followed by information on my operating system and toolchain. I’ve been able to compile models without any issues in the past, but hadn’t fit a model in a while and just updated to the latest versions of the various software. Any ideas what’s causing this?

First reprex: cmdstan_example()

library(cmdstanr)
#> This is cmdstanr version 0.7.0
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: /Users/jschwartz/.cmdstan/cmdstan-2.34.0
#> - CmdStan version: 2.34.0

cmdstanr_example()
#> Error in file(con, "r"): cannot open the connection

cmdstanr_example()
#>    variable   mean median   sd  mad     q5    q95 rhat ess_bulk ess_tail
#>  lp__       -65.95 -65.61 1.41 1.22 -68.66 -64.31 1.00     2059     2672
#>  alpha        0.38   0.38 0.21 0.21   0.03   0.74 1.00     4187     2650
#>  beta[1]     -0.66  -0.65 0.25 0.25  -1.08  -0.26 1.00     4135     2592
#>  beta[2]     -0.27  -0.27 0.23 0.22  -0.64   0.10 1.00     4127     2818
#>  beta[3]      0.67   0.67 0.27 0.27   0.24   1.13 1.00     4475     2920
#>  log_lik[1]  -0.52  -0.51 0.10 0.10  -0.69  -0.37 1.00     3961     2885
#>  log_lik[2]  -0.41  -0.39 0.15 0.15  -0.69  -0.20 1.00     4488     2980
#>  log_lik[3]  -0.50  -0.47 0.22 0.21  -0.88  -0.20 1.00     3821     2752
#>  log_lik[4]  -0.45  -0.44 0.15 0.15  -0.74  -0.23 1.00     3910     3061
#>  log_lik[5]  -1.18  -1.16 0.28 0.28  -1.66  -0.76 1.00     4442     3197
#> 
#>  # showing 10 of 105 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option)

Second reprex: brms model with “cmdstanr” and “rstan” backends

library(brms)
#> Loading required package: Rcpp
#> Loading 'brms' package (version 2.20.4). 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

brm(mpg ~ hp, data=mtcars, prior=prior(normal(0,1), class="b"), 
    cores=4, backend="rstan")
#> Compiling Stan program...
#> Start sampling
#>  Family: gaussian 
#>   Links: mu = identity; sigma = identity 
#> Formula: mpg ~ hp 
#>    Data: mtcars (Number of observations: 32) 
#>   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
#>          total post-warmup draws = 4000
#> 
#> Population-Level Effects: 
#>           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> Intercept    30.08      1.67    26.82    33.43 1.00     3433     2797
#> hp           -0.07      0.01    -0.09    -0.05 1.00     3481     2627
#> 
#> Family Specific Parameters: 
#>       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> sigma     3.99      0.54     3.11     5.18 1.00     3183     2489
#> 
#> Draws were sampled using sampling(NUTS). 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).

brm(mpg ~ hp, data=mtcars, prior=prior(normal(0,1),class="b"), 
       cores=4, backend="cmdstanr")
#> Error in file(con, "r"): cannot open the connection

brm(mpg ~ hp, data=mtcars, prior=prior(normal(0,1),class="b"), 
       cores=4, backend="cmdstanr")
#> Error in file(con, "r"): cannot open the connection

Created on 2024-01-20 with reprex v2.1.0

System information:

  • Operating System: Apple M2 Max, Sonoma 14.2.1
  • CmdStan Version: 2.34.0
  • brms version: 2.20.4
  • Compiler/Toolkit: Below is output from the Terminal:
$ clang++ --version
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: arm64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ make --version
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for i386-apple-darwin11.3.0

You need to update cmdstanr: CmdStanR v0.7.1 released

1 Like

Thanks Andrew!