User:MPopov (WMF)/Notes/RStan

From Meta, a Wikimedia project coordination wiki

With R 3.5.3 being available on Debian Buster hosts like stat1005 and stat1008, installation of {rstan}, {rstanarm}, and {brms} is as easy as:

install.packages(c("rstan", "brms"), repos = c(CRAN = "https://cran.rstudio.com/"))

Installing[edit]

Currently, all of our machines use R 3.3.3 because that's the latest version available for Debian Stretch through the official channel.[1] We've already requested newer versions in T220542 and T222933 but I'm not especially hopeful. For now, this means that trying to install RStan & RStanArm directly from CRAN is not going to work because they require R ≥3.4.0, so we're going to have to cheat a little bit by modifying which version of R the package requires. I have the modified package sources in my home dir on stat1004, stat1006, stat1007, notebook1003, and notebook1004, so unless they're missing you don't need to go through the process of modifying the package sources yourself.

By the way, assuming the package authors had very good reasons for requiring that specific version of R at minimum, I have no idea what actually breaks (if anything) under R 3.3.3, so use at your own risk.

First, add the following to your ~/.R/Makevars (via mkdir ~/.R && nano ~/.R/Makevars):

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -Wno-macro-redefined
CXXFLAGS+=-flto -Wno-unused-local-typedefs
CXXFLAGS+=-Wno-ignored-attributes -Wno-deprecated-declarations
CXX14=g++
CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -fPIC
CXX14FLAGS+=-flto -Wno-unused-local-typedefs -Wno-ignored-attributes -Wno-deprecated-declarations

And add the following to your ~/.Rprofile: Sys.setenv(MAKEFLAGS = "-j4"), which speeds up (parallelizes) the process of compiling C++ code in R packages. Now you'll need to install the dependencies step-by-step:

install.packages(c("Rcpp", "BH", "RcppArmadillo", "RcppEigen")) # required for StanHeaders
install.packages(c("bayesplot", "lme4", "loo")) # required for rstan
install.packages("/home/bearloga/StanHeaders_2.18.1-modified.tar.gz", repos = NULL)
install.packages("/home/bearloga/rstan_2.18.2-modified.tar.gz", repos = NULL)

# Installing RStanArm, a high-level interface to RStan:
install.packages(c("rstantools", "shinystan")) # required for rstanarm and brms
install.packages("/home/bearloga/rstanarm_2.18.2-modified.tar.gz", repos = NULL)

# Installing brms, also a high-level interface to RStan:
# Note: brms requires bridgesampling which requires mvtnorm which requires R (>= 3.5.0)
install.packages("/home/bearloga/mvtnorm_1.0.10-modified.tar.gz", repos = NULL)
install.packages("brms")

Use the 8 schools example to verify that RStan works and use this vignette to verify that RStanArm works.

Installing brms from CRAN works fine after RStan ≥2.17.2 and bridgesampling are installed. Check that it works by running the simple distributional model example from the vignette. Note: brms requires bridgesampling which requires mvtnorm which requires R ≥3.5.0 (on CRAN), so that's another package we have to cheat a little with and another one where using an older version of R than the one the package requires may result in problems.

Modifying[edit]

Here are the steps I took to modify the package sources from CRAN for installing manually under our old version of R:

# Step 0:
cd ~/Downloads
mkdir rstan\ sources && cd rstan\ sources
mkdir modified && mkdir originals && cd originals

# 1. Download the package sources:
curl https://cran.r-project.org/src/contrib/rstan_2.18.2.tar.gz --output rstan_2.18.2.tar.gz
curl https://cran.r-project.org/src/contrib/StanHeaders_2.18.1.tar.gz --output StanHeaders_2.18.1.tar.gz
curl https://cran.r-project.org/src/contrib/rstanarm_2.18.2.tar.gz --output rstanarm_2.18.2.tar.gz
curl https://cran.r-project.org/src/contrib/mvtnorm_1.0-10.tar.gz --output mvtnorm_1.0.10.tar.gz

# 2. Unpack into ../modified:
for f in *.tar.gz; do tar -xvf $f; done
# then: $> mv rstan ../modified/

# 3. Modify the DESCRIPTION file of each package to require R (>= 3.3.0) instead of R (>= 3.4.0)

# 4. Repack:
tar -czvf StanHeaders_2.18.1-modified.tar.gz rstanarm
tar -czvf rstan_2.18.2-modified.tar.gz rstan
tar -czvf rstanarm_2.18.2-modified.tar.gz rstanarm
tar -czvf mvtnorm_1.0.10-modified.tar.gz mvtnorm

References[edit]