The main required software for the course is R. It is the core computational environment we will be using.
In addition to R, you will need several R add-on packages:
rethinking
: This is the companion
package to Richard McElreath’s Statistical Rethinking textbook.
This package is great for learning about probability modeling and
distributions in general, so we’ll be using it in the first part of the
course. Later on we’ll switch to ‘workhorse’ packages like
brms
and lme4
that are much better fit for
general research.brms
: This is a general-purpose
package for estimating linear models with MCMC. brms
is
extremely flexible–it can estimate a wide range of models including
multilevel models, all of the generalized linear models we’ll cover in
the class, among many others.lme4
: This is the standard multilevel
/ mixed-effects estimation package for R. While it doesn’t cover the
same breadth of models or tools as brms
, it is extremely
efficient at estimating the models it does cover.RStan
This package provides a way to
run Stan code from R. Stan is “a
state-of-the-art platform for statistical modeling and high-performance
statistical computation.” Stan requires a working toolchain for
compiling C++ code, which we’ll cover how to install belowFinally, you likely want to install an integrated development environment (IDE). An IDE is a program that will run R, help you edit scripts, and provides interfaces for visualizations, installing packages, and other common needs. While not technically required, an IDE can make life a lot easier, especially if you don’t consider yourself a computer nerd. The most popular IDE for R is RStudio, but VSCode (and VSCodium, it’s open-source version with Microsoft’s tracking features removed) is also becoming more widely used. I’ll teach the class using RStudio because that is the most common among students.
r-base
using your package manager.Installing RStudio is optional, but recommended for most students.
This is the most complex component to install, and the process depends entirely on what OS you run.
Complete the following steps (consolidated from Rstan) in an R console (in RStudio, another IDE, or in a terminal window):
Install the remotes
package:
install.packages("remotes")
Install macrtools
package:
::install_github("coatless-mac/macrtools") remotes
Run the macrtools
toolchain installer:
::macos_rtools_install() macrtools
Running the following is not required, but will help your Stan code run faster:
<- file.path(Sys.getenv("HOME"), ".R")
dotR if (!file.exists(dotR)) dir.create(dotR)
<- file.path(dotR, "Makevars")
M if (!file.exists(M)) file.create(M)
<- ifelse(R.version$arch == "aarch64", "arm64", "x86_64")
arch cat(paste("\nCXX17FLAGS += -O3 -mtune=native -arch", arch, "-ftemplate-depth-256"),
file = M, sep = "\n", append = FALSE)
Install the RStan
package:
install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)
Make sure RStan
works by running an example (this
will take a few seconds to compile and run):
example(stan_model, package = "rstan", run.dontrun = TRUE)
Instructions consolidated from RStan:
Ensure you have the latest version of R installed (upgrade if necessary).
Visit the Rtools website (https://cran.r-project.org/bin/windows/Rtools/rtools43/rtools.html).
Download and run the RTools43 installer linked from that page.
Running the following in an R console is not required, but will help your Stan code run faster:
<- file.path(Sys.getenv("HOME"), ".R")
dotR if (!file.exists(dotR)) dir.create(dotR)
<- file.path(dotR, "Makevars.win")
M if (!file.exists(M)) file.create(M)
cat("\n CXX17FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
file = M, sep = "\n", append = FALSE)
Install the RStan
package:
install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)
Make sure RStan
works by running an example (this
will take a few seconds to compile and run):
example(stan_model, package = "rstan", run.dontrun = TRUE)
If you use Linux, you almost certainly already have a C++ toolchain installed.
Running the following in an R console is not required, but will help your Stan code run faster:
<- file.path(Sys.getenv("HOME"), ".R")
dotR if (!file.exists(dotR)) dir.create(dotR)
<- file.path(dotR, "Makevars")
M if (!file.exists(M)) file.create(M)
cat("\nCXX17FLAGS=-O3 -march=native -mtune=native -fPIC",
"CXX17=g++", # or clang++ but you may need a version postfix
file = M, sep = "\n", append = TRUE)
Install the RStan
package:
Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1)
install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)
Make sure RStan
works by running an example (this
will take a few seconds to compile and run):
example(stan_model, package = "rstan", run.dontrun = TRUE)
The remaining required packages should be easy to install from within an R console:
Install cmdstanR
and rethinking
:
install.packages("cmdstanr", repos = c('https://stan-dev.r-universe.dev', getOption("repos")))
# possibly:
install_cmdstan()
install.packages(c("coda","mvtnorm","loo","dagitty","shape"))
::install_github("rmcelreath/rethinking") devtools
Install brms
and lme4
:
install.packages(c("brms","lme4"))