The TMB objective functions are seemingly defined in one function block that is saved to a <name>.cpp
file. Then, after compiling the file, each objective function is accessed by loading with the command dyn.load(dynlib(<name>))
.
Is it possible to store more than one objective function in each .cpp
file? For example, the following two objective functions are very similar to each other, but at the moment need to be saved to different files:
// TMB Tutorial but with fixed variance
#include <TMB.hpp> // Links in the TMB libraries
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_VECTOR(x); // Data vector transmitted from R
PARAMETER(mu); // Parameter value transmitted from R
Type sigma = 1.0;
Type f; // Declare the "objective function" (neg. log. likelihood)
f = -sum(dnorm(x,mu,sigma,true)); // Use R-style call to normal density
return f;
}
and
// TMB Tutorial
#include <TMB.hpp> // Links in the TMB libraries
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_VECTOR(x); // Data vector transmitted from R
PARAMETER(mu); // Parameter value transmitted from R
PARAMETER(sigma); //
Type f; // Declare the "objective function" (neg. log. likelihood)
f = -sum(dnorm(x,mu,sigma,true)); // Use R-style call to normal density
return f;
}