[R] residuals from lowess fit

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Mar 13 00:46:20 CET 2001


"S.McClatchie" <s.mcclatchie at niwa.cri.nz> writes:

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

Sure, the fitted values are in the $y component of the result,
although sorted by the x values, so you need something like

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

but more expediently

library(modreg)
y - predict(loess(y~x))

(but note that loess and lowess have different defaults...)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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