| 1 |
n.plot <- |
| 2 |
function(x, y=NULL, nam = NULL, abbr = n >= 20 || max(nchar(nam))>=8, |
| 3 |
type = "n", xlab = NULL, ylab = NULL, log = "", |
| 4 |
cex = par("cex"), col = par("col"), pt.col = par("col"), ...) |
| 5 |
{ |
| 6 |
## Purpose: "Name Plot"; Names (or numbers) instead of points in plot(..) |
| 7 |
## --> help(n.plot) ! |
| 8 |
if(inherits(x,"formula")) # is(x, "formula") |
| 9 |
stop("formula not yet supported") |
| 10 |
## this is like plot.default(): |
| 11 |
xlabel <- if (!missing(x)) deparse(substitute(x)) |
| 12 |
ylabel <- if (!missing(y)) deparse(substitute(y)) |
| 13 |
xy <- xy.coords(x, y, xlabel, ylabel, log) |
| 14 |
xlab <- xlab %||% xy$xlab |
| 15 |
ylab <- ylab %||% xy$ylab |
| 16 |
plot(xy, type = type, xlab = xlab, ylab = ylab, log = log, col = pt.col, ...) |
| 17 |
n <- length(x) |
| 18 |
## Use "any names", otherwise take 1,2,.. : |
| 19 |
nam <- nam %||% rownames(x) %||% names(x) %||% names(y) %||% as.character(seq_len(n)) |
| 20 |
if(abbr) nam <- abbreviate(nam, minlength=1) |
| 21 |
text(xy, labels=nam, cex=cex, col=col) |
| 22 |
invisible(nam) |
| 23 |
} |
| 24 |
|
| 25 |
TA.plot <- |
| 26 |
function(lm.res, fit = fitted(lm.res), |
| 27 |
res = residuals(lm.res, type = "pearson"), |
| 28 |
labels = NULL, main = mk.main(), xlab = "Fitted values", |
| 29 |
draw.smooth = n >= 10, show.call = TRUE, show.2sigma = TRUE, |
| 30 |
lo.iter = NULL, lo.cex = NULL, |
| 31 |
par0line = list(lty = 2, col = "gray"), |
| 32 |
parSmooth = list(lwd = 1.5, lty = 4, col = 2), |
| 33 |
parSigma = list(lwd = 1.2, lty = 3, col = 4), |
| 34 |
verbose = FALSE, ...) |
| 35 |
{ |
| 36 |
## Purpose: Produce a Tukey-Anscombe plot of a linear model fit |
| 37 |
## Note that residuals and fitted are UN-correlated (IFF intercept..) |
| 38 |
## ------------------------------------------------------------------------- |
| 39 |
## Arguments: lm.res = RESult of lm(..) |
| 40 |
## res : (weighted) residuals by default, |
| 41 |
## labels = 'symbols' for point, default(NULL): extract names or use seq.nr |
| 42 |
## use '*' to get simple '*' symbols. |
| 43 |
## |
| 44 |
## --- see on-line help by "?TA.plot" !! |
| 45 |
## ------------------------------------------------------------------------- |
| 46 |
## Uses : n.plot(.) |
| 47 |
## ------------------------------------------------------------------------- |
| 48 |
## Author: Martin Maechler, Date: Dec 92 / Nov.93; for R: 1999/2000 |
| 49 |
if(missing(main)) { |
| 50 |
call <- getCall(lm.res) |
| 51 |
if(is.call(call[["formula"]]) && any(c("lm", "aov") == call[[1]])) |
| 52 |
call <- call[["formula"]] |
| 53 |
else { #-- only formula part; no extra 'data =' |
| 54 |
if (length(call) >= 3 && !is.na(m.f <- match("formula", names(call)))) { |
| 55 |
call <- call[c(1, m.f)] |
| 56 |
names(call)[2] <- "" |
| 57 |
} |
| 58 |
} |
| 59 |
mk.main <- function() { |
| 60 |
cal <- call |
| 61 |
if(is.null(cal)) |
| 62 |
"Tukey-Anscombe plot of ???" |
| 63 |
else { |
| 64 |
nc <- nchar(ccal <- deparse(cal, width.cutoff = 200)[1]) |
| 65 |
if(verbose) |
| 66 |
cat("|cal|=", length(cal), "; nchar(ccal) =", nc,": '", ccal, "'\n", sep="") |
| 67 |
if(nc > 36) |
| 68 |
warning("TA.plot: 'main' title is long; consider using cex.main = 0.8", |
| 69 |
call. = FALSE) |
| 70 |
##-- now should even go further: |
| 71 |
##-- E.g. if nc > 50, use cex = .8 in the call to n.plot below |
| 72 |
paste(if(nc < 13) "Tukey-Anscombe plot of : " |
| 73 |
else if(nc < 24) "T.A. plot of: " else "TA-pl:", ccal) |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
if("ylab" %in% names(list(...))) { |
| 78 |
n.plot(fit, res, nam = labels, xlab = xlab, main = main, ...) |
| 79 |
} else { |
| 80 |
yl <- "Residuals" |
| 81 |
## FIXME $weights, $resid -- not portable (e.g., to lmer obj!) ==> use extractors? |
| 82 |
if(is.list(lm.res) && !is.null(lm.res$weights) && is.numeric(res0 <- lm.res$resid) && |
| 83 |
any(abs(res0 - res) > 1e-6*mad(res))) |
| 84 |
yl <- paste("WEIGHTED", yl) |
| 85 |
n.plot(fit, res, nam = labels, xlab = xlab, ylab = yl, main = main, ...) |
| 86 |
} |
| 87 |
if(show.call) |
| 88 |
mtext(deparse(match.call()), side = 3, line = 0.5, cex = 0.6, adj=1) |
| 89 |
do.call("abline", c(list(h= 0), par0line)) |
| 90 |
p.mgp <- par("mgp")[1:2] #-- line numbers of margin text: xlab & label |
| 91 |
if(missing(lo.cex)) |
| 92 |
lo.cex <- max(.2, min(0.8*par("mex"), .9*-diff(p.mgp))/par("mfg")[4]) |
| 93 |
m.line <- if(par("mfg")[4]==1) .2+ p.mgp[1] else |
| 94 |
max(p.mgp[1] - .2*lo.cex, sum(p.mgp)/2) |
| 95 |
if(show.2sigma) { |
| 96 |
s2 <- c(-2,2) * mad(res, center=0, na.rm=TRUE) # NA's possible in residuals |
| 97 |
rr <- range(res, na.rm=TRUE) |
| 98 |
if(s2[1] < rr[1] || s2[2] > rr[2]) |
| 99 |
mtext(paste("2 sigma = ", format(s2[2])), |
| 100 |
side= 1, line= m.line, adj = 0, cex= lo.cex) |
| 101 |
##abline(h= s2, lwd=1.8, lty=3, col=4) |
| 102 |
do.call("abline", c(list(h= s2), parSigma)) |
| 103 |
} |
| 104 |
n <- length(res) |
| 105 |
if(draw.smooth) { |
| 106 |
if(!is.list(parSmooth)) stop("`parSmooth' must be a list") |
| 107 |
##-- lo.iter: idea of Werner Stahel: no robustness for 'glm' residuals |
| 108 |
if (is.null(lo.iter)) |
| 109 |
lo.iter <- if(inherits(lm.res, "glm") && family(lm.res)$family[1] != "Gaussian") |
| 110 |
0 else 3 |
| 111 |
if(anyNA(res) && anyNA(fit)) { |
| 112 |
i.ok <- !is.na(res) & !is.na(fit) |
| 113 |
res <- res[i.ok] |
| 114 |
fit <- fit[i.ok] |
| 115 |
n <- sum(i.ok) |
| 116 |
} |
| 117 |
f <- max(0.2, 1.25 * n^-.2) #'-- Martin's very empirical formula... |
| 118 |
rlow <- lowess(fit, res, f = f, iter = lo.iter) |
| 119 |
do.call("lines",c(rlow, parSmooth)) |
| 120 |
|
| 121 |
mtext(paste("-.-.-.- : lowess smooth (f =", format(round(f,2)), |
| 122 |
if(lo.iter!=3) paste(", it=", lo.iter), ")"), |
| 123 |
side = 1, line = m.line, cex = lo.cex, adj = 1) |
| 124 |
} |
| 125 |
##- "Correlation:", formatC(cor(fit,res), dig=3), |
| 126 |
## mtext(paste(" -- Rank corr.:", formatC(cor(rank(fit),rank(res)), dig=3)) ) |
| 127 |
invisible() |
| 128 |
} |