R/boot.lowess.R

Defines functions boot.lowess

Documented in boot.lowess

#' Bootstrapping with LOWESS
#'
#' Sample the original data with replacement and fit LOWESS curves accordingly.
#'
#' We keep on resampling the data and finally we will see several bootstrapped
#' LOWESS curves, which may give us a rough idea about a ``confidence interval''
#' of the LOWESS fit.
#' @param x,y,f,iter passed to \code{\link{lowess}}
#' @param line.col the color of the LOWESS lines
#' @param ... other arguments passed to the scatterplot by \code{\link{plot}}
#' @return NULL
#' @author Yihui Xie
#' @references Examples at \url{https://yihui.org/animation/example/boot-lowess/}
#' @export
boot.lowess = function(x, y = NULL, f = 2/3, iter = 3, line.col = '#FF000033', ...) {
  if (missing(x)) {
    x = datasets::cars[, 1]
    y = datasets::cars[, 2]
  }
  if (NCOL(x) > 1) {
    y = x[, 2]
    x = x[, 1]
  }
  idx = replicate(ani.options('nmax'), sample(length(x), replace = TRUE))
  for (i in 1:ncol(idx)) {
    dev.hold()
    plot(x, y, ...)
    sunflowerplot(x[idx[, i]], y[idx[, i]], add = TRUE)
    apply(idx[, 1:i, drop = FALSE], 2, function(j) {
      lines(lowess(x[j], y[j], f = f, iter = iter), col = line.col)
    })
    ani.pause()
  }
}

Try the animation package in your browser

Any scripts or data that you put into this service are public.

animation documentation built on Oct. 7, 2021, 9:18 a.m.