[R] predict/residual

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Apr 26 15:03:08 CEST 2003


On 26 Apr 2003, michaell taylor wrote:

> I am having trouble understanding the way naresid is used when using
> predict/residual in a new data frame. 

[From the example, for an lm fit, and for predict: you cannot use 
`residual in a new data frame'.]

It isn't: what makes you think otherwise?  naresid is *never* used for 
prediction, and napredict is only used for predictions on the fitted data 
set.

I think you want to set na.action=na.pass whilst making your predictions,
which you will have to do via options() as predict.lm uses the default, 
na.omit.  (I would call that a design error in predict.lm: some other 
predict methods handle this better.)

BTW `_' is stringly deprecated, and defunct in R-devel.

> 
> >From the example below, the NAs are displayed when predicting inside the
> original frame, but are dropped when applied to a new frame.
> 
> ultimately I need to cbind the residuals and predictions to a dataframe.
> Any help would be appreciated. Thanks in advance.
> 
> > a_data.frame(y=rnorm(10),x=rnorm(10))
> > m<- lm(y~x,data=a,na.action=na.exclude)
> > a_data.frame(y=rnorm(10),x=rnorm(10))
> 
> 
> # CREATE A MISSING VALUE
> > a[5,2]<-NA
> > m<- lm(y~x,data=a,na.action=na.exclude)
> > predict(m)
>          1          2          3          4          5         
> 6          7 
>  0.9152478  1.5665630  1.1444221  0.4781361         NA  0.1659269 
> 0.6856764 
>          8          9         10 
>  1.5615913 -0.1687372  0.4164926 
> 
> # MAKE A COPY OF A, THEN CHANGE A COUPLE OF VALUES
> > b<-a
> > b[1,2]<-NA
> > b[3,2] <-100 # just to make sure the correct frame is used
> > predict(m,b)
>           2           3           4           6           7           8 
>   1.5665630 -44.9938287   0.4781361   0.1659269   0.6856764   1.5615913 
>           9          10 
>  -0.1687372   0.4164926 
> > 
> #  THE PROBLEM IS OF COURSE THAT I NO LONGER HAVE APPROPRIATE MISSING
> VALUES IN THE VECTOR - obs 1 and 5 are no-shows.
> 
> 
> Michaell
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list