Brms/cmdstanr looking for things in invalid directory

Windows 10. I have installed cmdstan itself to C:\cmdstan2-26.1 manually and added the necessary Rtools paths to Windows’ Path variable. In Command Prompt, I am already able to run the bernoulli example successfully.

But I cannot use cmdstanr with brms to fit models with cmdstan functionality in R. Either cmdstan or cmdstanr is looking for things in the wrong directory:

require(brms)
require(cmdstanr)
testmod <- brm(y ~ (1|groups) + (1|subjects) + x1 + x2, prior = mypriors, family = categorical, data = mydata, decomp = "QR", backend = "cmdstanr")

and I get:

Compiling Stan program…
C:\Users\Folio:1: *** target pattern contains no ‘%’. Stop.
Error: An error occured during compilation! See the message above for more information.

Also, when I issue a diagnostic command seen in some other threads, the following error occurs:

cat(readLines(file.path(cmdstan_default_install_path(), "cmdstan-2.24.0", "stan","lib","stan_math","lib","boost_1.72.0","bootstrap.log")), sep = "\n")

Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:\Users\Folio 1040\Documents/.cmdstanr/cmdstan-2.24.0/stan/lib/stan_math/lib/boost_1.72.0/bootstrap.log’: No such file or directory

Note that the path that it’s trying to access is completely invalid: backslashes change into forward slashes mid-string. Also, I’m aware that having a space in the username is not great – however, that username was created by the person who sold me this laptop, so I have no control over it.

Could it be the space you have in “Folio 1040”?

It’s possible, but that’s a username (chosen by the previous owner of the laptop) and I can’t change it. I could perhaps rename the directory, but I suspect a lot of things would then break because the directory name would no longer match the username.

Hi,

can you run:

cmdstanr::check_cmdstan_toolchain()

and paste the output?
Also maybe the output of:

cmdstanr::cmdstan_version()

and

fit <- cmdstanr::cmdstanr_example("logistic", quiet = FALSE)

In general I would advise installing cmdstan via the install_cmdstan() command as that also takes care of everything you need to make CmdStan(r) work on windows (install mingw32-make, fix and prepare PATH, etc). The only thing you should need to do apart from install_cmdstan() is install RTools.

Given that you install cmdstan manually, did you use set_cmdstan_path() to point cmdstanr to the install location?

> cmdstanr::cmdstan_version()
[1] "2.26.1"
> fit <- cmdstanr::cmdstanr_example("logistic", quiet = FALSE)
Compiling Stan program...
C:\Users\Folio:1: *** target pattern contains no '%'.  Stop.
Error: An error occured during compilation! See the message above for more information.

Thanks.

What about:

file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
mod <- cmdstan_model(file, quiet = FALSE)

I forgot quiet = FALSE, I edited the above command.

@rok_cesnovar:

EDIT: now using quiet = FALSE:

Compiling Stan program...
C:\Users\Folio:1: *** target pattern contains no '%'.  Stop.
C:\Users\Folio:1: *** target pattern contains no '%'.  Stop.
Error: An error occured during compilation! See the message above for more information.
Model executable is up to date!

I did indeed do set_cmdstan_path("C:/cmdstan-2.26.1") (the correct path) before trying to fit my model. The strange thing is that after the model fails to fit, calling set_cmdstan_path() reports the cmdstan path as “CmdStan path set to: C:/Users/Folio 1040/Documents/.cmdstanr/cmdstan-2.26.1”, which is obviously the wrong directory. It’s like something overrules the manual setting, pointing again to the wrong directory.

Thanks. One more thing to try

   set_cmdstan_path("C:/cmdstan-2.26.1") 
   file <- file.path(cmdstan_path(), " examples", "bernoulli", "bernoulli.stan")
   mod <- cmdstan_model(file, quiet = FALSE, force_recompile = TRUE)

calling set_cmdstan_path() with no arguments will set the default path which is the one you paste. set_cmdstan_path() should only be used when you want to switch back to the default install location. Otherwise always use set_cmdstan_path("C:/cmdstan-2.26.1")

Does cmdstanr::check_cmdstan_toolchain() return any problems?

> set_cmdstan_path("C:/cmdstan-2.26.1")
CmdStan path set to: C:/cmdstan-2.26.1
> file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
> mod <- cmdstan_model(file, quiet = FALSE, force_recompile = TRUE)
Compiling Stan program...
C:\Users\Folio:1: *** target pattern contains no '%'.  Stop.
C:\Users\Folio:1: *** target pattern contains no '%'.  Stop.
Error: An error occured during compilation! See the message above for more information.

