I can't get rid of this error:
Error: '*this' is not a valid 'advector' (constructed using illegal operation?)
The error is probably caused by this indexing:
sum(log(s[NDFirstRow[i]:NDLastSurvivedRow[i]])) . Can't seem to find anything relevant for this in
Ben's tips. I found this issue, but this should have been fixed since v 1.7:
https://github.com/kaskr/RTMB/issues/33I would appreciate help with this!
I am using RTMB v 1.8. This is my NLL function:
nest_surv_nll <- function(par, data) {
getAll(data, par, warn=FALSE)
# "[<-" <- ADoverload("[<-")
#ADREPORT(beta)
s <- plogis(x %*% beta)[,1] # s = daily survival rate
ll <- 0
for (i in 1:nrow(nests)) {
ll <- ll + sum(log(s[NDFirstRow[i]:NDLastSurvivedRow[i]]))
if (!
is.na(Fate[i]) && Fate[i] == 0) { # we know this nest failed
stopifnot(!
is.na(NDFirstNotSurvivedRow[i]))
ll <- ll + log(1 - prod(s[NDFirstNotSurvivedRow[i]:NDLastRow[i]]))
}
}
-ll
}
cmb <- function(f, d) function(p) f(p, d)
obj <- MakeADFun(cmb(nest_surv_nll, data), par)
Here is how the data looks like:
> str(data)
List of 6
$ NDFirstRow : int [1:1052] 1309 1323 124 143 1 28 51 54 77 85 ...
$ NDLastRow : int [1:1052] 1322 1328 142 160 27 50 53 76 84 92 ...
$ NDLastSurvivedRow : int [1:1052] 1322 1323 142 156 27 50 53 76 79 92 ...
$ NDFirstNotSurvivedRow: int [1:1052] NA 1324 NA 157 NA NA NA NA 80 NA ...
$ Fate : num [1:1052] 1 0 1 0 NA 1 NA 1 0 NA ...
$ x : num [1:15165, 1:57] 1 1 1 1 1 1 1 1 1 1 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:15165] "1" "2" "3" "4" ...
.. ..$ : chr [1:57] "(Intercept)" "Habitatjetel" "Habitatkukurice" "Habitatlouka" ...
..- attr(*, "assign")= int [1:57] 0 1 1 1 1 1 1 1 1 1 ...
..- attr(*, "contrasts")=List of 4
.. ..$ Habitat : chr "contr.treatment"
.. ..$ klimaticky_region: chr "contr.treatment"
.. ..$ teply_region_flag: chr "contr.treatment"
.. ..$ slope_cat_txt : chr "contr.treatment"
> str(par)
List of 1
$ beta: Named num [1:57] 0 0 0 0 0 0 0 0 0 0 ...
..- attr(*, "names")= chr [1:57] "(Intercept)" "Habitatjetel" "Habitatkukuric
Thanks!
Tomas