BwUniCluster3.0/Software/R/Rjags

From bwHPC Wiki
Jump to navigation Jump to search

General information

rjags is an R interface to use JAGS, Just another Gibbs Sampler, a program for analysis of Bayesian hierarchical models using Markov Chain Monte Carlo (MCMC) simulation

Installation instructions

rjags needs a working JAGS installation. The following instructions will build and install JAGS 4.3.2 to $JAGS_HOME on bwUniCluster 3.0.

First, in order to compile them on a compute node, we obtain an interactive session:

# Obtain interactive batch session
salloc -p dev_cpu -N1 --ntasks-per-node 4 -t 30 --mem=4096

Install external dependencies

Then we download and compile JAGS using GCC 11.4:

# Load required modules.
module purge
module load math/R

# Set up JAGS installation directory.
export JAGS_HOME="$HOME/sw/jags"

# Set up source directory.
mkdir -p ~/src
cd ~/src

# Download JAGS source.
wget https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Source/JAGS-4.3.2.tar.gz
tar -xf JAGS-4.3.2.tar.gz
rm JAGS-4.3.2.tar.gz
cd JAGS-4.3.2

# Compile JAGS.
export CFLAGS="-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration"
export CXXFLAGS="-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -std=c++23 -Wno-implicit-function-declaration"

./configure  --prefix="$JAGS_HOME" --with-blas="-lopenblas"
make -j 4
make install

Install rjags in R

Preparations

Prepare an .R/Makevars file (if it does not already exist). This file specifies how R should compile the packages (i.e., sets some 'compiler flags').

If an .R/Makevars file is present in your home directory ($HOME), check whether the flags displayed below are already set and apply adjustments, if necessary:

cat $HOME/.R/Makevars

CXX14=g++
CXX17=g++
CFLAGS+=-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration
CXXFLAGS+=-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration
CXX14FLAGS += -std=c++14
CXX17FLAGS += -std=c++17 -fPIC

Please run the following lines of code to set the flags, if necessary:

mkdir -p ~/.R

echo "CXX14=g++" > $HOME/.R/Makevars
echo "CXX17=g++" >> $HOME/.R/Makevars
echo "CFLAGS+=-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration" >> $HOME/.R/Makevars
echo "CXXFLAGS+=-O3 -fPIC -malign-data=cacheline -finline-functions -m64 -Wno-implicit-function-declaration" >> $HOME/.R/Makevars
echo "CXX14FLAGS += -std=c++14" >> $HOME/.R/Makevars
echo "CXX17FLAGS += -std=c++17 -fPIC" >> $HOME/.R/Makevars

Install rjags package

# Set up environment and install JAGS within R session.
export PKG_CONFIG_PATH=$JAGS_HOME/lib/pkgconfig
export LD_RUN_PATH=$JAGS_HOME/lib

R -q
> install.packages("rjags", configure.args="--enable-rpath")
> library(rjags)

Use rjags

# Set up environment
export JAGS_HOME="$HOME/sw/jags"
export PATH=$JAGS_HOME/bin:$PATH
export PKG_CONFIG_PATH=$JAGS_HOME/lib/pkgconfig
export LD_RUN_PATH=$JAGS_HOME/lib

R -q
> library(rjags)