Using C++ directives to make ADMB report file

One way to get output from ADMB is making a suitable encode REPORT_SECTION, allowing custom output of the model result. However, when your model have hundreds of output, declare each one can be a repetitive task. Recently, I was exploring ADMB repositories and I got a nice option, provided by Steve Martell, that make use of C++ directives.

Consider this simple linear regression model (data gist):

DATA_SECTION
 init_int nobs
 init_matrix data(1,nobs,1,2)
 vector x(1,nobs)
 vector y(1,nobs)

PARAMETER_SECTION
 init_number a(1)
 init_number b(2)
 objective_function_value f
 LOCAL_CALCS
   x = column(data,2);
   y = column(data,1);
 END_CALCS

PROCEDURE_SECTION
 f=0;
 for (int i=1;i<=nobs;i++)
    {
     f+=square(y(i)-(a+b*x(i)));
    }

REPORT_SECTION
 report << "rss: " << f << endl;
 report << "data: " << data << endl;

Note that REPORT_SECTION have two lines with repetitive code. Using both #undef and #define C++ directives, you can save a lot of encode, principally if your model is multiparametric like most fisheries stock assessment models. All you need to do is add the following lines into the GLOBALS_SECTION and modify the REPORT_SECTION according to C++ directives:


GLOBALS_SECTION
 #undef myReport
 #define myReport(object) report << #object "\n" << object << endl;

DATA_SECTION
 init_int nobs
 init_matrix data(1,nobs,1,2)

PARAMETER_SECTION
 ....
 ....
 ....

REPORT_SECTION
 myReport(x);
 myReport(f);

Enjoy!!!