MakeTape throws an error: Error in advector(y) : Invalid argument to 'advector' (lost class attribute?)

58 views
Skip to first unread message

Tomas T.

unread,
Sep 8, 2025, 9:24:57 AMSep 8
to TMB Users
Hello,

I am using MakeTape to use the AD feature. MakeTape throws a weird error - here is a reproducible code:


library(RTMB)

N <- 100
d <- data.frame(present = rbinom(N, 1, 0.3))

inv.logit <- plogis

# log likelihood but I need the vector for each data point rather than just a sum
ll_pa <- function (data, par)
{
y <- data
cat("parameter par:\n")
str(par)
cat("\n")
p <- inv.logit(par)
#p <- plogis(par)
#p <- exp(par)/(exp(par)+1)

y0 <- y$present == 0
y1 <- y$present == 1
ll_ <- c()
ll_[y1] <- log(p[y1])
ll_[y0] <- log(1 - p[y0])
ll_
}

cmb <- function(f, data) function(par) f(data, par)
# closure trick somewhere from the RTMB docs, to tie this to particular data and prevent unnecessary headaches!

F <- MakeTape(cmb(ll_pa, data = d), numeric(N))

The output is:

parameter par:
Formal class 'advector' [package ""] with 0 slots
 list()

Error in advector(y) :
  Invalid argument to 'advector' (lost class attribute?)


When I try to call my function directly, it works alrigth:

par0 <- rnorm(N)
ll_pa(d, par0)

Where is a problem here? I also tried to replace my inv.logit directly with plogis or the exp formula, but it's the same.

Thanks in advance!

Tomas

Phillip Vetter

unread,
Sep 8, 2025, 9:56:50 AMSep 8
to TMB Users
Looks like you need to pre-allocate ll_:
 
ll_ <- numeric(N) instead of ll_ <- c()

Tomas T.

unread,
Sep 9, 2025, 2:25:44 PM (13 days ago) Sep 9
to TMB Users
Thanks, it works!!

But, what the heck? This is quite a gotcha. The function doesn't need the pre-allocation to be run directly! Also, the error message didn't help much.

Is there some documentation of all the requirements the function must fulfill? It would be very helpful to read it beforehand and avoid these gotchas :)
The MakeTape help just says R function, differentiable.

Thank you very much!

Tomas

Phillip Vetter

unread,
Sep 9, 2025, 4:28:35 PM (13 days ago) Sep 9
to TMB Users
I think you have to learn the cases where this error occurs, and what to look out for.

You'll need Kasper for the details, but the heart of the issue, as the error suggests is that you are losing the 'advector' class somewhere

?RTMB::advector
An advector is a class used behind the scenes to replace normal R numeric objects during automatic differentiation. An advector has a 'temporary lifetime' and therefore you do not see / need to know it as a normal user.

This apparently happens when you try to assign an 'advector' (log(p[y1])) into the 'NULL'  created by 'c()'. This creates a vector of class 'complex' not 'advector'.

library(RTMB)
f <- function(x){
  y <- c()
  print(class(y))
  y[TRUE] <- x
  print(class(y))
  y
}
try(MakeTape(f, numeric(1)))

When the object is created as a numeric then

f2 <- function(x){
  y <- numeric(1)
  print(class(y))
  y[TRUE] <- x
  print(class(y))
  y
}
MakeTape(f2, numeric(1))

Ben Bolker

unread,
Sep 9, 2025, 8:16:12 PM (13 days ago) Sep 9
to tmb-...@googlegroups.com
As I earlier said I might, I've started a repo/document here:

https://github.com/bbolker/rtmb_tips/blob/main/README.md

Suggestions, issues, pull requests welcome.
> --
> To post to this group, send email to us...@tmb-project.org. Before
> posting, please check the wiki and issuetracker at https://github.com/
> kaskr/adcomp/ <https://github.com/kaskr/adcomp/>. Please try to create a
> simple repeatable example to go with your question (e.g issues 154, 134,
> 51). Use the issuetracker to report bugs.
> ---
> You received this message because you are subscribed to the Google
> Groups "TMB Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to tmb-users+...@googlegroups.com <mailto:tmb-
> users+un...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/tmb-
> users/4c198ce5-2356-4fc7-b3a8-e39888ca5b39n%40googlegroups.com <https://
> groups.google.com/d/msgid/tmb-users/4c198ce5-2356-4fc7-b3a8-
> e39888ca5b39n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Associate chair (graduate), Mathematics & Statistics
Director, School of Computational Science and Engineering
* E-mail is sent at my convenience; I don't expect replies outside of
working hours.

Tomas T.

unread,
Sep 10, 2025, 12:10:32 PM (12 days ago) Sep 10
to TMB Users
Dear Ben, Phillip, all,

Ben, thank you very much for the new README! This is awesome, exactly what I needed! I have created a pull request proposing a few changes about this gotcha (slightly conflicting with Phillip's pull request, see below in this email), and added another gotcha that I just reported to github.

Phillip, thank you very much for explanation and the explanatory code! I figured I don't even need to preallocate the vector to particular length. I just need to declare it is numeric. So instead of

y <- c()

which throws the error, I can just fix it by

y <- numeric()

So it seems the vector length isn't an issue, just the type. I have proposed a pull request of Ben's documentation in this sense.

Thanks Phillip for teaching me about advector! I didn't even know this exists, since it is an internal RTMB type. That's also why I couldn't make the sense of the error message.

Thank you both very much!

Tomas


Reply all
Reply to author
Forward
0 new messages