Supercharge your Statistics with JAGS and Stan on RONIN
At RONIN, we know that many of our researchers are moving beyond frequentist statistics and embracing the power of Bayesian modeling. Tools like JAGS (Just Another Gibbs Sampler) and Stan are essential for this kind of work.
However, installing these tools on a Linux server can sometimes feel like a dark art. They require external C++ engines, specific system libraries, and interfaces that need to link everything together perfectly. If you try to install these purely inside RStudio on a modest virtual machine, you might find yourself waiting 30 minutes for compilation, only for it to crash due to memory limits.
We’re going to show you the robust, "RONIN way" to get this stack running in under 5 minutes.
Step 1: The Foundation (Crucial Step)
First, you should install RStudio with RONIN LINK, as it will set up the correct repositories for you.
- First create an Ubuntu 22.04 machine in RONIN
- Next open RONIN LINK on your desktop and connect to your new machine
- Next, click Connect To Machine > LINK (RSTUDIO)
While you might be tempted to just install R directly, the RONIN LINK installation script does something vital in the background: it configures the c2d4u (CRAN to Debian for Ubuntu) repository.
Why does this matter?
Normally, when you install a complex package like rstan inside R, R has to download the source code and compile it on your machine. This is slow and resource-intensive. The c2d4u repository provides over 4,000 pre-compiled CRAN packages as standard Ubuntu binaries. This means we can install them instantly using apt, guaranteed stable.
Assuming you have run the RONIN LINK RStudio setup script on your fresh Ubuntu 22.04 machine, you are ready for Step 2.
Step 2: The Terminal is Your Friend
For this installation, we are going to step away from RStudio for a moment and use your machine's SSH terminal. We want to install these tools at the system level to ensure the underlying engines are present before R tries to talk to them.
Log into your machine via SSH and run the following commands.
2a. Update your lists
Always start by ensuring your package manager knows about the latest software versions:
sudo apt update
2b. Install JAGS
JAGS requires two parts: the standalone engine executable, and the R package that interfaces with it. Because we set up the repos correctly in Step 1, we can install both seamlessly:
Bash
# 'jags' is the engine
# 'r-cran-rjags' is the pre-compiled R interface
sudo apt -y install jags r-cran-rjags
Installing via apt avoids common issues where R can't locate the shared JAGS library files.
2c. Install Stan
Stan is a heavy lifter. It translates your models into C++ and compiles them. To do this smoothly, it needs a few system dependencies (like V8 for Javascript and CURL for data transfer).
Again, because we are using the pre-compiled binaries via RONIN's setup, we skip the long compilation times:
Bash
# Install necessary system dependencies and the R interface
sudo apt -y install libv8-dev libcurl4-openssl-dev libssl-dev r-cran-rstan
Step 3: Verification
Now, let's log into your RStudio Server by running the RStudio Link button again and run a "Hello World" script to ensure both JAGS and Stan can compile a basic model and run chains in parallel.
Open a new R Script in RStudio and run this code block:
# Test JAGS
library(rjags)
jags_test <- jags.model(textConnection("model { b ~ dnorm(0,1) }"), data = list(), n.chains=2)
update(jags_test, 100)
print("JAGS is working!")
# Test Stan
library(rstan)
# This allows Stan to run in parallel and save compiled models
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
stan_code <- "
parameters { real y; }
model { y ~ normal(0,1); }
"
fit <- stan(model_code = stan_code, iter = 200)
print(fit)
print("Stan is working!")If that script runs to completion and prints the SUCCESS messages, your RONIN machine is ready for your research. Happy modeling!
