~ubuntu-branches/ubuntu/oneiric/latticeextra/oneiric

« back to all changes in this revision

Viewing changes to R/panel.smoother.R

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2010-01-17 13:08:47 UTC
  • mto: (9.1.1 sid) (1.4.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20100117130847-ap3rv0zm5r82599e
Tags: upstream-0.6-5
ImportĀ upstreamĀ versionĀ 0.6-5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
## based on the stat_smooth() function in ggplot2 package.
 
3
 
 
4
panel.smoother <-
 
5
    function(x, y, form = y ~ x, method = "loess", ...,
 
6
             se = TRUE, level = 0.95, n = 100,
 
7
             col = plot.line$col, col.se = col,
 
8
             lty = plot.line$lty, lwd = plot.line$lwd,
 
9
             alpha = plot.line$alpha,
 
10
             alpha.se = 0.25, border = NA,
 
11
             ## ignored (do not pass to method()):
 
12
             subscripts, group.number, group.value,
 
13
             type, col.line, col.symbol, fill,
 
14
             pch, cex, font, fontface, fontfamily)
 
15
{
 
16
    plot.line <- trellis.par.get("plot.line")
 
17
    if (all(is.na(col)) && !missing(col.line))
 
18
        col <- col.line
 
19
    ## allow 'form' to be passed as the first argument
 
20
    missing.x <- missing(x)
 
21
    if (!missing.x && inherits(x, "formula")) {
 
22
        form <- x
 
23
        missing.x <- TRUE
 
24
    }
 
25
    ## use 'x' and 'y' if given
 
26
    ## otherwise try to find them in the formula environment
 
27
    if (missing.x)
 
28
        x <- environment(form)$x
 
29
    if (missing(y))
 
30
        y <- environment(form)$y
 
31
    data <- list(x = x, y = y)
 
32
    mod <- do.call(method,
 
33
                   c(alist(form, data = data), list(...)))
 
34
    ## use the limits of the data, or panel limits, whichever is smaller
 
35
    lims <- current.panel.limits()
 
36
    xrange <- c(max(min(lims$x), min(x)), min(max(lims$x), max(x)))
 
37
    xseq <- seq(xrange[1], xrange[2], length = n)
 
38
    pred <- predict(mod, data.frame(x = xseq), se = se)
 
39
    if (se) {
 
40
        std <- qnorm(level/2 + 0.5)
 
41
        panel.polygon(x = c(xseq, rev(xseq)),
 
42
                      y = c(pred$fit - std * pred$se,
 
43
                      rev(pred$fit + std * pred$se)),
 
44
                      col = col.se, alpha = alpha.se, border = border)
 
45
        pred <- pred$fit
 
46
    }
 
47
    panel.lines(xseq, pred, col = col, alpha = alpha,
 
48
                lty = lty, lwd = lwd)
 
49
}