gpanel.fit: Panel functions for predicted values and SE bands

Description Usage Arguments Details Value Functions Author(s) Examples

Description

Panel functions for predicted values and SE bands using 'layer' and 'glayer' in the package latticeExtra

This is an experiment in writing a function that can be called via layer or glayer without further complications e.g. xyplot(......,labels = rownames(data)) + layer(gpanel.labels(...)) or xyplot(....., labels = rownames(data), subscripts = T) + glayer(gpanel.labels(...)). For selected labels see the examples with trellis.focus and panel.identify

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
gpanel.fit(x, y, fit, lower, upper, subscripts, ..., type, group.number, alpha,
  col, col.line, col.symbol, border = F, font, fontface)

panel.fit(x, y, fit, lower, upper, subscripts, ..., type, group.number, alpha,
  col, col.line, col.symbol, border = F, font, fontface)

gpanel.band(x, y, fit, lower, upper, subscripts, ..., type, group.number, alpha,
  col, col.line, col.symbol, border = F, font, fontface)

gpanel.labels(x, y, labels, subscripts, ...)

Arguments

x, y

position of labels, usually supplied through panel call

fit

fitted values of a model, generally passed through 'layer' from a call to 'xyplot': e.g. xyplot( y ~ x, data, groups = g, fit = data$yhat, lower = with(data, yhat - 2*se), upper = with(data, yhat + 2*se), subscripts = T)

...

NOTE: may specify anything you don't want passed through ...

col.symbol

is used to control color when using 'groups'

border

default = FALSE for panel.band.

labels

default is rownames of data

data

data frame to be used to add additional values of numeric variable

form

formula evaluated in data. The first term defines the variable with values to be filled in and the remaining terms define the variables to be used for grouping determining the minima and maxima within which values are added.

n

the number of values to be added between the global mininum and maximum. Values falling outside conditional minima and maxima are culled. Default 200.

xpd

expansion factor to add points beyond minima and maxima. Default 1.0.

labels

to display

Details

With 'layer' and 'glayer' in 'latticeExtra', these functions can be used to easily generate fitted values and confidence or prediction bands that have a reasonable appearance whether a plot uses 'groups' or not.

Value

The 'panel.bands', 'panel.fit', and 'panel.labels' functions are invoked for their graphical effect.

Functions

Author(s)

Georges Monette <georges@yorku.ca>

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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
## Not run: 
  library(yscs)
  library(latticeExtra)
  library(car)

  fit <- lm(prestige ~ (income + I(income^2)) * type, Prestige,
      na.action = na.exclude)
  pred <- cbind(Prestige, predict(fit, newdata = Prestige, se = TRUE))
  head(pred)
  (p <- xyplot( prestige ~ income , pred,  groups = type,
                subscripts = T,
                fit = pred$fit,
                lower = with(pred, fit - 2*se.fit),
                upper = with(pred, fit + 2*se.fit)))
  p + glayer(gpanel.fit(...))

  ###
  ### Use 'fillin' to add points in sparse regions of the predictor
  ### to produce smoother bands
  ###

  fit <- lm( income ~
        (education+I(education^2)+I(education^3) +I(education^4))* type,
        Prestige, na.action = na.exclude)    # overfitting!
  # adding extra values of predictor to get smooth line
  Prestige$occupation <- rownames(Prestige)
  z <- fillin(Prestige, ~education + type, xpd = 1.1)

  dim(z)
  dim(Prestige)
  z <- cbind(z, predict(fit, newdata = z, se = TRUE))
  head(z)
  gd(3,cex=2,lwd=2, alpha = .7)
  (p <-  xyplot( income ~ education, z, groups = type,
                 subscripts = T,
                 fit = z$fit,
                 lower = z$fit - z$se,
                 upper = z$fit + z$se,
                 auto.key = list(space='right', lines= T)))
  p + glayer( gpanel.fit(...))
  p + glayer( gpanel.fit(...,  alpha = .1))
  # Using spida:gd() to get a ggplot-like appearance
  gd(3,lty=1,lwd=2)
  p + glayer( gpanel.fit(...,alpha = .1))

  ###
  ###  Using gpanel.fit with no groups
  ###

  (p <-  xyplot( income ~ education| type, z,
                 subscripts = T,
                 fit = z$fit,
                 lower = z$fit - z$se,
                 upper = z$fit + z$se,
                 auto.key = list(space='right', lines= T)))
  p + layer( panel.fit(...))

  # gd_(basecol = 'tomato4')  # Use 'gd_' to set parameters without groups
  gd_(base = 'tomato4')  # Use 'gd_' to set parameters without groups
  p + layer( panel.fit(...))
  p + layer( panel.fit(...,  col = 'grey10'))

  ###
  ### With panels and groups
  ###

  z <- Prestige
  z$gender <- with(z, cut( women, c(-1,15,50,101),labels = c("Male","Mixed","Female")))
  tab(z, ~ gender + type)
  z <- fillin( z, ~ education + type + gender, xpd = 1.1)
  fit <- lm( income ~ (education+I(education^2)+I(education^3) )* type * gender,
             z, na.action = na.exclude)    # overfitting!
  summary(fit)
  z <- cbind( z, predict(fit, newdata = z, se = TRUE))
  head(z)
  (p <-  xyplot( income ~ education| gender, z, groups = type,
                 subscripts = T,
                 fit = z$fit,
                 lower = z$fit - z$se,
                 upper = z$fit + z$se,
                 layout = c(1,3),
                 auto.key = list(space='right', lines= T, cex = 1.5)))

  p + glayer(gpanel.fit(...))
  trellis.focus()
  panel.identify(labels= z$occupation)
  trellis.unfocus()
  z$type2 <- with( z, reorder(type,education, mean, na.rm=T))
  gd(3)
  (p <-  xyplot( income ~ education| type2, z, groups = gender,
                 subscripts = T,
                 fit = z$fit,
                 lower = z$fit - z$se,
                 upper = z$fit + z$se,
                 layout = c(1,3),
                 par.strip.text = list(cex = 2),
                 auto.key = list(space='right', lines= T, cex = 1.5)))

  p + glayer( gpanel.fit(...))
  trellis.focus()
  panel.identify(labels= z$occupation)
  trellis.unfocus()

  ###
  ### With panels^2
  ### need to remove 'col = col.line'
  ###

  z <- Prestige
  z$occ <- rownames(Prestige)
  z$gender <- with(z, cut( women, c(-1,15,50,101),labels = c("Male","Mixed","Female")))
  z$type2 <- with( z, reorder(type,education, mean, na.rm=T))
  tab(z, ~ gender + type2)
  z <- fillin( z, ~ education + type + gender, xpd = 1.1)
  fit <- lm( income ~ (education+I(education^2)+I(education^3) )* type * gender,
             z, na.action = na.exclude)    # overfitting!
  summary(fit)
  z <- cbind( z, predict(fit, newdata = z, se = TRUE))
  head(z)
  (p <-  xyplot( income ~ education| gender*type, z,
                 subscripts = T,
                 fit = z$fit,
                 labels = z$occ,
                 lower = z$fit - z$se,
                 upper = z$fit + z$se,
                 auto.key = list(space='right', lines= T, cex = 1.5)))

  p + layer( gpanel.fit(...))
  p + layer( gpanel.fit(..., col = 'black', alpha = .1)) + layer(gpanel.labels(...))

## End(Not run)
## Not run: 
trellis.focus()
panel.identify(labels = rownames(data),rot=-15,col = col.symbol, etc.)
trellis.unfocus()

## End(Not run)

gmonette/yscs documentation built on May 17, 2019, 7:28 a.m.