'错误的范围组件'来自TMB的错误消息(未计算最大梯度组件)

时间:2018-04-30 11:20:12

标签: c++ r rcpp tmb

亲爱所有热心的TMB用户或在R中使用C ++的用户帮助我。

作为婴儿TMB用户,我对来自TMB的错误消息有疑问。

我发现代码中存在错误。 (第二个错误被修改)

结果来自gdbsource()

1:Incomplete final line found on我的cpp文件(实际上这是一条警告信息,而不是错误。我想知道为什么会这样。)

2:编译后出现以下错误消息:

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)

您对第二个错误有任何疑问吗?

我提前感谢您的努力,反馈,提示以及您的帮助!

我附上了我的cpp,R代码和数据。

我想在此页面上发布更多内容。 我的代码中的主要问题是定义参数或向量。

在评论之后,我运行了我的代码' cout'部分。 和结果

CPP代码(修订版)

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))

数据

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

我还附上了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

0 个答案:

没有答案