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?> lowessfunction (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? Help will be appreciated. Thanks Sam Sam McClatchie, Research scientist (fisheries acoustics)))))))))) NIWA (National Institute of Water & Atmospheric Research Ltd) PO Box 14 901, Kilbirnie, Wellington, New Zealand s.mcclatchie at niwa.cri.nz /\ ...>><xX(?> // \\ /// \\\ //// \\\\ /// <?)Xx><< ///// \\\\\\ ><(((?> >><(((?> ...>><xX(?>O<?)Xx><< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._