RStan installation on a Windows 10/11 or a WURclient computer

Image credit: Panos Sakalakis on Unsplash
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 state-of-the-art platform for statistical modeling and high-performance 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 (L-BFGS)

Stan’s math library provides differentiable probability functions & linear algebra (C++ autodiff). Additional R packages provide expression-based linear modeling, posterior visualization, and leave-one-out cross-validation.

This post will show how to install Rtools on a privately owned computer running Windows 10/11, or a WURclient desktop or laptop computer.

Requirements for RStan installation

Prior requirements for installing RStan:

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:

Installation

The RStan installation, described here, assumes usage of R version 4.2.x or above. It uses the latest versions of the rstan and StanHeaders packages available on CRAN.

To install RStan:

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

  2. Remove, if present, the existing installation of the packages StanHeaders and rstan by executing at the R prompt:

remove.packages(pkgs = c("StanHeaders", "rstan"))
  1. Install the latest on CRAN available version of StanHeaders and rstan by executing at the R prompt:
install.packages(pkgs = c("StanHeaders","rstan"),
                repos = c("https://cloud.r-project.org",
                getOption("repos")))
Congratulations, 😆, you now have RStan installed on your personal Windows 10/11 or WURclient computer!

Verifying the RStan installation

To verify the RStan installation run the RStan example/test model at the R prompt:

# Load the RStan library
library(rstan)
# Start up settings
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
rstan_options(threads_per_chain = 1)
# Run example
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_64-w64-mingw32.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_64-w64-mingw32.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_64-w64-mingw32.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_64-w64-mingw32.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.32.x or above. Executing the following commands will show the installed versions:

packageVersion(pkg = "StanHeaders")
packageVersion(pkg = "rstan")

When the installed versions are lower than version 2.32.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 start up message says, when using rstan locally on a multi-core 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 start up 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")
Maikel Verouden, Ph.D.
Maikel Verouden, Ph.D.
Researcher | Lecturer | IT & Organization contact person

My research interests include Statistics, Teaching and programmable matter (statistical software).