Setup instructions
1 Installing R
The latest version of an R installation binary (or source code) can be downloaded from one of the Comprehensive R Archive Network (or CRAN) mirrors. Having selected one of the (Australian) mirrors, follow one of the sets of instructions below (depending on your operating system).
Download R:
- Go to the CRAN R-project website https://cran.r-project.org/ and click on “Download R for Windows”.
- Select the “base” subdirectory
- Select the “Download R-X.X.X for Windows” option (where X.X.X are a series of version and release numbers) to download.
Run the installer: Double-click the downloaded .exe file and follow the installation wizard. Accept the default settings unless you have specific needs.
Optional: Set R as the default: Check the checkbox to set R as the default for R scripts during installation. This allows you to run R scripts by double-clicking them.
Verify installation:
- Open a new command prompt (Start > Run > cmd) and type
R
. If the R console opens, the installation was successful. - Alternatively, search for R in the Start menu
- Open a new command prompt (Start > Run > cmd) and type
Download R:
- Go to the CRAN R-project website (https://cran.r-project.org/) and click on “Download R for macOS”.
- Choose the latest stable version that is appropriate for your architecture.
Open the disk image: Double-click the downloaded .pkg file and drag the R application icon to your Applications folder.
Verify installation:
- Open Terminal: Go to Applications > Utilities and open Terminal.
- Type
R
in the Terminal window. If the R console opens, the installation was successful.
Open Terminal: You can access Terminal through your application launcher or search bar.
Install R: The commands vary slightly depending on your Linux distribution. Here are common examples:
- Debian/Ubuntu:
sudo apt install r-base
- Fedora/CentOS:
sudo yum install R
- Arch Linux:
sudo pacman -S R
- Debian/Ubuntu:
Verify installation: Type
R
in the Terminal window. If the R console opens, the installation was successful.
2 Installing Rstudio
- Download R:
- RStudio requires R to be installed. If you have not already done so, download and install R from the official CRAN website.
- Download RStudio:
- Visit the RStudio Download page and select the “RStudio Desktop” version compatible with your Windows operating system.
- Install RStudio:
- Run the downloaded RStudio installer and follow the installation wizard.
- Accept the default settings unless you have specific preferences.
- Launch RStudio:
- After installation, launch RStudio from the Start menu or desktop shortcut.
- Download R:
- If you have not already done so, download and install R on macOS from the official CRAN website.
- Download RStudio:
- Navigate to the RStudio Download page and choose the “RStudio Desktop” version for macOS.
- Install RStudio:
- Run the downloaded RStudio package, and macOS will guide you through the installation process.
- Launch RStudio:
- Open RStudio from the Applications folder or use Spotlight to search for it.
- Download R:
- If you have not already done so, install R on your Linux distribution using the package manager. For example, on Ubuntu, run:
- Download RStudio:
- Visit the RStudio Download page and choose the appropriate RStudio Desktop version for your Linux distribution.
- Install RStudio:
- Run the downloaded RStudio package, and follow any additional instructions based on your Linux distribution.
- Launch RStudio:
- Open a terminal and type rstudio to launch RStudio.
3 Installating git
Git Bash (Command Line Version):
- Download the Git for Windows installer from Git for Windows
- Click the Download button
- Select the latest version from the list of
Assets
- Run the installer and follow the installation prompts.
- Choose the default options unless you have specific preferences.
- Select the default text editor (usually Vim) or choose another editor like Nano or Notepad++.
- Choose to use Git from the Windows Command Prompt (recommended).
- Complete the installation.
Using Homebrew:
- Open Terminal.
- Install Homebrew if not installed:
- Install Git using Homebrew:
To verify that the software is installed and accessible, open a terminal and issue the following:
Windows:
On Windows, you can access a terminal via one of the following:
- via the command Prompt:
- Press
Win + R
to open the Run dialog. - Type
cmd
and pressEnter
.
- Press
- via PowerShell:
- Press
Win + X
and select “Windows PowerShell.”
- Press
- Git Bash (Optional):
- if Git is installed (which we are hoping it is!), open “Git Bash” for a Unix-like terminal experience.
MacOS:
- via Terminal:
- Press
Cmd + Space
to open Spotlight. - Type
terminal
and pressEnter
.
- Press
Linux:
Oh please. You cannot seriously tell me that you are using Linux and don’t know how to access a terminal.
In the command above, pay particular attention to the number of hyphens in the above command - there are two in a row and no spaces between the --
and the word version
.
If you get output similar to above (an indication of what version of git you have on your system), then it is likely to be properly installed. If instead you get an error message, then it is likely that git is not properly installed and you should try again.
4 Setup a free github account
To create a free github account:
- visit https://github.com and click “Sign up for github”
- register by providing your prefered email address, a username and a password when prompted
- to complete the account activation, you will need to verify your details via an email sent to your nominated email address
As of the start of 2024, github now requires Two-Factor Authentication (2FA) for enhanced security. Whenever you login to github (or are prompted for a password, you will also need to use 2FA. To setup 2FA:
- click on your profile picture in the top right corner.
- select “Settings” from the dropdown menu.
- select “Password and authentication” in the left sidebar.
- under “Two-factor authentication” section, click “Enable”.
- choose your preferred method (authenticator app or SMS) and follow the prompts to set it up.
Passwords and Two-Factor Authentication (2FA) are used when you (as a human) securely login and interact directly with the GitHub website. However, it is also possible to have other tools (such as git
) interact with Github on your behalf via an Application Programming Interfacet (API). Passwords/2FA are not appropriate to authenticate these machine to machine communications. Instead, Github requires the use of a Personal Access Token (PAT). PATs offer a more secure and granular approach, allowing users to control access without exposing their account password.
To generate a Personal Access Token (PAT):
click on your profile picture in the top right corner.
select “Settings” from the dropdown menu.
select “Developer settings” from the bottom of the left sidebar.
select “Personal access tokens” from the left sidebar.
select “Tokens (classic)” from the dropdown menu
click “Generate new token”
select “Generate new token (classic)” from the dropdown menu
at this point you will likely be prompted for your password
provide a “note” - this is more of a short description of what the token is to be used for (in the example below, I have entered “git push/pull” to remind me that this is a simple token for regular push/pull interaction between my local and remote repositories).
You also need to provide an expiration. Although not secure or recommended, I have selected “No expiration” as I don’t want to have to re-do my PAT across multiple machines too regularly.
Finally, you also need to indicate scope (what activities you are granting permission for the tools to be able to perform). In this case, I have ticked the “repo” box. This grants general rea/write access to my repositories. I have not granted permission for more administration like activities such as managing teams, deleting repositories, etc - these activities I am happy to perform myself via the website.
click “Generate token” and securely copy the generated token. Until this is stored safely (see below) do not close the page, because Github will never show you this PAT again.
Important: Store your PAT safely as you won’t be able to see it again! Ideally, you should store this PAT in a digital wallet. Digital wallets vary according to operating systems. R users might like to use the r
function from the asdf
package (which you will need to install prior) as follows in order to store the PAT.
In an R console, enter:
When propted for a password, paste in the copied PAT that hopefully is still in your clipboard - else you might need to re-copy it.
To confirm that you have successfully stored your PAT in your wallet, you can:
and confirm that it indicates that there is a hidden password.
5 Installing stan (for Bayesian modelling)
Install and setup Rtools (a collection of R focused build tools for windows)
- go to CRAN Rtools website https://cran.r-project.org/bin/windows/Rtools/
- click on the Rtools version that matches the major version of R you are using.
- click on the installer link (midway down the page) to download the installer
- run the installer
- follow all defaults during the installation process
Install cmdstan (an implementation of the STAN language)
- using selected instructions from https://mc-stan.org/cmdstanr/articles/cmdstanr.html
open a new R session and issue the following
make sure the package loads
ensure that the c++ toolchain (from Rtools) is correctly installed and configured
if the toolchain is correctly configured, install cmdstan
- using selected instructions from https://mc-stan.org/cmdstanr/articles/cmdstanr.html
Ensure that cmdstan is properly configured by compiling a built in example
file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan") mod <- cmdstan_model(file) data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) fit <- mod$sample( data = data_list, seed = 123, chains = 4, parallel_chains = 4, refresh = 500 # print update every 500 iters )
Running MCMC with 4 parallel chains... Chain 1 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 1 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 1 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 1 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 1 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 1 Iteration: 2000 / 2000 [100%] (Sampling) Chain 2 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 2 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 2 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 2 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 2 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 2 Iteration: 2000 / 2000 [100%] (Sampling) Chain 3 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 3 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 3 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 3 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 3 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 3 Iteration: 2000 / 2000 [100%] (Sampling) Chain 4 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 4 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 4 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 4 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 4 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 4 Iteration: 2000 / 2000 [100%] (Sampling) Chain 1 finished in 0.0 seconds. Chain 2 finished in 0.0 seconds. Chain 3 finished in 0.0 seconds. Chain 4 finished in 0.0 seconds. All 4 chains finished successfully. Mean chain execution time: 0.0 seconds. Total execution time: 0.3 seconds.
If you get output resembling the above, then cmdstan is setup correctly.
Install the brms - an R package that provides a more familiar R model fitting interface to
STAN
.install the package
test whether the whole tool chain works
Loading required package: StanHeaders
rstan version 2.32.2 (Stan version 2.32.2)
For execution on a local, multicore CPU with excess RAM we recommend calling options(mc.cores = parallel::detectCores()). To avoid recompilation of unchanged Stan programs, we recommend calling rstan_options(auto_write = TRUE) For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions, change `threads_per_chain` option: rstan_options(threads_per_chain = 1)
Loading required package: Rcpp
Loading 'brms' package (version 2.21.0). Useful instructions can be found by typing help('brms'). A more detailed introduction to the package is available through vignette('brms_overview').
Attaching package: 'brms'
The following object is masked from 'package:rstan': loo
The following object is masked from 'package:stats': ar
Compiling Stan program...
Start sampling
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1). Chain 1: Chain 1: Gradient evaluation took 7e-06 seconds Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.07 seconds. Chain 1: Adjust your expectations accordingly! Chain 1: Chain 1: Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 1: Iteration: 2000 / 2000 [100%] (Sampling) Chain 1: Chain 1: Elapsed Time: 0.013 seconds (Warm-up) Chain 1: 0.012 seconds (Sampling) Chain 1: 0.025 seconds (Total) Chain 1: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2). Chain 2: Chain 2: Gradient evaluation took 3e-06 seconds Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 2: Adjust your expectations accordingly! Chain 2: Chain 2: Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 2: Iteration: 2000 / 2000 [100%] (Sampling) Chain 2: Chain 2: Elapsed Time: 0.012 seconds (Warm-up) Chain 2: 0.012 seconds (Sampling) Chain 2: 0.024 seconds (Total) Chain 2: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 3). Chain 3: Chain 3: Gradient evaluation took 3e-06 seconds Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 3: Adjust your expectations accordingly! Chain 3: Chain 3: Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 3: Iteration: 2000 / 2000 [100%] (Sampling) Chain 3: Chain 3: Elapsed Time: 0.012 seconds (Warm-up) Chain 3: 0.013 seconds (Sampling) Chain 3: 0.025 seconds (Total) Chain 3: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 4). Chain 4: Chain 4: Gradient evaluation took 3e-06 seconds Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 4: Adjust your expectations accordingly! Chain 4: Chain 4: Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 4: Iteration: 2000 / 2000 [100%] (Sampling) Chain 4: Chain 4: Elapsed Time: 0.013 seconds (Warm-up) Chain 4: 0.013 seconds (Sampling) Chain 4: 0.026 seconds (Total) Chain 4:
Family: gaussian Links: mu = identity; sigma = identity Formula: y ~ x Data: dat (Number of observations: 10) Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1; total post-warmup draws = 4000 Regression Coefficients: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS Intercept -0.50 0.38 -1.29 0.24 1.00 2925 2438 x 0.06 0.34 -0.65 0.74 1.00 2903 2197 Further Distributional Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 1.10 0.33 0.64 1.91 1.00 2023 2445 Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS and Tail_ESS are effective sample size measures, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat = 1).
Again, if you get output similar to that above, then the complete Bayesian toolchain is correctly configured and ready for use.
Install and setup Xcode (a collection of build tools for MacOSX)
- open a terminal and enter
Install cmdstan (an implementation of the STAN language)
- using selected instructions from https://mc-stan.org/cmdstanr/articles/cmdstanr.html
open a new R session and issue the following
make sure the package loads
ensure that the c++ toolchain (from Rtools) is correctly installed and configured
if the toolchain is correctly configured, install cmdstan
- using selected instructions from https://mc-stan.org/cmdstanr/articles/cmdstanr.html
Ensure that cmdstan is properly configured by compiling a built in example
file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan") mod <- cmdstan_model(file) data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) fit <- mod$sample( data = data_list, seed = 123, chains = 4, parallel_chains = 4, refresh = 500 # print update every 500 iters )
Running MCMC with 4 parallel chains... Chain 1 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 1 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 1 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 1 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 1 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 1 Iteration: 2000 / 2000 [100%] (Sampling) Chain 2 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 2 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 2 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 2 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 2 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 2 Iteration: 2000 / 2000 [100%] (Sampling) Chain 3 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 3 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 3 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 3 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 3 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 3 Iteration: 2000 / 2000 [100%] (Sampling) Chain 4 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 4 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 4 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 4 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 4 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 4 Iteration: 2000 / 2000 [100%] (Sampling) Chain 1 finished in 0.0 seconds. Chain 2 finished in 0.0 seconds. Chain 3 finished in 0.0 seconds. Chain 4 finished in 0.0 seconds. All 4 chains finished successfully. Mean chain execution time: 0.0 seconds. Total execution time: 0.3 seconds.
If you get output resembling the above, then cmdstan is setup correctly.
Install the brms - an R package that provides a more familiar R model fitting interface to
STAN
.install the package
test whether the whole tool chain works
library(rstan) library(brms) dat <- data.frame(y = rnorm(10), x = rnorm(10)) brm(y ~ x, data = dat, backend = "rstan")
Compiling Stan program...
Start sampling
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1). Chain 1: Chain 1: Gradient evaluation took 7e-06 seconds Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.07 seconds. Chain 1: Adjust your expectations accordingly! Chain 1: Chain 1: Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 1: Iteration: 2000 / 2000 [100%] (Sampling) Chain 1: Chain 1: Elapsed Time: 0.013 seconds (Warm-up) Chain 1: 0.019 seconds (Sampling) Chain 1: 0.032 seconds (Total) Chain 1: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2). Chain 2: Chain 2: Gradient evaluation took 3e-06 seconds Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 2: Adjust your expectations accordingly! Chain 2: Chain 2: Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 2: Iteration: 2000 / 2000 [100%] (Sampling) Chain 2: Chain 2: Elapsed Time: 0.013 seconds (Warm-up) Chain 2: 0.013 seconds (Sampling) Chain 2: 0.026 seconds (Total) Chain 2: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 3). Chain 3: Chain 3: Gradient evaluation took 3e-06 seconds Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 3: Adjust your expectations accordingly! Chain 3: Chain 3: Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 3: Iteration: 2000 / 2000 [100%] (Sampling) Chain 3: Chain 3: Elapsed Time: 0.013 seconds (Warm-up) Chain 3: 0.013 seconds (Sampling) Chain 3: 0.026 seconds (Total) Chain 3: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 4). Chain 4: Chain 4: Gradient evaluation took 3e-06 seconds Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 4: Adjust your expectations accordingly! Chain 4: Chain 4: Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 4: Iteration: 2000 / 2000 [100%] (Sampling) Chain 4: Chain 4: Elapsed Time: 0.012 seconds (Warm-up) Chain 4: 0.01 seconds (Sampling) Chain 4: 0.022 seconds (Total) Chain 4:
Family: gaussian Links: mu = identity; sigma = identity Formula: y ~ x Data: dat (Number of observations: 10) Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1; total post-warmup draws = 4000 Regression Coefficients: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS Intercept -0.49 0.37 -1.21 0.24 1.00 2905 2071 x 0.05 0.32 -0.61 0.69 1.00 2827 2185 Further Distributional Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 1.08 0.32 0.65 1.88 1.00 2148 2048 Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS and Tail_ESS are effective sample size measures, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat = 1).
Again, if you get output similar to that above, then the complete Bayesian toolchain is correctly configured and ready for use.
Ensure that you have installed (via your package manager) the following dependencies:
- build-essential
- g++
- gcc
- curl
- libcurl4-openssl-dev
Install cmdstan (an implementation of the STAN language)
- using selected instructions from https://mc-stan.org/cmdstanr/articles/cmdstanr.html
open a new R session and issue the following
make sure the package loads
ensure that the c++ toolchain (from Rtools) is correctly installed and configured
if the toolchain is correctly configured, install cmdstan
- using selected instructions from https://mc-stan.org/cmdstanr/articles/cmdstanr.html
Ensure that cmdstan is properly configured by compiling a built in example
file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan") mod <- cmdstan_model(file) data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) fit <- mod$sample( data = data_list, seed = 123, chains = 4, parallel_chains = 4, refresh = 500 # print update every 500 iters )
Running MCMC with 4 parallel chains... Chain 1 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 1 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 1 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 1 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 1 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 1 Iteration: 2000 / 2000 [100%] (Sampling) Chain 2 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 2 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 2 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 2 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 2 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 2 Iteration: 2000 / 2000 [100%] (Sampling) Chain 3 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 3 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 3 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 3 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 3 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 3 Iteration: 2000 / 2000 [100%] (Sampling) Chain 4 Iteration: 1 / 2000 [ 0%] (Warmup) Chain 4 Iteration: 500 / 2000 [ 25%] (Warmup) Chain 4 Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 4 Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 4 Iteration: 1500 / 2000 [ 75%] (Sampling) Chain 4 Iteration: 2000 / 2000 [100%] (Sampling) Chain 1 finished in 0.0 seconds. Chain 2 finished in 0.0 seconds. Chain 3 finished in 0.0 seconds. Chain 4 finished in 0.0 seconds. All 4 chains finished successfully. Mean chain execution time: 0.0 seconds. Total execution time: 0.3 seconds.
If you get output resembling the above, then cmdstan is setup correctly.
Install the brms - an R package that provides a more familiar R model fitting interface to
STAN
.install the package
test whether the whole tool chain works
library(rstan) library(brms) dat <- data.frame(y = rnorm(10), x = rnorm(10)) brm(y ~ x, data = dat, backend = "rstan")
Compiling Stan program...
Start sampling
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1). Chain 1: Chain 1: Gradient evaluation took 7e-06 seconds Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.07 seconds. Chain 1: Adjust your expectations accordingly! Chain 1: Chain 1: Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 1: Iteration: 2000 / 2000 [100%] (Sampling) Chain 1: Chain 1: Elapsed Time: 0.013 seconds (Warm-up) Chain 1: 0.019 seconds (Sampling) Chain 1: 0.032 seconds (Total) Chain 1: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 2). Chain 2: Chain 2: Gradient evaluation took 3e-06 seconds Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 2: Adjust your expectations accordingly! Chain 2: Chain 2: Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 2: Iteration: 2000 / 2000 [100%] (Sampling) Chain 2: Chain 2: Elapsed Time: 0.013 seconds (Warm-up) Chain 2: 0.013 seconds (Sampling) Chain 2: 0.026 seconds (Total) Chain 2: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 3). Chain 3: Chain 3: Gradient evaluation took 3e-06 seconds Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 3: Adjust your expectations accordingly! Chain 3: Chain 3: Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 3: Iteration: 2000 / 2000 [100%] (Sampling) Chain 3: Chain 3: Elapsed Time: 0.013 seconds (Warm-up) Chain 3: 0.013 seconds (Sampling) Chain 3: 0.026 seconds (Total) Chain 3: SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 4). Chain 4: Chain 4: Gradient evaluation took 3e-06 seconds Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.03 seconds. Chain 4: Adjust your expectations accordingly! Chain 4: Chain 4: Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup) Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup) Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup) Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup) Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup) Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup) Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling) Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling) Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling) Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling) Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling) Chain 4: Iteration: 2000 / 2000 [100%] (Sampling) Chain 4: Chain 4: Elapsed Time: 0.012 seconds (Warm-up) Chain 4: 0.01 seconds (Sampling) Chain 4: 0.022 seconds (Total) Chain 4:
Family: gaussian Links: mu = identity; sigma = identity Formula: y ~ x Data: dat (Number of observations: 10) Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1; total post-warmup draws = 4000 Regression Coefficients: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS Intercept -0.49 0.37 -1.21 0.24 1.00 2905 2071 x 0.05 0.32 -0.61 0.69 1.00 2827 2185 Further Distributional Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 1.08 0.32 0.65 1.88 1.00 2148 2048 Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS and Tail_ESS are effective sample size measures, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat = 1).
Again, if you get output similar to that above, then the complete Bayesian toolchain is correctly configured and ready for use.