Debian Codegraph @

demo/hatmat-ex.R

raw
1 #### Demos for sfsmisc::hatMat()
2
3 ##' Matrix trace -- tr(M) = \sum_i M_{i,i}
4 ##'
5 ##' .. content for \details{} ..
6 ##' @title Matrix trace
7 ##' @param m
8 ##' @return sum(<diagonal elements of 'm'>)
9 ##' @author Martin Maechler
10 TR <- function(m) { ## Matrix trace(.) == tr(.) :
11 stopifnot(length(d <- dim(m)) == 2, d[1] == d[2])
12 sum(diag(m))
13 }
14
15 ## Take those from ?hatMat -- modified --
16 ##--.--.--.--.--.--.--.--
17 ## Example 'pred.sm' arguments for hatMat() :
18 pspl <- function(x,y,...) predict(smooth.spline(x,y, ...), x = x)$y
19 ## needed! default surface="interpolate" is not good enough :
20 loess.C <- loess.control(surface = "direct")
21 ploess <- function(x,y,...) predict(loess(y ~ x, ..., control=loess.C))
22
23 pksm <- function(x,y,...) ksmooth(sort(x),y, kernel="normal", x.points=x, ...)$y
24 ## maybe rather than ksmooth():
25 if(require("sm"))
26 pksm2 <- function(x,y,...)
27 sm.regression(x,y, display="none", eval.points=x, ...)$estimate
28 ##--.--.--.--.--.--.--.--
29
30 set.seed(21)
31
32 x <- seq(0, 10, length= 201) ## sorted !! -- otherwise:
33 mf <- function(x) x^1.5 * sin(x)
34 y <- mf(x) + 5*rnorm(x)
35
36 ## Plot data + smooths -- smoothing parameters carefully chosen
37 ## such that df ~= 8.8 for all:
38 plot(x,y, cex=.6)
39 lines(x, mf(x), col="gray", lwd=3)## true m(x)
40 lines(predict(s1 <- smooth.spline(x,y, spar=.8)), col="blue"); s1 #-> df = 8.8
41 lines(x, predict(s2 <- loess(y ~ x, span=.385, control=loess.C)),
42 col="forest green"); s2 # df = 8.77
43 lines(x, s3y <- ksmooth(x,y, "normal", bandwidth= 1.3)$y, col="tomato")
44 s4 <- sm.regression(x,y, h = 0.54, display="none", eval.points=x)
45 lines(x, s4$estimate, col = "purple")
46 legend("topleft", c("true m(.)", "sm.spline", "loess", "ksmooth", "sm.regression"),
47 col=c("gray","blue","forest green","tomato","purple"),
48 lty=1, lwd=c(3, 1,1,1,1), inset=.01)
49
50 TR(H.sspl <- hatMat(x, pred.sm = pspl, spar = .8)) # 8.808432
51 TR(H.loess <- hatMat(x, pred.sm = ploess, span = .385)) # 8.865958
52 TR(H.ksm <- hatMat(x, pred.sm = pksm, bandwidth = 1.3))# 8.788017
53 TR(H.ksm2 <- hatMat(x, pred.sm = pksm2, h = 0.54)) # 8.80269 -- sm.regression is S.L.O.W
54
55 ## Check consistency:
56 stopifnot(
57 ## Smoothing Spline:
58 all.equal(c(H.sspl %*% y), fitted(s1))
59 ,
60 all.equal(sum(diag(H.sspl)), s1$df)
61 ,
62 ## Loess
63 all.equal(c(H.loess %*% y), fitted(s2))
64 ,
65 ## ksmooth()
66 all.equal(c(H.ksm %*% y), s3y)
67 ,
68 ## sm.regression()
69 all.equal(c(H.ksm2 %*% y), s4$estimate)
70 )
71
72
73 op <- mult.fig(mfrow=c(4,1), marP=-.5)$old.par
74 yl <- c(-.01, 0.10)
75 matplot(x, H.sspl, type="l", ylim=yl)
76 matplot(x, H.loess, type="l", ylim=yl)
77 matplot(x, H.ksm, type="l", ylim=yl)
78 matplot(x, H.ksm2, type="l", ylim=yl)
79 par(op)
80
81 ## or just a subset
82 i <- c(1, seq(10,200, by=20), length(x))
83 op <- mult.fig(mfrow=c(4,1), marP= -.5,
84 main = paste("rows",paste(i,collapse=",")," of hat matrices"))$old.par
85 matplot(x, H.sspl [,i], type="l", ylim=yl)
86 matplot(x, H.loess[,i], type="l", ylim=yl)
87 matplot(x, H.ksm [,i], type="l", ylim=yl)
88 matplot(x, H.ksm2 [,i], type="l", ylim=yl)
89 par(op)
90
91 ##' Image plot of a symmetric matrix -- traditional graphics
92 ##'
93 ##' @title Image Plot of Symmetric Matrix
94 ##' @param m symmetric numeric matrix
95 ##' @param color
96 ##' @param levels
97 ##' @param ... passed to filled.contour()
98 ##' @return
99 ##' @author Martin Maechler
100 pMatrix <- function(m, color=topo.colors, levels= pretty(range(m), 20), ...)
101 {
102 stopifnot(length(d <- dim(m)) == 2, d[1] == d[2])
103 n <- d[1]
104 ii <- seq_len(n)
105 i. <- rev(ii)
106 il <- unique(c(1,pretty(ii)))
107 op <- par(mgp = c(3, .6, 0)); on.exit(par(op))
108 filled.contour(ii, ii, m[i.,], color=color, levels=levels,
109 plot.axes = { axis(3, il); axis(2, at = n+1-il, labels = il)}, ...)
110 }
111
112 levs <- pretty(c(-0.025, 0.15), 25)
113 pMatrix(H.sspl, levels=levs,
114 main = "hat matrix S for smooth.spline()")
115
116 if(dev.interactive()) dev.new()
117 pMatrix(H.loess, levels=levs, main = "hat matrix S for loess()")
118
119 if(dev.interactive()) dev.new()
120 pMatrix(H.ksm, levels=levs, main = "hat matrix S for ksmooth()")