Hi all,
Just FYI, in answer to the (my own) question, the following seems to work in both regular R and with RTMB:
Recall (see original message) that I wanted to calculate values for a function "f" used in an objective function only at values of a vector "z" that were less than a fixed parameter "z1".
So
z1: a fixed parameter value using map-list(z1=factor(NA)) when MakeADFun'ing the objective function
in the R function called in the objective function, I used:
if (RTMB:::ad_context()){
#--in AD mode
nz1 = RTMB:::getValues(z1); #--convert value to regular numeric
} else {
nz1 = z1; #--just copy in regular R session
}
fv = AD(array(0,dim=length(z))); #--advector for results
idx = which(z>=z1); #--indices of z at which to calculate f
fv[idx] = f(z[idx],other stuff); #--calculates only at specified indices, 0 otherwise
The above seems to work: got no errors when making the objective function and got sensible results in a simple optimization test case.
The gradient from the objective function for the z1 parameter was 0, as expected, and the gradients for the other parameters I was fitting to were on the order of 10^-6.