I'm facing NA/NaN errors with my TMB code. Searching on the internet, I found that including fenv.h
file into my code can give me a clue about the source of errors.
Since this example nan error does not work on Windows, I decided to follow an advice to run this example in Virtual TMB (Because my old desktop cannot afford to run more than one operating system).
But the new problem appeared. I get unused variable errors
after running the code.
This code is copied and pasted from the link above.
cpp code
// Illustrates how to make the debugger catch a floating point error.
#include <TMB.hpp>
#include <fenv.h> // Extra line needed
template<class Type>
Type objective_function<Type>::operator() ()
{
feenableexcept(FE_INVALID | FE_OVERFLOW | FE_DIVBYZERO | FE_UNDERFLOW); // Extra line needed
DATA_SCALAR(lambda);
PARAMETER(x);
Type f;
f = sqrt(-1.); // FE_INVALID ( sqrt(-1.) returns NaN )
//f = 1./0.; // FE_DIVBYZERO ( division by zero )
//f = exp(100000.); // FE_OVERFLOW ( exp(100000.) returns Inf ) [Does not work on all platforms]
//f = exp(-100000.); // FE_UNDERFLOW ( exp(-100000.) returns 0 )
return f;
}
and here is the corresponding r code for Linux
data <- list(lambda = 25)
parameters <- list(x=1)
require(TMB)
compile('nan.cpp','-fno-gnu-unique -O0 -Wall')
dyn.load(dynlib('nan'))
model <- MakeADFun(data, parameters)
fit <- nlminb(model$par, model$fn, model$gr)
rep <- sdreport(model)
print(rep)
I attached my sessionInfo and the warning messages via image files.
Error
nan.cpp: In instantiation of 'Type objective_function<Type>::operator()() [with Type =double]':
/home/tmb/r/library/TMB/include/tmb_core.hpp:1416:16: required from here
nan.cpp:7:15:warning:unused variable 'lambda' [-Wunused -variable]
DATA_SCALAR(lambda);
^
/home/tmb/r/library/TMB/include/tmb_core.hpp:199:32: note: in definition of macro 'DATA_SCALAR'
#define DATA_SCALAR(name) Type name(asVector<Type>( \
nan.cpp:8:13: warning: unused variable 'x' [-Wunused-variable]
PARAMETER(x);
^
/home/tmb/r/library/TMB/include/tmb_core.hpp:182:30: note: in definition of macro 'PARAMETER'
How can I solve this problem?
Thanks!
Warning message:
Session Info: