[R] Plotting lm() attributes
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Nov 13 08:40:27 CET 2003
On Wed, 12 Nov 2003, Tony Plate wrote:
> 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.)
More accurately, where a non-NA prediction could be produced. There never
was a fitted value for those cases, so NA is correct. That predict gives
what people expect with NA values is a new feature in 1.8.0.
And please, please, folks use the extractor functions and not the
components (NOT attributes) directly.
>
> 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
>
> ______________________________________________
> 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