Stan
is a probabilistic programming language for
Bayesian statistical inference. The R
package
rstan
allows users to use Stan
functions
within R
. Installing rstan
involves installing
several other programs, the required steps differ depending on your
operating system. Everything in this document is repeated (sometimes in
more detail) on the rstan
installation
page. There is also an extensive and active Stan
user forum. If you have
problems, we suggest looking there first for solutions.
For this guide, we will assume you are using either Windows or Mac OS
(Catalina or greater) with the most recent version of R
(version 4.3.1).
If you have a different operating system, or a different version of
R
, please go to the rstan
installation page
for instructions for your specific case. We have had trouble recently
installing rstan
with older versions of R
. If
something fails, start by updating R
.
In general, it may take some time to install all necessary programs, and during installation you may see a lot of messages in the console. This is normal, and as long as there are no explicit error messages, everything is fine.
To follow the guide, copy and paste the grey blocks of code into your
R
console.
All of this information is available on the macrtools
website.
Install the remotes
package (if it is not already
installed). When prompted, you do not need to update any of your other
packages.
install.packages("remotes")
Then, use remotes to install the package macrtools
from
github.
remotes::install_github("coatless-mac/macrtools")
Now, use macrtools
to install XCode CLI, gfortran and R
development binaries. This may take some time. There will be a
pop-window which asks for your password; enter the password for your
computer.
macrtools::macos_rtools_install()
You can see if XCode CLI and gfortran successfully installed by running:
macrtools::is_xcode_cli_installed()
macrtools::is_gfortran_installed()
If there’s a chance you had a previous installation of
rstan
on your computer, remove it
remove.packages("rstan")
if (file.exists(".RData")) file.remove(".RData")
Then, restart R
. Then, install from source.
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))
install.packages(c("StanHeaders","rstan"),type="source")
This will produce a lot of text in your console, and may take some time.
example(stan_model, package = "rstan", run.dontrun = TRUE)
If you see output at the end that looks like
SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 4).
Chain 4:
Chain 4: Gradient evaluation took 0 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4:
Chain 4:
Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 4:
Chain 4: Elapsed Time: 0.005541 seconds (Warm-up)
Chain 4: 0.005559 seconds (Sampling)
Chain 4: 0.0111 seconds (Total)
Chain 4:
then you have successfully installed!
This step is optional.
Copy and paste this into your R console
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars")
if (!file.exists(M)) file.create(M)
arch <- ifelse(R.version$arch == "aarch64", "arm64", "x86_64")
cat(paste("\nCXX14FLAGS += -O3 -mtune=native -arch", arch, "-ftemplate-depth-256"),
file = M, sep = "\n", append = FALSE)
Download Rtools43 and install with all default settings as instructed here.
If there’s a chance you had a previous installation of rstan on your computer, remove it
remove.packages("rstan")
if (file.exists(".RData")) file.remove(".RData")
The current version of rstan
on CRAN is not working with
either R 4.3, but version 2.26 should work. Install it (and the
accompanying package StanHeaders
) using this code
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
Try running the rstan
example
example(stan_model, package = "rstan", run.dontrun = TRUE)
If you see output at the end that looks like
SAMPLING FOR MODEL '16a540c6086086816528e4524def24d9' NOW (CHAIN 4).
Chain 4:
Chain 4: Gradient evaluation took 0 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4:
Chain 4:
Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 4:
Chain 4: Elapsed Time: 0.005541 seconds (Warm-up)
Chain 4: 0.005559 seconds (Sampling)
Chain 4: 0.0111 seconds (Total)
Chain 4:
then you have successfully installed!
NOTE: If you are not comfortable with debugging in R, we recommend skipping this step for now, as it may create a bug you’ll need to resolve.
If you have the package withr
version 2.2.0 or greater
installed, this step may cause problems.
You can install withr
version 2.2.0 using:
# install.packages("devtools") # do this first if you don't have devtools installed
devtools::install_version("withr", version="2.2.0")
Copy and paste this into your R
console
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars.win")
if (!file.exists(M)) file.create(M)
cat("\n CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
file = M, sep = "\n", append = FALSE)