rmcelreath / rethinking

Statistical Rethinking course and book package
2.1k stars 596 forks source link

processx error with ulam() #391

Open jswesner opened 1 year ago

jswesner commented 1 year ago

Hi: I'm having trouble fitting a model with ulam. The model appears to start sampling, but ends in an error that "processx_exec failed". Th model works fine with quap(), and other models also work fine in rstan or brms. I've updated all packages (including cmdstan and process (the package referenced in the error). Any advice on next steps would be great. Thanks, jeff

Here's the model: m5.3 = ulam( alist( D ~ dnorm(mu, sigma), mu <- a + bM*M + bA*A, a ~ dnorm(0, 0.3), bM ~ dnorm(0, 0.5), bA ~ dnorm(0, 0.5), sigma ~ dexp(1)), data = list(D = d$D, A = d$A, M = d$M))

Here's the error: Running MCMC with 1 chain, with 1 thread(s) per chain...

Error in process_initialize(self, private, command, args, stdin, stdout, …: ! Native call to processx_exec failed Caused by error in chain_call(c_processx_exec, command, c(command, args), pty, pty_options, …: ! create process 'ulam_cmdstanr_26394daa2cfa2ded9eea3abe7aa8d8c2.exe' (system error 5, Access is denied. ) @win/processx.c:1040 (processx_exec)

Backtrace:

  1. rethinking::ulam(alist(D ~ dnorm(mu, sigma), mu <- a + bM M + bA A, a ~ …
  2. mod$sample(data = data, chains = chains, parallel_chains = cores, …
  3. runset$run_cmdstan()
  4. private[[run_method]]()
  5. procs$new_proc(id = chain_id, command = self$command(), args = self$command_args()[[cha…
  6. withr::with_path(c(toolchain_PATH_env_var(), tbbpath()), private$processes[[id]] <- w…
  7. base::force(code)
  8. cmdstanr:::wsl_compatible_process_new(command = command, args = args, wd = wd, …
  9. base::do.call(processx::process$new, run_args)
    1. (function (...) …
    2. local initialize(...)
    3. processx:::process_initialize(self, private, command, args, stdin, stdout, …
    4. processx:::chain_call(c_processx_exec, command, c(command, args), pty, pty_options, …
    5. | base::withCallingHandlers(do.call(".Call", list(.NAME, ...)), error = function(e) { …
    6. | base::do.call(".Call", list(.NAME, ...))
    7. | base::.handleSimpleError(function (e) …
    8. | local h(simpleError(msg, call))
    9. | processx:::throw_error(err, parent = e)

Here is my system info: OS Name Microsoft Windows 10 Enterprise Version 10.0.19044 Build 19044 platform x86_64-w64-mingw32
arch x86_64
os mingw32
crt ucrt
system x86_64, mingw32
major 4
minor 2.2
year 2022
month 10
day 31
svn rev 83211
language R
version.string R version 4.2.2 (2022-10-31 ucrt) nickname Innocent and Trusting

rmcelreath commented 1 year ago

The problem is not my package, but that cmdstanr doesn't have permission to write to the directory. So it couldn't write the compiled model to disk. Seems like other people have had this problem and there is an option in cmdstanr to change the path? This is what I found by searching a bit: https://discourse.mc-stan.org/t/permissions-error-running-brms-on-windows-with-cmdstanr-backend/28429

jswesner commented 1 year ago

Thank you. I tried changing the path as suggested there with no luck. But since the issue is in cmdstanr and permissions, I'll see if I can resolve it within cmdstanr first.

rmcelreath commented 1 year ago

Sorry I can't be of more help. But yes, if you can get cmdstanr to work on, then ulam() should work. ulam() just builds Stan code and passes it to cmdstanr for the hard part.

torkar commented 1 year ago

Remember that you need to compile cmdstan etc. again with a new path.

jswesner commented 1 year ago

I've found a workaround by using rstan as the backend instead of cmdstan with set_ulam_cmdstan(F). In case it was confusing to anyone else, I first tried to use rstan within the ulam() function: ulam(model...., cmdstan = F). But this did not work. After reading the help closer, I needed to use set_ulam_cmdstan(F) first outside of the ulam() model. Then running the ulam() model worked fine.