[R] suggestion for example for base:naresid

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Aug 29 13:54:19 CEST 2001


On Wed, 29 Aug 2001, Markus Jantti wrote:

> Dear list:
>
> since it took me a little while to figure out how to make use of naresid, I thought that
> the below R code might be useful as an example on the help page.

Really it is for internal use only. End users should be using residuals().
It does say

     These are utility functions used to allow `predict' and `resid'
     methods for modelling functions to compensate for the removal of
     `NA's in the fitting process.  There are used by methods in

which is probably enough of a hint.  Anyone wanting to write such a method
would surely copy residuals.lm, say.


>
> Regards,
>
> Markus
>
>  # generate some data
> x1 <- runif(20)
> y <- 10 + 5*x1 + rnorm(20)
> summary(lm.0 <- lm(y ~ x1))
> # append some NA's to y
> y <- c(y, rep(NA, 5))
> # generate some further x1s
> x1 <- c(x1, runif(5))
> # this fills the residuals vector with NA's to the end
> summary(lm.1 <- lm(y ~ x1, na.action = na.exclude))
> residuals(lm.1) # is equal to
> naresid(na.exclude(lm.1), lm.1$residuals)
> tmp.omit <- lm.1$na.action # modify the na.action to omit
> class(tmp.omit) <- "omit"
> naresid(tmp.omit, lm.1$residuals)
> # same as residuals(lm.0)
> summary(lm.2 <- lm(y ~ x1))
> residuals(lm.2)
> tmp.omit <- lm.2$na.action # modify the na.action to be "exclude"
> class(tmp.omit) <- "exclude"
> naresid(tmp.omit, lm.2$residuals)

Please, that's underhand.  Don't alter classes on objects like that.
There is no guarantee that will continue to work (and actually it
won't now unless options(na.action) happens to be the default).

There is a set of examples in tests/nafns.R.


> --
> Markus Jantti
> markus.jantti at uta.fi ** http://www.abo.fi/~mjantti
> Dept of Economics ** University of Tampere ** FIN 33014 University of Tampere
> PGP public key: http://www.abo.fi/~mjantti/pubring.asc
>
>
>

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list