[R] predict/residual

michaell taylor mt at michaelltaylor.com
Sat Apr 26 14:11:15 CEST 2003


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

>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



More information about the R-help mailing list