[R] can predict ignore rows with insufficient info

Thomas W Blackwell tblackw at umich.edu
Tue Sep 16 23:57:39 CEST 2003



On Tue, 16 Sep 2003, Peter Whiting wrote:

> It seems that predict removes rows with insufficient information
> (ie, if I replace "ALBANY" with NA and refactor everything works)
> - I wonder why it doesn't exhibit the same behavior when it
> encounters a new level - just eliminate the row and go on...
>
> Somewhat related: I had been assuming (incorrectly)
> that length(x) would equal length(const$days) after
> x<-predict(g,const) - this isn't the case if any of the rows of
> const don't contain enough info for the model.  Those rows are
> eliminated - I'd have expected them to just be NAs in the result.
> I'll go back and look through the documents to see if there is a
> straight forward way to convert:
>
> > x
>   1   3   4
> 1.5 1.5 1.5
>
> to
> > x
>   1  2  3   4  5
> 1.5 NA 1.5 1.5 NA
>
> slowly learning,
> pete

Before running  predict(...),  do  options(na.action="na.exclude").
this will give the equal length behavior that you may want ... as
long as you have replaced unsupported factor levels with NA.  See
help("na.omit")  and  help("options")  to see what this is doing.
(It won't have any effect of course, if you subscript the newdata
argument to predict() using my strategy.)

And, DO use a simple strategy that you cooked up yourself, in
preference to anything canned.  It's much easier to maintain.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -




More information about the R-help mailing list