|
| 1 | +# $Id: pcb.R,v 1.9 2008-02-01 22:39:44 edzer Exp $ |
| 2 | +# FIGURE 1: |
| 3 | +library(sf) |
| 4 | +library(stars) |
| 5 | +library(gstat) |
| 6 | +library(ggplot2) |
| 7 | +data(pcb) |
| 8 | +pcb = st_as_sf(pcb, coords = c("x", "y"), remove = FALSE) |
| 9 | +data(ncp.grid) |
| 10 | +ncp.grid = st_as_stars(ncp.grid) |
| 11 | +wsv = read_sf(system.file("external/ncp.shp", package="gstat")) |
| 12 | +classes = c(.2,1,2,5,10,20) |
| 13 | +print(xyplot(y ~ x | as.factor(year), groups = sqrt(PCB138)/3, |
| 14 | + data = as.data.frame(pcb), |
| 15 | + panel = function(x, y, groups, subscripts, ...) { |
| 16 | + plot(st_geometry(wsv), col = grey(.5), add = TRUE) |
| 17 | + panel.xyplot(x, y, cex = groups[subscripts], ...) |
| 18 | + }, |
| 19 | + xlab = "x-coordinate", ylab = "y-coordinate", |
| 20 | + key = list(corner = c(0,0), x=0.8, y=0.25, points = list(pch = 1, col = 1, |
| 21 | + cex = sqrt(classes)/3), text = list(as.character(classes))), |
| 22 | + aspect = "iso", as.table = T, |
| 23 | + xlim = c(464000, 739000), ylim = c(5696500, 6131500), |
| 24 | + scales = list(draw = F))) |
| 25 | + |
| 26 | +# FIGURE 2: |
| 27 | +pcb$yf = as.factor(pcb$year) |
| 28 | +pcb$int = rep(NA, dim(pcb)[1]) |
| 29 | +c = lm(log(PCB138)~-1+depth+yf,pcb)$coefficients |
| 30 | +i = 2 |
| 31 | +for (f in levels(pcb$yf)) { |
| 32 | + pcb$int[pcb$yf == f] = c[i] |
| 33 | + i = i+1 |
| 34 | +} |
| 35 | +print(xyplot(log(PCB138)~depth | as.factor(year), groups = int, |
| 36 | + data = as.data.frame(pcb), |
| 37 | + panel = function(x, y, groups, subscripts, ...) { |
| 38 | + # panel.grid(h=-1, v= 2) |
| 39 | + panel.xyplot(x, y) |
| 40 | + y = c(groups[subscripts][1], groups[subscripts][1] + -0.0641*45) |
| 41 | + llines(c(0, 45), y, lty = 2) |
| 42 | + }, |
| 43 | + scales = list( |
| 44 | + y = list(at=log(c(.2, .5, 1, 2, 5, 10, 20)), |
| 45 | + labels=c(".2",".5","1","2","5","10","20"), alternating = F) |
| 46 | + ), xlab = "water depth", ylab = "PCB138", as.table = T) |
| 47 | +) |
| 48 | + |
| 49 | +# FIGURE 3: |
| 50 | +# ps.options(width=2, height=2) |
| 51 | +pcb$res=residuals(lm(log(PCB138)~year+depth, pcb)) |
| 52 | +v3 = variogram(res ~ year, pcb, dX=.1, bound=c(0,1000,3000,5000,(1:16)*10000)) |
| 53 | +print(plot(v3, model = vgm(.224,"Exp",17247,.08), plot.numbers = TRUE)) |
| 54 | + |
| 55 | +# FIGURE 4: |
| 56 | +# next |
| 57 | +g.pcb = NULL |
| 58 | +merge = list(c("P1986", 2, "P1991", 2), c("P1986", 2, "P1996", 2), |
| 59 | + c("P1986", 2, "P2000", 2)) |
| 60 | +for (f in levels(pcb$yf)[c(1,4,6,7)]) |
| 61 | + g.pcb= gstat(g.pcb, |
| 62 | + paste("P", as.character(f), sep = ""), |
| 63 | + log(PCB138)~depth, pcb[pcb$yf == f,], |
| 64 | + merge = merge) |
| 65 | +g.pcb = gstat(g.pcb, model = vgm(.224,"Exp",17247,.08), fill.all=T) |
| 66 | +v = variogram(g.pcb, cutoff=1e5) |
| 67 | +#plot(v, model = fit.lmc(v, g)) |
| 68 | +#plot(v, model = g,plot.numbers = TRUE) |
| 69 | +PCB.cor = matrix(NA, 4,4) |
| 70 | +i = 1 |
| 71 | +for (x in levels(pcb$yf)[c(1,4,6,7)]) { |
| 72 | + j = 1 |
| 73 | + for (y in levels(pcb$yf)[c(1,4,6,7)]) { |
| 74 | + A = log(pcb[pcb$yf == x,]$PCB138) |
| 75 | + B.tmp = krige(PCB138~1, pcb[pcb$yf == y,], |
| 76 | + pcb[pcb$yf == x,], nmax = 1, set=list(debug=0)) |
| 77 | + B = log(B.tmp$var1.pred) |
| 78 | + # print(paste(x, y, cor(A,B))) |
| 79 | + PCB.cor[i,j] = cor(A,B) |
| 80 | + j = j + 1 |
| 81 | + } |
| 82 | + i = i + 1 |
| 83 | +} |
| 84 | +years=c(1986,1991,1996,2000) |
| 85 | +dimnames(PCB.cor)=list(years,years) |
| 86 | +PCB.cor.ns = PCB.cor |
| 87 | +PCB.cor = 0.5 * (PCB.cor + t(PCB.cor)) |
| 88 | +i = 1 |
| 89 | +for (x in levels(pcb$yf)[c(1,4,6,7)]) { |
| 90 | + j = 1 |
| 91 | + for (y in levels(pcb$yf)[c(1,4,6,7)]) { |
| 92 | + if (j > i) { |
| 93 | + name = paste(paste("P", x, sep=""), paste("P", y, sep=""),sep = ".") |
| 94 | + print(name) |
| 95 | + g.pcb$model[[name]]["psill"] = |
| 96 | + g.pcb$model[[name]]["psill"] * PCB.cor[i,j] |
| 97 | + } |
| 98 | + j = j + 1 |
| 99 | + } |
| 100 | + i = i + 1 |
| 101 | +} |
| 102 | +print(plot(v, model = g.pcb, plot.numbers = FALSE)) |
| 103 | + |
| 104 | +print(PCB.cor.ns, digits=3) |
| 105 | + |
| 106 | +print(PCB.cor, digits=3) |
| 107 | + |
| 108 | +# FIGURE 5: |
| 109 | +pcb.cok = predict(g.pcb, newdata = ncp.grid, debug.level = 0) |
| 110 | +levs = c(.1,.2,.5,1,2,5,10,20) |
| 111 | + |
| 112 | +spplot(pcb.cok[c(1,3,5,7)], |
| 113 | + as.table=T, col.regions = bpy.colors(7), |
| 114 | + at = log(levs), |
| 115 | + colorkey = list(at = log(levs), labels = as.character(levs), |
| 116 | + col = bpy.colors(7)), |
| 117 | + layout = c(4,1) |
| 118 | +) |
| 119 | + |
| 120 | +spplot(pcb.cok[c(4,11,6,12,13,8,14,15,16,10)-2], |
| 121 | + skip=c(F,T,T,T,F,F,T,T,F,F,F,T,F,F,F,F), |
| 122 | + as.table=T, layout=c(4,4), |
| 123 | + asp="iso", col.regions = bpy.colors()) |
| 124 | + |
| 125 | +X = cbind(rep(1,7), c(1986, 1987, 1989, 1991, 1993, 1996, 2000)) |
| 126 | +X2=X[c(1,4,6,7),] |
| 127 | +lambda = solve(t(X2) %*% X2) %*% t(X2) |
| 128 | +dimnames(lambda) = list(NULL, c(1986, 1991, 1996, 2000)) |
| 129 | +print(lambda[2, ], digits=3) |
| 130 | + |
| 131 | +# FIGURE 7: |
| 132 | +pcb.contr = get.contr(pcb.cok, g.pcb, X=lambda[2, ]) |
| 133 | +# copy coordinates |
| 134 | +#pcb.contr$x = pcb.cok$x |
| 135 | +#pcb.contr$y = pcb.cok$y |
| 136 | + |
| 137 | +pl1 = spplot(pcb.contr["beta.1"], |
| 138 | + main = "log-PCB138: change estimate", |
| 139 | + col.regions = bpy.colors(100)) |
| 140 | +pcb.contr$sig = pcb.contr$beta.1 / sqrt(pcb.contr$var.beta.1) |
| 141 | +pl2 = spplot(pcb.contr["sig"], |
| 142 | + main = "log-PCB138 change/SE", |
| 143 | + col.regions = bpy.colors(100)) |
| 144 | +print(pl1, position=c(0,0,0.5,1), more = TRUE) |
| 145 | +print(pl2, position=c(0.5,0,1,1), more = FALSE) |
| 146 | + |
| 147 | +cat("source:\n\nEdzer J. Pebesma, Richard N.M. Duin (2005) Spatio-temporal mapping of\nsea floor sediment pollution in the North Sea. In: Ph. Renard, and\nR. Froidevaux, eds. Proceedings GeoENV 2004 -- Fifth European Conference\non Geostatistics for Environmental Applications; Springer.\n") |
0 commit comments