Debian Codegraph @

R/p.res.2x.WSt.R

raw
1 #### was part of ./p.goodies.R
2
3 ### Exports :
4
5 ### p.res.2x Werner Stahels Plot; z.B Residuen gegen 2 x-Var.
6 ### p.res.2fact Aehnliche Idee: Residuen gegen 2 Faktoren (boxplots)
7
8 ## p.wstPlot <- function(...)
9 ## {
10 ## warning("\n\n*** p.wstPlot(.) heisst neu p.res.2x(.)\n** Diese verwenden!\n")
11 ## p.res.2x(...)
12 ## }
13
14 p.res.2x <- function(x, ...) UseMethod("p.res.2x")
15
16 p.res.2x.default <-
17 function(x, y, z, restricted = NULL, size = 1, slwd = 1, scol = 2:3,
18 xlab = NULL, ylab = NULL, main = NULL,
19 xlim = range(x), ylim = range(y), ...)
20 {
21 ## Purpose: Stahels Residuen-Plot
22 ## Author: ARu , Date: 11/Jun/91
23 ## Aenderungen: MMae, 30/Jan/92, Dez.94 --> help(p.res.2x)
24 if(is.null(xlab)) xlab <- deparse(substitute(x))
25 if(is.null(ylab)) ylab <- deparse(substitute(y))
26 if(is.null(main)) main <- deparse(substitute(z))
27
28 ok <- !(is.na(x) | is.na(y) | is.na(z))
29 x <- x[ok]; y <- y[ok]; z <- z[ok]
30 ##
31 ##--- restrict z values: ---
32 az <- abs(z)
33 has.restr <-
34 if(is.null(restricted)) FALSE else any(restr <- az > restricted)
35 if(has.restr) {
36 z[z > restricted] <- restricted
37 z[z < - restricted] <- - restricted
38 }
39
40 ##--- fix plot region: ---
41 pcm <- par("pin") * 2.54 #damit in cm
42 ##--- damit im Plot das Symbol wirklich die Groesse size hat:
43 size <- size/(2 * sqrt(2))
44 fx <- (size * diff(xlim))/(pcm[1] - 2 * size)/2
45 fy <- (size * diff(ylim))/(pcm[2] - 2 * size)/2
46 ##--
47 plot(x, y, xlim = xlim + c(-1,1)* fx, ylim = ylim + c(-1,1)* fy, pch = ".",
48 xlab = xlab, ylab = ylab, main = main, ...)
49
50 ##--- draw symbols: ---
51 z <- z/max(az, na.rm = TRUE)
52 usr <- par("usr")
53 sxz <- diff(usr[1:2])/pcm[1] * size * z
54 syz <- abs(diff(usr[3:4])/pcm[2] * size * z)
55 if(length(scol) == 2) scol <- scol[1 + as.integer(z < 0)]
56 segments(x - sxz, y - syz, x + sxz, y + syz, lwd = slwd, col = scol)
57
58 ##--- mark restricted observations: ---
59 if(has.restr) {
60 points((x - sxz)[restr], (y - syz)[restr], pch = 8, mkh = 1/40)
61 points((x + sxz)[restr], (y + syz)[restr], pch = 8, mkh = 1/40)
62 }
63 invisible()
64 }
65
66 ## graphics:::mosaicplot.formula as an example
67 p.res.2x.formula <- function(x = ~., data,
68 main = deparse(substitute(data)),
69 xlab = NULL, ylab = NULL, ...)
70 {
71 ## Purpose: plot residuals vs. two x's
72 ## Author: ARu , Date: 11/Jun/91
73 ## Aenderungen: MMae, 30/Jan/92, Dez.94 / WSt
74 ## --------------------------------------------------------------------------
75 ## Arguments:
76 ## x formula defining the variables zu be used, either
77 ## z ~ x + y
78 ## ~ x + y in this case, data must inherit from lm ,
79 ## and the residuals of data will be used as z .
80 ## data a data.frame or an lm or aov object.
81 ## In the latter case, p.res.2x will look for the data
82 ## that was used to fit the model.
83 ## restricted absolute value which truncates the size.
84 ## The corresponding symbols are marked by stars.
85 ## size the symbols are scaled so that 'size' is the size of
86 ## the largest symbol in cm.
87 ## slwd, scol line width and color to be used for the symbols
88 ## ... additional arguments for the S-function 'plot'
89 ## EXAMPLE :
90 ## g.res2x(zz~.,data=data.frame(xx=rep(1:10,7),yy=rep(1:7, rep(10,7)),
91 ## zz=rnorm(70)), restr = 2, main = "i.i.d. N(0,1) random residuals")
92 ## --------------------------------------------------------------------------
93 if(miss.main <- missing(main))
94 force(main)
95 formula <- as.formula(x)
96 t.d <- if(inherits(data, "lm")) {
97 if(miss.main) main <- paste0("residuals(", main, ")")
98 if(!is.data.frame(t.d <- data$model)) {
99 ## try to look for the data that was used to fit the model.
100 cl <- data$call
101 i <- if("data" %in% names(cl)) "data" else 3 # try ..
102 t.d <- get(as.character(cl[[i]]))
103 }
104 if (length(formula) < 3) {
105 if(identical(format(formula), "~.")) formula <- formula(data)
106 formula <- update.formula(formula, residuals ~ .)
107 ## formula <- substitute(residuals ~ RHS, list(RHS = formula[[2]]))
108 cbind(t.d, residuals = residuals(data))
109 } else t.d
110 } else
111 data
112 if (!is.data.frame(t.d)) {
113 if(is.matrix(data)) data <- as.data.frame(data) else
114 stop("data is not a data frame or 'lm' object with 'model' or existing data")
115 }
116 t.d <- na.omit(model.frame(formula, t.d))
117 z <- t.d[,1]
118 x <- t.d[,2]; if(is.null(xlab)) xlab <- names(t.d)[2]
119 y <- t.d[,3]; if(is.null(ylab)) ylab <- names(t.d)[3]
120 if(is.factor(x) && is.factor(y))
121 p.res.2fact(x, y, z, main=main, xlab=xlab, ylab=ylab, ...)
122 else {
123 x <- as.numeric(t.d[,2])
124 y <- as.numeric(t.d[,3])
125 p.res.2x.default(x,y,z, main=main, xlab=xlab, ylab=ylab, ...)
126 }
127 }
128
129 p.res.2fact <-
130 function(x, y, z, restricted, notch = FALSE,
131 xlab = NULL, ylab = NULL, main = NULL)
132 {
133 if(is.null(xlab)) xlab <- deparse(substitute(x))
134 if(is.null(ylab)) ylab <- deparse(substitute(y))
135 if(is.null(main)) main <- deparse(substitute(z))
136
137 ok <- !(is.na(x) | is.na(y) | is.na(z))
138 x <- x[ok]; y <- y[ok]; z <- z[ok]
139 x <- as.factor(x)
140 y <- as.factor(y)
141 lx <- levels(x); ly <- levels(y)
142
143 ##--- restrict z values: ---
144 if(missing(restricted)) restr <- FALSE
145 else {
146 if(!is.numeric(restricted) || restricted <= 0)
147 stop("'restricted' must be POSITIVE !")
148 if(any(restr <- abs(z) > restricted)) {
149 zorig <- z
150 z[z > restricted] <- restricted
151 z[z < -restricted] <- - restricted
152 }
153 }
154 rz <- range(z)
155 op <- par(mfrow = c(length(ly), 1), oma = c(5,6,6,0), mar = .1 + c(2,4,0,1))
156 on.exit(par(op))
157 for (yv in rev(ly)) {
158 Ind <- y == yv
159 plot (x[Ind], z[Ind], ylim = rz, xlab = "", ylab = yv, notch = notch)
160 abline(h = 0, lty = 3, lwd = 0)
161 if(any(II <- restr & Ind)) {
162 ## boxplot creates a coord.system with x = [-4, 104]
163 jx <- as.numeric(x[II]) #-- in 1:length(lx)..
164 cat("..Cut z=",format(zorig[II])," at ",
165 xlab,"=",x[II],", ", ylab, "=",yv,"\n")
166 points( u.boxplot.x(length(lx),jx) , z[II]*1.02, pch = 8, mkh = 1/25)
167 }
168 }
169 mtext (xlab, side = 1, line = 1, outer = TRUE, cex = 1.3)
170 mtext (ylab, side = 2, line = 3, outer = TRUE, cex = 1.3)
171 mtext (main, side = 3, line = 2, cex = 1.5, outer = TRUE)
172 if(any(restr)) message(sum(restr), " restricted observation(s)")
173 invisible()
174 }
175
176
177 ## Not sure if I want this (as global function).
178 ## I had eliminated it long ago (from "SfS") but it's used above:
179
180 u.boxplot.x <- function(n, j = 1:n, fullrange = 100)
181 {
182 ## Purpose: Return the j-th x-coordinates in an 'n' side-by-side boxplot
183 ## -------------------------------------------------------------------------
184 ## Arguments: n : number of boxplots; j: indices of boxplots
185 ## fullrange: x-coords as 'uniform' in [0,fullrange] (f.=100, Splus 3.1,3.2)
186 ## -------------------------------------------------------------------------
187 ## Author: Martin Maechler, Date: 19 Jan 95, 17:57
188 cn <- fullrange/(3*n*(n+1))
189 Dn <- cn*(3*n+2) ## Delta_{n}
190 an <- cn*(2*n+1) ## a_{n}
191 ## x(j) = an + (j-1)*Dn :
192 an + (j-1)*Dn
193 }
194