RTMB tips

requirements for RTMB functions

  • the objective function must be differentiable with respect to parameters (no if(), abs(), round(), min(), max() depending on parameters)
  • exotic probability distributions are available in the RTMBdist package (on CRAN)
  • aliases to base functions made before call to library(RTMB) might not work
  • use of <-[ (see here) etc.
    • specifically, if you use the c() function, or if you use the diag<- function (which sets the diagonal of a matrix) or the [<- function (which assigns values within a matrix), you need to add e.g. ADoverload("[<-") to the beginning of your function
  • You can only “grow” empty vectors if initialized with numeric(0), not c() or NULL (i.e. x <- c(); x[1] <- 2 throws an error, but x <- numeric(0); x[1] <- 2 does work), see here. In any case it is better practice to preallocate instead of growing vectors, e.g. x <- numeric(1); x[1] <- 2 (see chapter 2 of the R Inferno).
  • for matrix exponentials, you should use Matrix::expm() rather than expm::expm()
  • RTMB is pickier than R about matrix types. You may need to use some combination of drop() and as.matrix() to convert matrices with dimension 1 in some direction (or Matrix matrices) back to vectors
  • [[-indexing may be much faster than [-indexing: see here (and later messages in that thread)
  • if you use cat() or print() to print out numeric values, the results may not make sense (you’ll see a printout of RTMB’s internal representation of autodiff-augmented numbers …)

if transitioning from TMB

  • RTMB uses %*% (as in base R), not * (as in C++) for matrix/matrix and matrix/vector multiplication

more general points

  • as in C++ code, you probably don’t have to worry as much about non-vectorized code (e.g. for loops) being very slow relative to vectorized codes. However, vectorized code builds the model object (i.e. runs MakeADFun) much faster. In addition, you can control vectorized RTMB code to use more efficient internal operations e.g. by setting TapeConfig(vectorize="enable"). Comparing natively vectorized code (fastest) to RTMB vectorized or for-loop code (both a bit slower, but not different from each other) to native R for-loops (much slower):

  • data handling (see here)
  • have to handle prediction (see here), tests, diagnostics, etc. etc. yourself (although the broom.mixed package does have a rudimentary tidy method for TMB objects (e.g. class(TMBobj) <- c("TMB", class(TMBobj)); broom.mixed::tidy(TMBobj))
  • if you do something clever where you define your objective function in a different environment from where you call MakeADFun, you can use assign(..., environment(objective_function)) to make sure that the objective function can see any objects it needs to know about …

Which standard operations are available in RTMB?

Description Function
The Beta Distribution dbeta, pbeta, qbeta
The Binomial Distribution dbinom, pbinom
The Cauchy Distribution dcauchy
The Exponential Distribution dexp, pexp, qexp
The F Distribution df
The Gamma Distribution dgamma, pgamma, qgamma
The Logistic Distribution dlogis, plogis, qlogis
The Multinomial Distribution dmultinom
The Negative Binomial Distribution dnbinom, pnbinom
The Normal Distribution dnorm, pnorm, qnorm
The Poisson Distribution dpois, ppois
The Student list(“t”) Distribution dt
The Weibull Distribution dweibull, pweibull, qweibull
Arithmetic Mean mean
Arithmetic Operators -, *, /, %%, ^, +
Logarithms and Exponentials exp, expm1, log, log1p
Trigonometric Functions acos, asin, atan, atan2, cos, sin, tan
Hyperbolic Functions acosh, asinh, atanh, cosh, sinh, tanh
Miscellaneous Mathematical Functions abs, sqrt
Special Functions of Mathematics gamma, lbeta, lgamma
Apply Functions Over Array Margins apply
Bessel Functions besselI, besselJ, besselK, besselY
Complex Numbers and Basic Functionality as.complex
Integration of One-Dimensional Functions integrate
Sign Function sign
Vectorize a Scalar Function Vectorize
Combine Values into a Vector or List c
Double-Precision Vectors as.double
Numeric Vectors is.numeric
Product of Vector Elements prod
Replicate Elements of Vectors and Vector-Like Objects rep
Sorting or Ordering Vectors sort
Sum of Vector Elements sum
Vectors - Creation, Coercion, etc as.vector
Calculate the Determinant of a Matrix determinant
Matrix Cross-Product crossprod, tcrossprod
Matrix Diagonals diag
Matrix Multiplication %*%
Singular Value Decomposition of a Matrix svd
Spectral Decomposition of a Matrix eigen
The Cholesky Decomposition chol
“Not Available” / Missing Values is.na
Array Transposition aperm
Combine R Objects by Rows or Columns cbind, rbind
Conditional Element Selection ifelse
Correlation, Variance and Covariance (Matrices) cov2cor
Cumulative Sums, Products, and Extremes cumprod, cumsum
Extract or Replace Parts of an Object [, [[, [<-
Fast Discrete Fourier Transform (FFT) fft
Find Interval Numbers or Indices findInterval
Finite, Infinite and NaN Numbers is.finite, is.infinite, is.nan
Form Row and Column Sums and Means colSums, rowSums
Interpolating Splines splinefun
Lagged Differences diff
Length of an Object length<-
Matrices matrix
Maxima and Minima max, min
Methods for Coercing an Object to a Class coerce
One Dimensional Root (Zero) Finding uniroot
Print Values print
Rounding of Numbers ceiling, floor, trunc
Show an Object show
Solve a System of Equations solve