Dear all TMB users with warm heart or users who use C++ in R about to help me.
As an infant TMB user, I have questions about my error messages from TMB.
I found that there are errors in my code. (The 2nd error is revised)
The results are from gdbsource()
1: Incomplete final line found on
my cpp file (Actually this is a warning message, not the error. I'd like to know why this happened.)
2: Following error message emerging after compilation:
Error in ev(obj$env$par): Wrong range component.
Error in ev(obj$env$par) : Wrong range component.
check1: 1
0.208386
9.70444e-005
6.95024e-005
8.33432e-005
7.90787e-005
5.96018e-005
7.0229e-005
9.99291e-005
0.000112216
0.000101964
9.13024e-005
8.54733e-005
8.77122e-005
8.77124e-005
8.36922e-005
6.80879e-005
0.000135715
0.00014771
6.97471e-005
5.73876e-005
5.2996e-005
7.63701e-005
check2: 0.367879
check3: 20.0855
check4: 0.00273944
check5: 0.0450492
check6: 0.0301974
Optimizing tape... Done
Error in ev(obj$env$par) : Wrong range component.
In addition: Warning messages:
1: In nlminb(model$par, model$fn, model$gr) : NA/NaN function evaluation
2: In he(par) : restarting interrupted promise evaluation
outer mgc: NaN
Error in nlminb(model$par, model$fn, model$gr) :
gradient function must return a numeric vector of length 5
Execution halted
[Inferior 1 (process 8152) exited with code 01]
C:\Users\POPDYN~1\AppData\Local\Temp\RtmpkL2d5A\file1f1048dc1743:4: Error in sourced command file:
No stack.
(gdb)
Do you have any idea about the second error?
I appreciate your effort, feedback, your tips, and your help in advance!
I attached my cpp, R codes, and data.
I would like to post more contents on this page. The main problem in my code thought to be defining parameter or vector.
Following the comment, I ran my code included 'cout' part. and the result
CPP code (revised)
template<class Type>
Type objective_function<Type>::operator() ()
{
//data
DATA_VECTOR(C);
DATA_VECTOR(I);
int n = C.size();
//free parameters
PARAMETER(logR);
PARAMETER(logK);
PARAMETER(logQ);
PARAMETER(logsdproc); //log(sd) in the process error;
PARAMETER(logSigma);
PARAMETER_VECTOR(P);
Type r = exp(logR);
Type k = exp(logK);
Type q = exp(logQ);
Type sdproc = exp(logsdproc);
Type sigma = exp(logSigma);
//derived parameters
vector<Type> Ihat(n);
Type f = 0.0;
Type fpen = 0.0;
Type tmpP;
P(0)=1.0;
for(int t=0; t<(n-1); t++) {
//P(t)=B(t)/k;
tmpP = P(t) + r*P(t)*(1-P(t))-C(t)/k;
P(t+1) = posfun(tmpP, Type(0.01), fpen);
f += fpen;
f -= dnorm(log(P(t+1)), log(tmpP), sdproc, true);
};
for(int t=0; t<n; t++) {
Ihat(t)=q*P(t)*k;
};
f -= sum(dnorm(log(I), log(Ihat), sigma, true));
REPORT(P);
REPORT(Ihat); // plot
REPORT(fpen);
std::cout << " check1: " << P << std::endl; //thank you Wave!
std::cout << " check2: " << r << std::endl;
std::cout << " check3: " << k << std::endl;
std::cout << " check4: " << q << std::endl;
std::cout << " check5: " << sdproc << std::endl;
std::cout << " check6: " << sigma << std::endl;
return f;
}
albacore <- read.table("albacore.csv", header=TRUE, sep=",")
albacore
names(albacore) <- c("t", "C", "I")
n=c(dim(albacore)[1]) #the number of Bs
parameters <- list(logR=-1.0, logK=3.0, logQ=-5.9, logsdproc=-3.1, logSigma=-3.5, P=rep(0.3,n));
parameters
require(TMB)
compile("scalbav2.cpp", "-O1 -g", DLLFLAGS="")
dyn.load(dynlib("scalbav2"))
library(TMB)
gdbsource("scalbav2.R", interactive=TRUE)
################################################################################
model<- MakeADFun(albacore, parameters, random="P", DLL="scalbav2")
model$par
length(parameters$P)
fit <- nlminb(model$par, model$fn, model$gr)
rep <- sdreport(model)
print(summary(rep))
Data
year catch cpue
1967 15.9 61.89
1968 25.7 78.98
1969 28.5 55.59
1970 23.7 44.61
1971 25.0 56.89
1972 33.3 38.27
1973 28.2 33.84
1974 19.7 36.13
1975 17.5 41.95
1976 19.3 36.63
1977 21.6 36.33
1978 23.1 38.82
1979 22.5 34.32
1980 22.5 37.64
1981 23.6 34.01
1982 29.1 32.16
1983 14.4 26.88
1984 13.2 36.61
1985 28.4 30.07
1986 34.6 30.75
1987 37.5 23.36
1988 25.9 22.36
1989 25.3 21.91
I also attached sessioninfo:
R version 3.5.0 (2018-04-23)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows >= 8 (build 9200)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] TMB_1.7.13
loaded via a namespace (and not attached):
[1] compiler_3.5.0 Matrix_1.2-14 tools_3.5.0 grid_3.5.0
[5] lattice_0.20-35