Installing Stan and RStan

These instructions are from: https://github.com/rmcelreath/rethinking

Go to http://mc-stan.org and follow the instructions for your platform. The biggest challenge is getting a C++ compiler configured to work with your installation of R. The instructions at the RStan getting started website are quite thorough.

Installing Stan can be a bit complicated on some machines, especially if you haven’t run programs that require a C++ compiler before (if that sentence doesn’t make sense to you, this means you), so please don’t wait until the morning of class to install Stan. Try to install it a few days beforehand, and if you can’t get it to work let me know and I’ll try to help.

To check that it works, once you’ve successfully done all the other steps please open R and run the code below. If it works, you’re good to go!

library(rstan)
stanmodelcode <- "
data {
  int<lower=0> N;
  real y[N];
} 

parameters {
  real mu;
} 

model {
  target += normal_lpdf(mu | 0, 10);
  target += normal_lpdf(y  | mu, 1);
} 
"

y <- rnorm(20) 
dat <- list(N = 20, y = y); 
fit <- stan(model_code = stanmodelcode, model_name = "example", 
            data = dat, iter = 2012, chains = 3, sample_file = 'norm.csv',
            verbose = TRUE) 
print(fit)
traceplot(fit)

Installing rethinking

Then you can install rethinking from within R using:

install.packages(c("coda","mvtnorm","devtools","loo","dagitty"))
library(devtools)
devtools::install_github("rmcelreath/rethinking")

More details on rethinking and other packages by Richard McElreath:
https://xcelab.net/rm/software/