kaskr / RTMB

R bindings to TMB
Other
48 stars 6 forks source link

base::ifelse() removes class attribute #1

Closed bragef closed 1 year ago

bragef commented 1 year ago

If ifelse() is used in the objective function, the class attribute is lost, resulting in an error.

> class(ifelse(TRUE,advector(1),advector(1)))
[1] "complex"

Example

library(RTMB)
Z <- rnorm(10) > 0
Y <- rnorm(10,10) * Z + rnorm(10,-10) * (1-Z)
par <- list(a=1)

f <- function(par) {
  a <- par$a
  logls <- ifelse(Z, dnorm(Y, par$a, log=TRUE), dnorm(Y, -par$a,log=TRUE))
  # class(logls) <- "advector"  
  -sum(logls)
}

obj <- MakeADFun(f, par)
opt <- nlminb(obj$par,obj$fn)
sdreport(obj)
kaskr commented 1 year ago

Yes, I've noticed that too. ifelse is used so frequently that it has to be fixed.