kaskr / RTMB

R bindings to TMB
Other
49 stars 6 forks source link

`rowSums(M, na.rm=TRUE)` causes an NA in the evaluated tape #27

Closed James-Thorson-NOAA closed 5 months ago

James-Thorson-NOAA commented 5 months ago

In developing an R-package using RTMB, I think I found that rowSums(M, na.rm=TRUE) when matrix M contains some NA values causes the obj$fn() built by obj = MakeADFun(.) to evaluate to NA even obj$report() outputs a non-NA scalar as expected for the same value.

I could work on a simplified example, but does this trigger any immediate thoughts on why this might happen, or how to add an informative error message for cases such as this?

jgbabyn commented 5 months ago

It looks like rowSums as defined by RTMB doesn't support na.rm as an option as shown in the manual. RTMB from my understanding runs the base R functions during the report which leads to the discrepancy. It would be nice for there to be some error message when an unsupported argument is used as this happens with a few functions in a few different ways (e.g., pnorm not supporting log.p) and it's not totally intuitive.

kaskr commented 5 months ago

An example:

F <- MakeTape(function(x)colSums(matrix(c(NA,NA,NA,x),2,2), na.rm=TRUE), 1)
F(2)

returns NA vector but should be c(0,2).

@jgbabyn you are right - there are many more examples of misleading error messages (including pnorm).