Skip to content

naresid.exclude query

2 messages · Simon Wood, Brian Ripley

#
x <- NA
  na.act <- na.action(na.exclude(x))
  y <- rep(0,0)
  naresid(na.act,y)

... currently produces the result...
  numeric(0)

... whereas the documentation might lead you to expect
  NA

The behaviour is caused by the line 
  if (length(x) == 0L) return(x)

in `stats:::naresid.exclude'. Removing this line results in the behaviour I'd 
expected in the above example (and in a test example where `x' is a zero row 
matrix). 

Is the coded behaviour necessary for some reason? Could it be changed (so that 
my example produces NA)? The reason I ask is that I use `napredict' in 
mgcv:predict.gam, and someone complained that if he predicts using newdata 
that is all NA, then he doesn't get what he expected (he has a pretty good 
reason for doing this). Part of the problem with predict.gam in this case was 
my code, but once I fixed that I ran up against the above problem.  

Actually, I just checked the source code and the line of naresid.exclude that 
causes the problem already has this comment after it....
  # << FIXME? -- reconstructing all NA object
... so I guess I'm really asking if there is any chance of fixing this soon, 
or whether I should just code up a work-around for predict.gam?

Simon
4 days later
#
There was a reason for that line, as running 'make check' with your 
modification would have shown you.  Sometimes naresid() is called with 
x=NULL.   AFAICS replacing the test with is.null(x) suffices, and I'm 
testing that in R-devel.  If nothing else comes up I will port it to 
R-patched later.

Note that R's own code does not AFAIR call these functions when 
predicting with 'newdata', and a fit with all cases omitted would not 
be very interesting so it is unsurprising no one noticed for >8 years.
On Fri, 14 Jan 2011, Simon Wood wrote: