1 of 6

Programming bits�(The “if” statement)

  • The if statement:

if (condition)

Statements-1;

else

Statements-2;

  • Combining statements:

{ statement; statement; }

2 of 6

Constructing conditions

  • RELATIONAL OPERATORS:

== Equal to (NOT =)

<= Less than or equal to

>= Greater than or equal to

< Less than

> Greater than

!= Not equal to

  • LOGICAL OPERATORS:

&& Condition is true if both sub-conditions are true

|| Condition is true if one of the sub-conditions is true

! Condition is true if sub-condition is false

  • Note: TMB may not work if a parameter or derived variable appears in a condition.

3 of 6

The “for” statement

for (initial state; terminal condition; increment)

{ statements; }

  • Initial state: Usually involves setting a “loop control variable” to some initial value.
  • Terminal condition: The loop will run until this condition is TRUE (note it need not involve the “loop control variable”).
  • Increment: This usually involves updating the “loop control variable” (e.g. X++; X--).
  • Parameters and derived variables should not appear in the terminal condition.

biomass(1) = 1;

for (year=0; year<nyear; year++)

biomass(year+1)=biomass(year)*r;

4 of 6

Vector & matrix functions

http://kaskr.github.io/adcomp/matrix_arrays_8cpp-example.html

Extracting from arrays and matrices:

vector = matrix.col(index)

vector = matrix.row(index)

vector = matrix.diagonal();

Subvectors: extract a subset of a vector:

vector2 = vector1(index1,index2)

NB: arrays start at 0, so index1=2 is element 3!

5 of 6

TMBhelper package

  • Contains AICTMB() function.

devtools::install_github("kaskr/TMB_contrib_R/TMBhelper")

  • Additional TMB contributed R packages available at

https://github.com/kaskr/TMB_contrib_R

(includes TMBphase to do phased estimation a la ADMB, and TMBdebug)

6 of 6

TMB Rstudio integration & code chunks

Some helpful code chunks:

https://github.com/kaskr/adcomp/wiki/Code--snippets

For Rstudio tmb snippets, and static code analyzer & more, use:

TMB:::setupRStudio()

Then restart Rstudio.