many ways to do this. here is one example:
# toy data
dat <- data.frame(replicate(2,rnorm(10)));
dat$X1[c(2,7)] <- NA; dat
# fit mod and fill in resids with NAs
out <- lm(X1~X2,data=dat);
dat$residNA <- NA;
dat$residNA[as.numeric(names(out$resid))] <- out$resid; dat
in general I would avoid stunts like this because it's a bit error prone.
Better: first trim the data to a working dataset that only contains your
clean non-missing obs. used for the estimations. Switching samples for each
model can be dangerous.
From: gov2001-l-bounces at
lists.fas.harvard.edu
[mailto:gov2001-l-bounces at
lists.fas.harvard.edu] On Behalf Of Laurence Tai
Sent: Saturday, March 29, 2008 10:16 AM
To: gov2001-l at
lists.fas.harvard.edu
Subject: [gov2001-l] including NAs in a residual vector
Hello,
?? Suppose I run an lm model on data with NAs. It won't (and shouldn't)
produce residuals for entries that are excluded or that already have NAs in
them. The residual vector skips the numbers for these entries. How could I
produce a vector of residuals that includes NAs in the spots that have been
skipped?
Thanks,
Laurence