-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathstressplot.wcmdscale.R
284 lines (275 loc) · 9.51 KB
/
stressplot.wcmdscale.R
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
### stressplot() methods for eigenvector ordinations wcmdscale, rda,
### cca, capscale, dbrda
`stressplot.wcmdscale` <-
function(object, k = 2, pch, p.col = "blue", l.col = "red", lwd = 2, ...)
{
## Check that original distances can be reconstructed: this
## requires that all axes were calculated instead of 'k' first.
hasdims <- NCOL(object$points)
if (!is.null(object$negaxes))
hasdims <- hasdims + NCOL(object$negaxes)
if (hasdims < length(object$eig))
stop("observed distances cannot be reconstructed: all axes were not calculated")
## Get the ordination distances in k dimensions
if (k > NCOL(object$points))
warning(gettextf("max allowed rank is k = %d", NCOL(object$points)))
k <- min(NCOL(object$points), k)
w <- sqrt(object$weights)
u <- diag(w) %*% object$points
odis <- dist(u[,1:k, drop = FALSE])
## Reconstitute the original observed distances
dis <- dist(u)
if (!is.null(object$negaxes))
dis <- sqrt(dis^2 - dist(diag(w) %*% object$negaxes)^2)
## Remove additive constant to get original dissimilarities
if (!is.na(object$ac)) {
if (object$add == "lingoes")
dis <- sqrt(dis^2 - 2 * object$ac)
else if (object$add == "cailliez")
dis <- dis - object$ac
else
stop("unknown Euclidifying adjustment: no idea what to do")
}
##Plot
if (missing(pch))
if (length(dis) > 5000)
pch <- "."
else
pch <- 1
plot(dis, odis, pch = pch, col = p.col, xlab = "Observed Dissimilarity",
ylab = "Ordination Distance", ...)
abline(0, 1, col = l.col, lwd = lwd, ...)
invisible(odis)
}
`stressplot.rda` <-
function(object, k = 2, pch, p.col = "blue", l.col = "red", lwd = 2, ...)
{
## Normalized scores to reconstruct data
u <- cbind(object$CCA$u, object$CA$u)
v <- cbind(object$CCA$v, object$CA$v)
ev <- c(object$CCA$eig, object$CA$eig)
## check that k does not exceed rank
if (k > length(ev)) {
warning(gettextf("max allowed rank is k = %d", length(ev)))
k <- min(k, length(ev))
}
## normalizing constant
nr <- NROW(u)
const <- sqrt(ev * (nr-1))
u <- u %*% diag(const, length(const))
## Distances
Xbar <- u %*% t(v)
Xbark <- u[, seq_len(k), drop = FALSE] %*% t(v[, seq_len(k), drop = FALSE])
if (!is.null(object$pCCA)) {
pFit <- ordiYbar(object, "pCCA") * sqrt(nr-1)
Xbar <- Xbar + pFit
Xbark <- Xbark + pFit
}
dis <- dist(Xbar)
odis <- dist(Xbark)
## plot like above
## Plot
if (missing(pch))
if (length(dis) > 5000)
pch <- "."
else
pch <- 1
plot(dis, odis, pch = pch, col = p.col, xlab = "Observed Dissimilarity",
ylab = "Ordination Distance", ...)
abline(0, 1, col = l.col, lwd = lwd, ...)
invisible(odis)
}
`stressplot.cca` <-
function(object, k = 2, pch, p.col = "blue", l.col = "red", lwd = 2, ...)
{
## Normalized scores to reconstruct data
u <- cbind(object$CCA$u, object$CA$u)
sev <- sqrt(c(object$CCA$eig, object$CA$eig))
w <- sqrt(object$rowsum)
u <- diag(w) %*% u %*% diag(sev, length(sev))
v <- cbind(object$CCA$v, object$CA$v)
v <- diag(sqrt(object$colsum)) %*% v
## check that k <= rank
if (k > length(sev)) {
warning(gettextf("max allowed rank is k = %d", length(sev)))
k <- min(k, length(sev))
}
## Distances
Xbar <- u %*% t(v)
Xbark <- u[,seq_len(k), drop = FALSE] %*% t(v[,seq_len(k), drop = FALSE])
if (!is.null(object$pCCA)) {
pFit <- ordiYbar(object, "pCCA")
Xbar <- Xbar + pFit
Xbark <- Xbark + pFit
}
dis <- dist(Xbar)
odis <- dist(Xbark)
## Plot
if (missing(pch))
if (length(dis) > 5000)
pch <- "."
else
pch <- 1
plot(dis, odis, pch = pch, col = p.col, xlab = "Observed Dissimilarity",
ylab = "Ordination Distance", ...)
abline(0, 1, col = l.col, lwd = lwd, ...)
invisible(odis)
}
`stressplot.capscale` <-
function(object, k = 2, pch, p.col = "blue", l.col = "red", lwd = 2, ...)
{
## Scores to reconstruct data
u <- cbind(object$CCA$u, object$CA$u)
## check rank
if (k > NCOL(u))
warning(gettextf("max allowed rank is k = %d", ncol(u)))
k <- min(k, ncol(u))
ev <- c(object$CCA$eig, object$CA$eig)
u <- u %*% diag(sqrt(ev) * object$adjust, length(ev))
## Constrained ordination needs also scores 'v' to reconstruct
## 'data', but these are not returned by capscale() which replaces
## original 'v' with weighted sums of 'comm' data.
if (!is.null(object$CCA))
v <- svd(ordiYbar(object, "CCA"), nu = 0, nv = object$CCA$qrank)$v
else
v <- NULL
if (!is.null(object$CA))
v <- cbind(v, svd(ordiYbar(object, "CA"), nu = 0,
nv = object$CA$rank)$v)
## Reconstruct Xbar and Xbark
Xbar <- u %*% t(v)
Xbark <- u[,seq_len(k), drop = FALSE] %*% t(v[,seq_len(k), drop = FALSE])
if (!is.null(object$pCCA)) {
pFit <- ordiYbar(object, "pCCA")
Xbar <- Xbar + pFit
Xbark <- Xbark + pFit
}
## Distances
dis <- dist(Xbar)
odis <- dist(Xbark)
if (!is.null(object$CA$imaginary.u.eig)) {
dis <- dis^2 - dist(object$CA$imaginary.u.eig)^2
if (all(dis > -sqrt(.Machine$double.eps)))
dis <- sqrt(pmax.int(dis, 0))
else # neg dis will be NaN with a warning
dis <- sqrt(dis)
}
## Remove additive constant to get original dissimilarities
if (!is.null(object$ac)) {
if (object$add == "lingoes")
dis <- sqrt(dis^2 - 2 * object$ac)
else if (object$add == "cailliez")
dis <- dis - object$ac
else
stop("unknown Euclidifying adjustment: no idea what to do")
}
## undo internal sqrt.dist
if (object$sqrt.dist)
dis <- dis^2
## plot like above
## Plot
if (missing(pch))
if (length(dis) > 5000)
pch <- "."
else
pch <- 1
plot(dis, odis, pch = pch, col = p.col, xlab = "Observed Dissimilarity",
ylab = "Ordination Distance", ...)
abline(0, 1, col = l.col, lwd = lwd, ...)
invisible(odis)
}
### dbrda() returns only row scores 'u' (LC scores for constraints,
### site scores for unconstrained part), and these can be used to
### reconstitute dissimilarities only in unconstrained ordination or
### for constrained component. stressplot across component would need
### reconstruction of data, but dbrda components are not additive, or
### ordiYbar(x, "initial") is *not* ordiYbar(x, "pCCA") + ordiYbar(x,
### "CCA") + ordiYbar(x, "CA").
`stressplot.dbrda` <-
function(object, k = 2, pch, p.col = "blue", l.col = "red", lwd = 2, ...)
{
if (!is.null(object$pCCA))
stop("partial models cannot be analysed")
## Reconstructed zero distances can be tiny (negative) non-zero
## values, and we zap them to zero
ZAP <- sqrt(.Machine$double.eps)
## Reconstruct original distances from Gower 'G'
dis <- ordiYbar(object, "initial")
dia <- diag(dis)
dis <- -2 * dis + outer(dia, dia, "+")
dis[abs(dis) < ZAP] <- 0
dis <- sqrt(as.dist(dis)) * object$adjust
## Remove additive constant to get original dissimilarities
if (!is.null(object$ac)) {
if (object$add == "lingoes")
dis <- sqrt(dis^2 - 2 * object$ac)
else if (object$add == "cailliez")
dis <- dis - object$ac
else
stop("unknown Euclidifying adjustment: no idea what to do")
}
## undo internal sqrt.dist
if (object$sqrt.dist)
dis <- dis^2
## Approximate dissimilarities from real components with positive
## eigenvalues.
if (!is.null(object$CCA)) {
U <- object$CCA$u
kmax <- object$CCA$poseig
eig <- object$CCA$eig[seq_len(kmax)]
} else {
U <- object$CA$u
kmax <- object$CA$poseig
eig <- object$CA$eig[seq_len(kmax)]
}
U <- U %*% diag(sqrt(eig), nrow = kmax)
if (k > kmax) {
warning(gettextf("max allowed rank is k = %d", kmax))
k <- kmax
}
if (k > 0) {
odis <- dist(U[, seq_len(k), drop = FALSE]) * object$adjust
} else {
odis <- dist(matrix(0, nrow(U)))
}
if (missing(pch))
if (length(dis) > 5000)
pch <- "."
else
pch <- 1
plot(dis, odis, pch = pch, col = p.col, xlab = "Observed Dissimilarity",
ylab = "Ordination Distance", ...)
abline(0, 1, col = l.col, lwd = lwd, ...)
invisible(odis)
}
## Standard R PCA functions
`stressplot.prcomp` <-
function(object, k = 2, pch, p.col = "blue", l.col = "red", lwd = 2, ...)
{
dis <- dist(object$x)
odis <- dist(object$x[, 1:k, drop = FALSE])
if (missing(pch))
if (length(dis) > 5000)
pch <- "."
else
pch <- 1
plot(dis, odis, pch = pch, col = p.col, xlab = "Observed Dissimilarity",
ylab = "Ordination Distance", ...)
abline(0, 1, col = l.col, lwd = lwd, ...)
invisible(odis)
}
`stressplot.princomp` <-
function(object, k = 2, pch, p.col = "blue", l.col = "red", lwd = 2, ...)
{
dis <- dist(object$scores)
odis <- dist(object$scores[, 1:k, drop = FALSE])
if (missing(pch))
if (length(dis) > 5000)
pch <- "."
else
pch <- 1
plot(dis, odis, pch = pch, col = p.col, xlab = "Observed Dissimilarity",
ylab = "Ordination Distance", ...)
abline(0, 1, col = l.col, lwd = lwd, ...)
invisible(odis)
}