and

cmdstanr::check_cmdstan_toolchain()
The CmdStan toolchain is setup properly!

If you feel quite sure that re-installing cmdstan and cmdstanr from scratch from within RStudio will help, then I can certainly try that and report back. I seem to recall that that was the first thing I tried this morning, and then opted for the manual approach only after encountering problems. But I could try again.

Before you go reinstalling, please check one more thing.

Please print what

tempdir()

returns. I suspect tempdir has spaces.

> tempdir()
[1] "C:\\Users\\FOLIO1~1\\AppData\\Local\\Temp\\RtmpSUg3SD"

Yeah, the FOLIO 1040 thing is a pre-specified username that I can’t change.

Yeah, I get that. It should not be a problem but it seems that it is.

We will move tmpdir for R to a different location and that will work until we find a workaround for this in cmdstanr. Can you output:

Sys.getenv("TMP")
Sys.getenv("TEMP")
Sys.getenv("TMPDIR")
> Sys.getenv("TMP")
[1] "C:\\Users\\FOLIO1~1\\AppData\\Local\\Temp"
> Sys.getenv("TEMP")
[1] "C:\\Users\\FOLIO1~1\\AppData\\Local\\Temp"
> Sys.getenv("TMPDIR")
[1] ""

Great. So now please create a folder C:/Rtemp (or any other name you prefer) and run

write('TMP="C:/Rtemp"', file = "~/.Renviron", append = TRUE)
write('TEMP="C:/Rtemp"', file = "~/.Renviron", append = TRUE)

Then restart Rstudio and then

Sys.getenv("TMP")
Sys.getenv("TEMP")

should output c:/Rtemp. If so, try to compile the model again.

> Sys.getenv("TMP")
[1] "C:/Rtemp"
> Sys.getenv("TEMP")
[1] "C:/Rtemp"

require(brms)
Loading required package: brms
Loading required package: Rcpp
Loading 'brms' package (version 2.14.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

require(cmdstanr)
Loading required package: cmdstanr
This is cmdstanr version 0.3.0
- Online documentation and vignettes at mc-stan.org/cmdstanr
- CmdStan path set to: C:/Users/Folio 1040/Documents/.cmdstanr/cmdstan-2.26.1
- Use set_cmdstan_path() to change the path

set_cmdstan_path("C:/cmdstan-2.26.1")
CmdStan path set to: C:/cmdstan-2.26.1

time0 <- Sys.time();testmod <- brm(dep.var ~ (1|trigger) + (1|CompLemma) + Word.Dist + time.period + polarity, prior = wdistpriors.both, family = categorical, data = d,
                                  decomp = "QR", backend = "cmdstanr");Sys.time()-time0

Compiling Stan program...
C:\Users\Folio:1: *** target pattern contains no '%'.  Stop.
Error: An error occured during compilation! See the message above for more information.
In addition: Warning messages:
1: Specifying global priors for regression coefficients in categorical models is deprecated. Please specify priors separately for each response category. 
2: Specifying global priors for regression coefficients in categorical models is deprecated. Please specify priors separately for each response category. 

It’s as if it forgets the new settings as soon as cmdstanr is called by brms.

Does

tempdir()

now also output C:/Rtemp/…?

What about:

set_cmdstan_path("C:/cmdstan-2.26.1") 
file <- file.path(cmdstan_path(), " examples", "bernoulli", "bernoulli.stan")
mod <- cmdstan_model(file, quiet = FALSE, force_recompile = TRUE)

Does this now work or still no?

Sorry this is so frustrating. And thanks for the patience. I want to figure out what is happening so we can help automatically in cmdstanr.

> tempdir()
[1] "C:/Rtemp\\RtmpqgzyCr"

This looks mostly correct aside from the mix of forward slashes and backslashes.

> set_cmdstan_path("C:/cmdstan-2.26.1") 
CmdStan path set to: C:/cmdstan-2.26.1
> file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
> mod <- cmdstan_model(file, quiet = FALSE, force_recompile = TRUE)
Compiling Stan program...
C:\Users\Folio:1: *** target pattern contains no '%'.  Stop.
C:\Users\Folio:1: *** target pattern contains no '%'.  Stop.
Error: An error occured during compilation! See the message above for more information.

It insists on looking for files in the directory whose path has spaces. Perhaps I should mention that while cmdstan was installed manually to C:\cmdstan-2.26.1, cmdstanr was installed by using the installer in RStudio. So cmdstanr is located in the default installation directory (whose path contains spaces) even though cmdstan itself is not.