One simple trick to make Stan run faster

Did you know that Stan automatically runs in parallel (and caches compiled models) from R if you do this:

source(“http://mc-stan.org/rstan/stan.R”)

P.S. This capability is automatically in the current version of rstan which you can load in from Cran.

19 thoughts on “One simple trick to make Stan run faster

  1. @Rahul the source command loads in the R code. It’s used when you keep functions in separate documents. In this case, the function is located at an email address. From what I can tell, the function is basically a version of the stan function set up to always use parallel. Not sure how well it would work on windows machines.

      • Yep. There are a few of R’s commands that mimic shell commands; for example, R’s “ls” function lists objects in the workspace much as the shell command list files in the local directory.

    • This version of the stan function uses “mclapply”, and on Windows, mclapply just calls (the standard non-parallelized function) “lapply” (unless you try to set the mc.cores argument to a value greater than one, in which case it throws an error).

      (That address is a URL, not an email address…)

      • What aspect of this would violate CRAN policies? I’ve seen lots of packages that “suggest” the `parallel` package and only use it if available.

        Are the CRAN violations Windows-specific? If not, could the parallel functionality still be added for Mac and Linux users?

        Thanks

  2. I’d like to point out that this is a TERRIBLE way to get the functionality. Specifically, there could be anything in that .R file on the server, so for example it might contain code to maliciously delete everything in your home directory, or whatever.

    Even if today there’s nothing wrong with the code, tomorrow some script-kiddie could find a vulnerability and replace that code on the server with their malicious file deleting, or personal data collecting alternative code.

    Never source something from a URL, go and get the contents, put it in your own directory, verify that it seems reasonable, and then source your local copy!

    • It is interesting that R actually allows a source cmd to execute with a remote file over the net.

      I wonder if a Linux shell’s source command will source a remote script.sh over the net. I’ve never tried it.

      • Yes, the shell will allow one to do all sorts of things. I’ve seen people using curl to fetch some script and pipe it directly to sh as a means of running installers. Not recommended.

  3. The parallel thing doesn’t violate CRAN restrictions; the other part of that function which silently writes the compiled model to the disk does. However, we have been given permission to write if the user specifies a non-default value for one of the options(). We might implement some options this week to facilitate doing chains in parallel too, but the logic of that function is essentially only right for a person working on a multicore laptop with lots of RAM relative to the model, which happens to be the only way Andrew uses Stan. To force it to always run that way would make it not work on clusters or for people with little RAM relative to the model.

Leave a Reply to Chris Cancel reply

Your email address will not be published. Required fields are marked *