Local software installation instructions for Bayes Workshop 2023

I recommend using Posit Cloud for this workshop, a demo server with all needed packages, software, and data installed (signup instructions here), especially if you have never used R before. If you decide not to use Posit Cloud for the workshop, but instead you want to install the software on your own machine, please follow the steps below. Note that after the workshop, if you want to analyze your own data, you’ll have to do this anyway or use Scinet. The Posit Cloud server is only intended for demonstration and teaching purposes. It is not a secure server, and government-owned data may not be uploaded to it.

NOTE: Some USDA employees have experienced issues getting this “software stack” working on their USDA Windows machines. Please contact me if you experience any problems, and copy and paste whatever error messages you get into the email.

My preferred setup for Bayesian models in R is to use the brms modeling package. brms is an R interface that allows you to write multilevel models with simple code that has similar syntax to the common mixed-model package lme4. “Behind the scenes” it fits a Bayesian model with the software Stan that has a state-of-the-art algorithm coded in C++ for quickly and efficiently sampling posterior distributions. There are two different packages that integrate between Stan and R, and you can specify which one to use in brms. While the default is rstan, the best option which makes the models run the fastest is CmdStanR. So in order to get brms working in R, you need to not only install brms but also have Stan installed on your system, and optionally CmdStanR. Then you have to set up everything so that all those pieces of software can communicate with each other.

I want to give props to Paul Bürkner, the developer of brms, and all the developers of Stan including Andrew Gelman, Bob Carpenter, and lots of other people. Their hard work makes our life easier and our stats better!

You’ll need to install the following software on your local machine.

Follow these steps to install R, RStudio, the needed R packages, and Stan.

NOTE: You will need to be connected to the Internet to install this software. I recommend disconnecting from any VPN before following these steps.

Step 1. Install R

You will need to download R from The Comprehensive R Archive Network, or CRAN. If you are using an older version of R, please update to version 4.3 or higher.

CRAN R download screenshot

Go to the CRAN website and select the link to download the most recent version of R for your operating system. Follow the installation instructions. You will need admin rights to do this so you may need to get help from your IT staff.

Step 2. Install RStudio

RStudio is an additional software program that makes it easy for you to write R code. It provides an interface that lets you run code, edit scripts, see what variables are in your working environment, and manage files. RStudio is a program that makes it easier to use R, but you need to download both R and RStudio separately.

Posit RStudio download screenshot

RStudio is now distributed by a company called Posit. Go to the RStudio Desktop download page and download the free version of RStudio Desktop. Again, you will need admin rights to do this on your USDA machine.

Step 3. Install R packages

Run the following code in your R or RStudio console (copy and paste). This installs all R packages needed for the workshop. Most are installed from the R package repository, CRAN. The brms package is installed from GitHub, and the cmdstanr package is installed from its own repository.

install.packages(c('devtools', 'tidyverse', 'emmeans', 'tidybayes', 'ggdist', 'easystats', 'logspline', 'lme4'))
devtools::install_github('paul-buerkner/brms')
install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

If you don’t want to bother with CmdStan, skip down to the Testing the installation section below to test whether brms and Stan were installed correctly. But if you want to set up CmdStan as well, read on. This is a summary of the installation instructions on the CmdStanR homepage.

Step 4. Configure C++

Because the Stan models will be compiled in C++, you need to make sure your system is configured to compile C++ programs. Run the following code in your R console.

cmdstanr::check_cmdstan_toolchain(fix = TRUE)

You may get a message that Rtools is not installed on your computer. If you don’t have Rtools installed, you will need to install it so that the models can compile. To install Rtools, quit R or RStudio, go to the [Rtools installation page on CRAN](https://cran.r-project.org/bin/windows/Rtools/rtools43/rtools.html, and download the Rtools43 installer using the link (it’s a very large file, several hundred MB). Run the installer, and restart R or RStudio when the installation is complete. Run cmdstanr::check_cmdstan_toolchain(fix = TRUE) again to make sure everything is good to go.

Step 5. Install Stan

Once you have confirmed that the CmdStan toolchain is configured correctly, you can install Stan (more specifically, a version of it called CmdStan). Run the following code in your R console.

cmdstanr::install_cmdstan()

Step 6. Test the installation

Run the following code in your R console. If you decided to skip installing CmdStan, omit the options(brms.backend = 'cmdstanr') line.

library(brms)
options(brms.backend = 'cmdstanr')
brm(mpg ~ hp, data = mtcars)

correct output of mtcars model

The above code loads brms and tells it to fit a simple model on a built-in example dataset. If it gives you a message that it is Compiling Stan model..., then progress indicators for the sampler, and then model summary output, as in the screenshot shown here, it means everything is working!

NOTE: On certain systems you may see some messages pop up from the C++ compiler. As long as the model runs, you don’t need to worry about them.

Step 7. Download the worksheet

Go back to the workshop homepage and download the worksheet. Open it in R or RStudio to follow along with the code during the workshop. Good luck!