[R] Plotting lm() attributes
Tony Plate
tplate at acm.org
Wed Nov 12 23:59:00 CET 2003
I believe this is the sort of things that the functions resid() and
predict(), in conjunction with na.exclude, are designed for. E.g.:
> data <- data.frame(x=c(1:5), y=c(1,3,2,NA,4))
> m <- lm(y~x, data=data, na.action=na.exclude)
> predict(m)
1 2 3 4 5
1.400000 2.028571 2.657143 NA 3.914286
> resid(m)
1 2 3 4 5
-0.40000000 0.97142857 -0.65714286 NA 0.08571429
>
Note that NA's are not reintroduced if na.action=na.omit, which is the
default (unless you have options("na.action") set otherwise). Also, note
that this technique produces a NA fitted value where a non-NA one could be
produced (use predict(m, newdata=data) to get those values.)
hope this helps,
Tony Plate
At Thursday 11:40 AM 11/13/2003 +1300, Murray Jorgensen wrote:
>Suppose you fit a linear model
>
> > model.1 ~ lm(v1 ~ ..., data=myframe)
>
>and v2 is some other column of myframe typically not in the model. You
>will often want to try
>
> > plot(v2, model.1$residuals)
>
>but this will fail if there are NAs in the response v1 as
>model.1$residuals has length equal to the number of nonmissing values
>in v1. I suppose
>
> > plot(v2[!is.na(v1)], model.1$residuals)
>
>does the job, but it seems irritating that model.1$residuals, does not
>have length agreeing with the number of rows in the data frame. It would
>be even more irritating for model.1$fitted.values, where the removed
>elements would often have nonmissing values.
>
>Murray
>
>--
>Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html
>Department of Statistics, University of Waikato, Hamilton, New Zealand
>Email: maj at waikato.ac.nz Fax 7 838 4155
>Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Tony Plate tplate at acm.org
More information about the R-help
mailing list