panel.cbH: Lattice panels for error bars and envelop bands

Description Usage Arguments Value Author(s) Examples

View source: R/panel.cbH.R

Description

Used to plot confidence bars or confidence bands in lattice plots.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
panel.cbH(
  x,
  y,
  ly,
  uy,
  subscripts,
  cty,
  col.line = plot.line$col,
  lwd = plot.line$lwd,
  desloc = NULL,
  fill = 1,
  alpha = 0.1,
  length = 0.05,
  ...
)

prepanel.cbH(y, ly, uy, subscripts)

Arguments

x, subscripts, col.line, lwd, ...

arguments passed to xyplot().

y

central value (point estimate).

ly

lower limit.

uy

upper limit.

cty

string that is the confidence type. Values current acepted are "bars" for error bars (confidence intervals) and "bands" for confidence/envelop bands.

desloc

a numeric vetor with length equal to y used to desloc vertically the values to avoid overlapping with points or others factor levels intervals.

fill

a color to fill the polygon defined by the confidence bands. Default is 1 that is the black color. Not used when cty = "bars".

alpha

transparecy level for the polygon defined by the confidence bands. Default is 0.1. Not used when cty = "bars".

length

is the length of the upper/lower error bars whiskers. Default is 0.05. Not used when cty = "bands".

Value

None is returned.

Author(s)

Walmes Zeviani, walmes@ufpr.br, based on the R-help mail list.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
library(lattice)
library(latticeExtra)

#--------------------------------------------

m0 <- lm(sqrt(dist) ~ speed, data = cars)
pred <- with(cars,
             data.frame(speed = seq(min(speed), max(speed),
                                    length.out = 20)))
aux <- predict(m0, newdata = pred, interval = "confidence")
pred <- cbind(pred, aux)

xyplot(sqrt(dist) ~ speed, data = cars,
       ylab = expression(sqrt(distance)),
       xlab = "Speed") +
    as.layer(xyplot(fit ~ speed, data = pred, type = "l",
                    ly = pred$lwr, uy = pred$upr,
                    cty = "bands", fill = "blue", alpha = 0.3,
                    prepanel = prepanel.cbH,
                    panel = panel.cbH))

#--------------------------------------------

m1 <- lm(weight ~ feed, data = chickwts)
pred <- with(chickwts, data.frame(feed = levels(feed)))
aux <- predict(m1, newdata = pred, interval = "confidence")
pred <- cbind(pred, aux)

xyplot(weight ~ feed, data = chickwts,
       xlab = "Feed", ylab = "Weight") +
    as.layer(xyplot(fit ~ feed, data = pred,
                    ly = pred$lwr, uy = pred$upr, cty = "bars",
                    desloc = rep(0.15, length(pred$fit)),
                    prepanel = prepanel.cbH,
                    panel = panel.cbH))

#-----------------------------------------------------------------------

set.seed(123)
da <- expand.grid(trt = gl(2, 1), x = 1:7)
da$y <- with(da, as.integer(trt) + 0.5 * x + rnorm(x, 0, 0.3))
xyplot(y ~ x, groups = trt, data = da)

m2 <- lm(y ~ trt + x, data = da)

pred <- with(da,
             expand.grid(trt = levels(trt),
                         x = seq(min(x), max(x), length.out = 20)))
aux <- predict(m2, newdata = pred, interval = "confidence")
pred <- cbind(pred, aux)

xyplot(y ~ x, groups = trt, data = da) +
    as.layer(xyplot(fit ~ x, groups = trt, data = pred, type = "l",
                    ly = pred$lwr, uy = pred$upr,
                    cty = "bands", alpha = 0.75,
                    prepanel = prepanel.cbH,
                    panel = panel.superpose,
                    panel.groups = panel.cbH),
             under = TRUE)

walmes/wzRfun documentation built on Aug. 10, 2021, 2:19 p.m.