| 1 |
loessDemo <- |
| 2 |
function(x, y, span = 1/2, degree = 1, family = c("gaussian", "symmetric"), |
| 3 |
nearest = FALSE, nout = 501, |
| 4 |
xlim = numeric(0), ylim = numeric(0), strictlim=TRUE, verbose = TRUE, |
| 5 |
inch.sym = 0.25, pch = 4, |
| 6 |
shade = TRUE, w.symbols = TRUE, |
| 7 |
sym.col = "blue", w.col = "light blue", line.col = "steelblue") |
| 8 |
{ |
| 9 |
## function to demonstrate the locally weighted regression function loess |
| 10 |
## written and posted to S-news, Thu, 27 Sep 2001 07:48 |
| 11 |
### Dr. Greg Snow |
| 12 |
## Brigham Young University, Department of Statistics |
| 13 |
## [email protected] |
| 14 |
## Modified by Henrik Aa. Nielsen, IMM, DTU ([email protected]) |
| 15 |
## spiffed up (R only), by M.Mächler, SfS ETH Zurich |
| 16 |
|
| 17 |
family <- match.arg(family) |
| 18 |
## drop NA's and sort: |
| 19 |
miss.xy <- is.na(x) | is.na(y) |
| 20 |
x <- x[!miss.xy] |
| 21 |
y <- y[!miss.xy] |
| 22 |
ix <- order(x) |
| 23 |
x <- x[ix] |
| 24 |
y <- y[ix] |
| 25 |
degree <- as.integer(degree) |
| 26 |
if(length(degree) != 1 || is.na(degree) || degree < 0 || 2 < degree) |
| 27 |
stop("'degree' must be in {0,1,2}") |
| 28 |
|
| 29 |
fit.D <- loess(y ~ x, degree = degree, span = span, family = family, |
| 30 |
control = loess.control(surface = "direct")) |
| 31 |
|
| 32 |
fit.I <- loess(y ~ x, degree = degree, span = span, family = family) |
| 33 |
|
| 34 |
xx <- seq(min(x), max(x), len = nout) |
| 35 |
est <- list(x = xx, |
| 36 |
y = predict(fit.I, newdata = data.frame(x = xx))) |
| 37 |
|
| 38 |
xl <- if(strictlim && is.numeric(xlim) && length(xlim) == 2) |
| 39 |
xlim |
| 40 |
else { |
| 41 |
xl <- range(x, est$x, xlim) |
| 42 |
xl <- xl + c(-1, 1) * 0.03 * diff(xl) |
| 43 |
} |
| 44 |
yl <- if(strictlim && is.numeric(ylim) && length(ylim) == 2) { |
| 45 |
dy <- 0.05 * diff(ylim) |
| 46 |
ylim |
| 47 |
} |
| 48 |
else { |
| 49 |
yl <- range(y, est$y, ylim, fitted(fit.D)) |
| 50 |
dy <- 0.05 * diff(yl) |
| 51 |
yl + c(-1, 1) * dy |
| 52 |
} |
| 53 |
## room below for weights |
| 54 |
dy <- 4*dy |
| 55 |
yl[1] <- yl[1] - dy |
| 56 |
stit <- paste("span = ", span,"; degree = ", degree) |
| 57 |
if(family != "gaussian") |
| 58 |
stit <- paste(stit,". family = \"", family,'"',sep="") |
| 59 |
|
| 60 |
fitPlot <- function(x, y, w, est, fit.D, xl, yl) |
| 61 |
{ |
| 62 |
pU <- par("usr") |
| 63 |
plot(x, y, pch = pch, xlim = xl, ylim = yl, sub = stit) |
| 64 |
if(!is.null(w)) { |
| 65 |
w <- w/max(w) # in [0,1] |
| 66 |
wP <- w > 1e-5 |
| 67 |
nw <- length(xw <- x[wP]) |
| 68 |
if(w.symbols) |
| 69 |
symbols(xw, y[wP], circles = sqrt(w[wP]), |
| 70 |
inches = inch.sym, add = TRUE, fg = sym.col) |
| 71 |
# scale [0,1] to yl[1] + [0, dy] : |
| 72 |
y0 <- pU[3] |
| 73 |
wy <- y0 + (dy+yl[1]-y0) * w[wP] |
| 74 |
polygon(c(xw[1], xw, xw[nw]), c(y0, wy, y0), col = w.col) |
| 75 |
segments(xw, rep(y0,nw), xw, wy, col=sym.col) |
| 76 |
} |
| 77 |
lines(x, fitted(fit.D), col = 2, lwd = 2) |
| 78 |
mtext("Exact estimate with linear interpolation between x-values ('surface = \"direct\")", |
| 79 |
col = 2, adj = 0.5, line = 0.5) |
| 80 |
lines(est, col = 3, lwd = 2) |
| 81 |
mtext("Estimate obtained using the default interpolation scheme", |
| 82 |
col = 3, adj = 0.5, line = 2) |
| 83 |
pU |
| 84 |
} |
| 85 |
|
| 86 |
fitPlot(x, y, w=NULL, est, fit.D, xl, yl) |
| 87 |
|
| 88 |
repeat { |
| 89 |
if(verbose) |
| 90 |
cat("click left for x0 to predict -- click right to stop ") |
| 91 |
x0 <- locator(1)$x |
| 92 |
if(verbose) cat("\n") |
| 93 |
if(length(x0) < 1)## right clicking leaves loop |
| 94 |
break |
| 95 |
if(nearest) |
| 96 |
x0 <- unique(x[abs(x - x0) == min(abs(x - x0))]) |
| 97 |
if(verbose) |
| 98 |
cat("x0 =", x0, "\n") |
| 99 |
Dx <- abs(x - x0) |
| 100 |
d <- |
| 101 |
if(span < 1) |
| 102 |
sort(Dx)[as.integer(span * length(x))] |
| 103 |
else max(Dx) * sqrt(span) |
| 104 |
w <- rep(0, length(x)) |
| 105 |
s <- Dx <= d |
| 106 |
w[s] <- (1 - (Dx[s]/d)^3)^3 # tricube weights |
| 107 |
pU <- fitPlot(x, y, w, est, fit.D, xl, yl) |
| 108 |
## ======= == |
| 109 |
|
| 110 |
if(degree > 0L) { ## is '1' or '2 |
| 111 |
if(degree == 1) |
| 112 |
abline(lm(y ~ x, weights = w), col = line.col) |
| 113 |
else ## (degree == 2) # predict(lm( ~ poly()) fails! |
| 114 |
lines(xx, predict(lm(y ~ x + I(x^2), weights = w), |
| 115 |
data.frame(x=xx)), |
| 116 |
col = line.col, err = -1) |
| 117 |
} else { ## degree == 0 |
| 118 |
##lines(x, fitted(lm(y ~ 1, weights = w)), col = line.col, err = -1) |
| 119 |
abline(a = sum(w*y)/sum(w), b = 0, col = line.col) |
| 120 |
} |
| 121 |
abline(v = x0, col = line.col, lty = 3, lwd = 0.2) |
| 122 |
axis(1, at= x0, labels = formatC(x0, digits=3), col.axis = line.col) |
| 123 |
if((x1 <- x0 - d) > xl[1]) { |
| 124 |
abline(v = x1, col = line.col, lty = 2) |
| 125 |
if(shade) polygon(c(pU[1],x1,x1,pU[1]), pU[c(3,3, 4,4)], density = 5) |
| 126 |
} |
| 127 |
if((x1 <- x0 + d) < xl[2]) { |
| 128 |
abline(v = x1, col = line.col, lty = 2) |
| 129 |
if(shade) |
| 130 |
polygon(c(x1, pU[c(2,2)],x1), pU[c(3,3, 4,4)], density = 5, |
| 131 |
angle = -45) |
| 132 |
} |
| 133 |
} |
| 134 |
} |