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

Steps to install TMB

willsatterthwaite edited this page Feb 2, 2017 · 8 revisions

To install TMB, please use the following steps:

Install a recent version of R (JTT currently uses 3.2.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

Install TMB from CRAN using install.packages("TMB") in the R terminal

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

######################
# Simulate data
######################

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 )
# Answer should be "313.4137"