Error: '*this' is not a valid 'advector' (constructed using illegal operation?)

13 views
Skip to first unread message

Tomas T.

unread,
Jan 23, 2026, 11:12:32 AM (14 days ago) Jan 23
to TMB Users
Hello,

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/33

I 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

Tomas T.

unread,
Jan 23, 2026, 12:40:04 PM (14 days ago) Jan 23
to TMB Users
I am sorry, please ignore the previous email, this is not an RTMB issue. It was caused by bad indices -  NDLastSurvivedRow[i] pointing to non-existing rows. (I am using model.matrix() to construct x and I didn't notice it is just deleting my rows without any warning!!! Rows with some predictors being NA, but still, I would prefer to keep them or at least give a warning....)

Cheers to you all!

Tomas

Reply all
Reply to author
Forward
0 new messages