RStan installation on a Windows 10/11 or a WURclient computer
Table of Contents
Motivation
Whether working or studying everybody uses various sorts and types of software on their computer. Students taking Statistical Courses, as taught by the Mathematical and Statistical Methods group at Wageningen University & Research, will most likely use R. Students enrolled in MAT34806 Bayesian Data Analysis will use RStan as well as basic R. Therefore, they will need to install RStan.
RStan is an implementation of Stan in R. Stan is a stateoftheart platform for statistical modeling and highperformance statistical computation. Thousands of users rely on Stan for statistical modeling, data analysis, and prediction in the social, biological, and physical sciences, engineering, and business.
Users specify log density functions in Stan’s probabilistic programming language and get:

full Bayesian statistical inference with MCMC sampling (NUTS, HMC)

approximate Bayesian inference with variational inference (ADVI)

penalized maximum likelihood estimation with optimization (LBFGS)
Stan’s math library provides differentiable probability functions & linear algebra (C++ autodiff). Additional R packages provide expressionbased linear modeling, posterior visualization, and leaveoneout crossvalidation.
Requirements for RStan installation
Prior requirements for installing RStan:

R properly installed:
 R installed on Windows 10/11
 R installed on a WURclient via Software Center, or
 R installed on a WURclient using a custom installation
Without having R and Rtools installed on your desktop or laptop computer, it makes no sense to try to install RStan. RStan needs to be able to compile from source within R, making R and Rtools indispensable.
Not required, however, highly recommended:

RStudio installed:
 RStudio installed on Windows 10/11
 RStudio installed on a WURclient via Software Ceneter, or
 RStudio installed on a WURclient using a custom installation
Installation
The RStan installation, described here, assumes usage of R version 4.2.x or above. It uses the latest development version (v2.26.x) of the RStan package, because of incompatibility of the current RStan package on CRAN (v2.21.x) for R versions above v4.2.x, as mentioned on The Stan Blog: Stan & R 4.2 on Windows.
To install RStan:

Start either RStudio, or the default R GUI. RStudio being the recommended interface!

Remove, if present, the existing installation of the packages
StanHeaders
andrstan
by executing at the R prompt:
remove.packages(pkgs = c("StanHeaders", "rstan"))
 Install the latest development version of
StanHeaders
andrstan
by executing at the R prompt:
install.packages(pkgs = c("StanHeaders","rstan"),
repos = c("https://mcstan.org/rpackages/",
getOption("repos")))
Verifying the RStan installation
To verify the RStan installation run the RStan example/test model at the R prompt:
example(stan_model, package = "rstan", run.dontrun = TRUE)
The model should then compile and sample.
If the example model fails with the error:
Error in compileCode(f, code, language = language, verbose = verbose) :
C:\rtools43\x86_64w64mingw32.static.posix\bin/ld.exe: file327c377a3dae.o:file327c377a3dae.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64w64mingw32.static.posix\bin/ld.exe: file327c377a3dae.o:file327c377a3dae.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64w64mingw32.static.posix\bin/ld.exe: file327c377a3dae.o:file327c377a3dae.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64w64mingw32.static.posix\bin/ld.exe: file327c377a3dae.o:file327c377a3dae.cpp:(.text$_ZN3tbb10interface
Error in sink(type = "output") : invalid connection
Verify that the packages StanHeaders
and rstan
installed are of version 2.26.x. Executing the following commands will show the installed versions:
packageVersion(pkg = "StanHeaders")
packageVersion(pkg = "rstan")
When the installed versions differ from version 2.26.x, then repeat steps 2. and 3. as described in the section Installation.
Loading the package
The package name is rstan
(all lowercase), to load the package execute:
library("rstan") # observe startup messages
As the startup message says, when using rstan
locally on a multicore machine and the system has plenty of RAM to estimate the model in parallel, at this point execute at the R prompt:
options(mc.cores = parallel::detectCores())
In addition, execute the second startup message at the R prompt:
rstan_options(auto_write = TRUE)
which allows to automatically save a bare version of a compiled Stan program to the hard disk so that it does not need to be recompiled (unless after changing the program). These commands will need to be run, each time the rstan
library is loaded load in R.
Finally, when using Windows as operating system, there will be a third startup message saying not to use march=native
compiler flag. This warning can be ignored, if the steps above have been followed and a Makevars.win
file on your system does not contain this flag. When the file name Makevars.win
does not ring any bells, this probably means it doesn’t exist in your system.
Installing the ‘brms’ package
In the course MAT34806 Bayesian Data Analysis the brms
package will be used. This package extends RStan for Bayesian regression modeling. The package abbreviation brms
stands for Bayesian Regression Models using ‘Stan’.
When the installation of RStan has been completed successfully, install the brms
package by executing at the R prompt the following command:
install.packages(pkgs = "brms")