[R] residuals from lowess fit

Thomas Lumley tlumley at u.washington.edu
Tue Mar 13 00:31:41 CET 2001


On Tue, 13 Mar 2001, S.McClatchie wrote:

> Colleagues
>
> ----------------------------------
> System info:
> R version rw1022 on NT
> ESS v. 5.1.18 using emacs ver. 20.4
>
> ----------------------------------
> I would like to access the residuals from a lowess fit (function lowess() )
> as an aid to assessing whether I am over-smoothing or under-smoothing
> a dataset (as in Cleveland's book "Visualizing data", section 3.3)
>
> Unless I misunderstand, the lowess function only returns the independent
> variable and the smoothed dependent variable, without residuals.
>
> e.g.
> > z <- lowess(....)
> > names(z)
> [1] "x" "y"
>
> I think from a look at the lowess() function that the coding where
> residuals are calculated is in C?
>
> > lowess
> function (x, y = NULL, f = 2/3, iter = 3, delta = 0.01 *
> diff(range(xy$x[o])))
> {
>     xy <- xy.coords(x, y)
>     if (length(xy$x) != length(xy$y))
>         stop("x and y lengths differ")
>     n <- length(xy$x)
>     o <- order(xy$x)
>     .C("lowess", x = as.double(xy$x[o]), as.double(xy$y[o]),
>         n, as.double(f), as.integer(iter), as.double(delta),
>         y = double(n), double(n), double(n), PACKAGE = "base")[c("x",
>         "y")]
> }
>
> Is there any way for me to get the residuals out?
>

You could just subtract the fitted value from the observed value

l<-lowess(x,y)
r<-y[order(x)]-l$y


or you could use loess() in the modreg package

library(modreg)
r<-residuals(loess(y~x))

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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