Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Steps to install TMB

Jim Thorson edited this page May 14, 2019 · 8 revisions

To install TMB, please use the following steps:

  1. Install a recent version of R

  2. If using a Windows machine, install a matching version of Rtools

    • Available at https://cran.r-project.org/bin/windows/Rtools/
    • For example, R v3.2.2 requires Rtools33
    • During installation, please check box to ensure that Rtools is added to your PATH
    • It is not necessary to install Rtools if using an apple or Linux operating system
    • On a government machine, this will require admin privileges, and will probably require working with your IT department (and they can often do this remotely)
  3. Install TMB from CRAN using install.packages("TMB") in the R terminal

  4. Confirm that TMB is working by running a simple TMB example, by cut-pasting the following text into your R terminal:

######################
# Simulate data for a linear mixed model with random intercepts
######################

set.seed(1)
Factor = rep( 1:10, each=10)
Z = rnorm( length(unique(Factor)), mean=0, sd=1)

X0 = 0
Y = Z[Factor] + X0 + rnorm( length(Factor), mean=0, sd=1)

######################
# Run in TMB
######################

install.packages("TMB")
library(TMB)
Version = "linear_mixed_model"

# Download CPP file
setwd( tempdir() )
download.file( url="https://raw.githubusercontent.com/James-Thorson/mixed-effects/master/linear_mixed_model/linear_mixed_model.cpp", destfile="linear_mixed_model.cpp", method="auto")
compile( paste0(Version,".cpp") )

# Generate inputs for TMB
Data = list( "n_data"=length(Y), "n_factors"=length(unique(Factor)), "Factor"=Factor-1, "Y"=Y)
Parameters = list( "X0"=-10, "log_SD0"=2, "log_SDZ"=2, "Z"=rep(0,Data$n_factor) )
Random = c("Z")

# Build TMB object
dyn.load( dynlib(Version) )
Obj = MakeADFun(data=Data, parameters=Parameters, random=Random)  #

# Check that TMB is working
Obj$fn( Obj$par )

You should have gotten 313.4137 in your R terminal. If you did not, TMB is not working properly. The most frequent error is a warning message in the compile( paste0(Version,".cpp") ) line, when a message is displayed that ends with Status 127. This error message indicates that TMB was unable to find a C++ compiler, probably because Rtools was not installed properly.