Create a cpp template to get started.

template(file = NULL)

Arguments

file

Optional name of cpp file.

Details

This function generates a C++ template with a header and include statement. Here is a brief overview of the C++ syntax used to code the objective function. For a full reference see the Doxygen documentation (more information at the package URL).

Macros to read data and declare parameters:

Template SyntaxC++ typeR type
DATA_VECTOR(name)vector<Type>vector
DATA_MATRIX(name)matrix<Type>matrix
DATA_SCALAR(name)Typenumeric(1)
DATA_INTEGER(name)intinteger(1)
DATA_FACTOR(name)vector<int>factor
DATA_IVECTOR(name)vector<int>integer
DATA_SPARSE_MATRIX(name)Eigen::SparseMatrix<Type>dgTMatrix
DATA_ARRAY(name)array<Type>array
PARAMETER_MATRIX(name)matrix<Type>matrix
PARAMETER_VECTOR(name)vector<Type>vector
PARAMETER_ARRAY(name)array<Type>array
PARAMETER(name)Typenumeric(1)

Basic calculations:

Template SyntaxExplanation
REPORT(x)Report x back to R
ADREPORT(x)Report x back to R with derivatives
vector<Type> v(n1);R equivalent of v=numeric(n1)
matrix<Type> m(n1,n2);R equivalent of m=matrix(0,n1,n2)
array<Type> a(n1,n2,n3);R equivalent of a=array(0,c(n1,n2,n3))
v+v,v-v,v*v,v/vPointwise binary operations
m*vMatrix-vector multiply
a.col(i)R equivalent of a[,,i]
a.col(i).col(j)R equivalent of a[,j,i]
a(i,j,k)R equivalent of a[i,j,k]
exp(v)Pointwise math
m(i,j)R equivalent of m[i,j]
v.sum()R equivalent of sum(v)
m.transpose()R equivalent of t(m)

Some distributions are available as C++ templates with syntax close to R's distributions:

Function headerDistribution
dnbinom2(x,mu,var,int give_log=0)Negative binomial with mean and variance
dpois(x,lambda,int give_log=0)Poisson distribution as in R
dlgamma(y,shape,scale,int give_log=0)log-gamma distribution
dnorm(x,mean,sd,int give_log=0)Normal distribution as in R

Examples

template()
#> #include <TMB.hpp>
#> 
#> template<class Type>
#> Type objective_function<Type>::operator() ()
#> {
#>   /* Minimal example */
#>   DATA_VECTOR(x);
#>   PARAMETER(mu);
#>   PARAMETER(logSigma);
#> 
#>   Type f = 0;
#>   f -= dnorm(x, mu, exp(logSigma), true).sum();
#> 
#>   return f;
#> 
#>   /* Quick Reference
#>      ===============
#> 
#>      ** Macros to read data and declare parameters:
#> 
#>      _Template_Syntax_              _C++_type_                     _R_type_
#>      DATA_VECTOR(name)              vector<Type>                   vector
#>      DATA_MATRIX(name)              matrix<Type>                   matrix
#>      DATA_SCALAR(name)              Type                           numeric(1)
#>      DATA_INTEGER(name)             int                            integer(1)
#>      DATA_FACTOR(name)              vector<int>                    factor
#>      DATA_SPARSE_MATRIX(name)       Eigen::SparseMatrix<Type>      dgTMatrix
#>      DATA_ARRAY(name)               array<Type>                    array
#>      PARAMETER_MATRIX(name)         matrix<Type>                   matrix
#>      PARAMETER_VECTOR(name)         vector<Type>                   vector
#>      PARAMETER_ARRAY(name)          array<Type>                    array
#>      PARAMETER(name)                Type                           numeric(1)
#> 
#>      ** Macro to report intermediate expressions back to R:
#> 
#>      REPORT(x)
#>      ADREPORT(x)
#> 
#>      ** Basic constructors:
#> 
#>      vector<Type> v(n1);
#>      matrix<Type> m(n1,n2);
#>      array<Type> a(n1,n2,n3)
#> 
#>      ** Basic operations:
#> 
#>      v+v,v-v,v*v,v/v                Pointwise binary operations
#>      m*v                            Matrix-vector multiply
#>      a.col(i)                       R equivalent of a[,,i]
#>      a.col(i).col(j)                R equivalent of a[,j,i]
#>      a(i,j,k)                       R equivalent of a[i,j,k]
#>      exp(v)                         Pointwise math
#>      m(i,j)                         R equivalent of m[i,j]
#>      v.sum()                        R equivalent of sum(v)
#>      m.transpose()                  R equivalent of t(m)
#> 
#>      ** Distributions:
#> 
#>      Type dnbinom2(const Type &x, const Type &mu, const Type &var, int give_log=0)
#>      Type dpois(const Type &x, const Type &lambda, int give_log=0)
#>      Type dlgamma(Type y, Type shape, Type scale, int give_log=0)
#>      Type dnorm(Type x, Type mean, Type sd, int give_log=0)
#> 
#>      ** Parallel accumulator declaration (only methods "+=" and "-="):
#>      
#>      parallel_accumulator<Type> res(this);
#> 
#>   */
#> 
#> }