If x is an advector manually created with advector, then is.na(x) will unexpectedly return TRUE regardless of the actual value of x. I encountered this when working with rasters/matrices with some NA entries where I needed to update all cells that were not NA. Hopefully this turns out to be an easy fix because this bug does not affect advectors coming from the parameter list.
As a (cumbersome) workaround you can create a non-advector copy of x and run is.na(notad_x).
Thanks for looking into this!
library(RTMB)
# is.na(x) = TRUE, unexpected
is.na(advector(1))
is.na(AD(1, force = TRUE))
# is.na(x) returns TRUE for all entries of x
# Overwritten x is
# [NA 0 0]
# [0 0 0]
# [0 0 0],
# unexpected
x<- advector(matrix(c(NA, rep(0, 8)), nrow = 3, ncol = 3))
x[!is.na(x)]<- seq_along(x[!is.na(x)])
x
# Workaround to get overwritten x to be
# [NA 3 6]
# [1 4 7]
# [2 5 8],
# as expected
notad_x<- matrix(c(NA, rep(0, 8)), nrow = 3, ncol = 3)
x<- advector(notad_x)
x[!is.na(notad_x)]<- seq_along(notad_x[!is.na(notad_x)])
x
# is.na(x) = FALSE, expected
obj<- MakeADFun(
function(pars) {
print(is.na(pars[[1]]))
return(0)
},
list(1)
)
Session Info:
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Pop!_OS 22.04 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
time zone: America/Halifax
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RTMB_1.5
loaded via a namespace (and not attached):
[1] MASS_7.3-61 compiler_4.4.1 Matrix_1.6-5 TMB_1.9.14 Rcpp_1.0.13 codetools_0.2-19
[7] grid_4.4.1 lattice_0.22-5
If
x
is an advector manually created withadvector
, thenis.na(x)
will unexpectedly returnTRUE
regardless of the actual value ofx
. I encountered this when working with rasters/matrices with someNA
entries where I needed to update all cells that were notNA
. Hopefully this turns out to be an easy fix because this bug does not affect advectors coming from the parameter list.As a (cumbersome) workaround you can create a non-advector copy of x and run
is.na(notad_x)
.Thanks for looking into this!
Session Info: