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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
distance <- function(x, method="euclidean", sprange=NULL, spweight=NULL)
{
# calculates similarity and dissimilarity coefficients
# as described in Legendre and Legendre 1998
# returns lower-triangle
###
# Sarah Goslee
# 2 March 2006
# revised 31 March 2008
# bug-fix 15 December 2008
###
# uses clever matrix math to calculate the pieces needed
# by dissimilarity matrices, to make it easy to add new
# indices.
###
# to add a new metric:
# add it to the commented list below
# add it to the end of the METHODS <- c(...) list
# add the code at the appropriate point at the bottom of
# the function
###
# Gower offers the option of dividing by the species range
# if sprange=NULL no range is used
# Euclidean, Manhattan, Gower allow weighting options
# if spweight=NULL no weighting is used
# if spweight="absence" then W is = 0 if both species are absent,
# and 1 otherwise, thus deleting joint absences
### Available methods
# 1: euclidean
# 2: bray-curtis
# 3: manhattan
# 4: mahalanobis
# 5: jaccard
# 6: simple difference
# 7: sorensen
# 8: Gower
# 9: Modified Gower base 10 (Anderson et al 2006)
# 10: Modified Gower base 2 (Anderson et al 2006)
pairedsum <- function(x)
{
### paired sums
### returns an N by N by P matrix containing each
### combination of
N <- nrow(x)
P <- ncol(x)
A <- numeric(N * N * P)
A <- .C("psum",
as.double(as.vector(t(x))),
as.integer(N),
as.integer(P),
A = as.double(A),
PACKAGE = "ecodist")$A
A <- array(A, dim=c(N, N, P))
A
}
paireddiff <- function(x)
{
### paired differences
N <- nrow(x)
P <- ncol(x)
A <- numeric(N * N * P)
A <- .C("pdiff",
as.double(as.vector(t(x))),
as.integer(N),
as.integer(P),
A = as.double(A),
PACKAGE = "ecodist")$A
A <- array(A, dim=c(N, N, P))
A
}
jointpresence <- function(x)
{
### joint count of presences
N <- nrow(x)
P <- ncol(x)
A <- numeric(N * N * P)
A <- .C("jpres",
as.double(as.vector(t(x))),
as.integer(N),
as.integer(P),
A = as.double(A),
PACKAGE = "ecodist")$A
A <- array(A, dim=c(N, N, P))
A
}
jointabsence <- function(x)
{
### joint count of absences
N <- nrow(x)
P <- ncol(x)
A <- numeric(N * N * P)
A <- .C("jabs",
as.double(as.vector(t(x))),
as.integer(N),
as.integer(P),
A = as.double(A),
PACKAGE = "ecodist")$A
A <- array(A, dim=c(N, N, P))
A
}
firstonly <- function(x)
{
### present only in first sample
N <- nrow(x)
P <- ncol(x)
A <- numeric(N * N * P)
A <- .C("jfirst",
as.double(as.vector(t(x))),
as.integer(N),
as.integer(P),
A = as.double(A),
PACKAGE = "ecodist")$A
A <- array(A, dim=c(N, N, P))
A
}
secondonly <- function(x)
{
### present only in second sample
N <- nrow(x)
P <- ncol(x)
A <- numeric(N * N * P)
A <- .C("jsec",
as.double(as.vector(t(x))),
as.integer(N),
as.integer(P),
A = as.double(A),
PACKAGE = "ecodist")$A
A <- array(A, dim=c(N, N, P))
A
}
x <- as.matrix(x)
## code borrowed from dist()
METHODS <- c("euclidean", "bray-curtis", "manhattan", "mahalanobis", "jaccard", "difference", "sorensen", "gower", "modgower10", "modgower2")
method <- pmatch(method, METHODS)
if (is.na(method))
stop("invalid distance method")
if (method == -1)
stop("ambiguous distance method")
N <- nrow(x)
P <- ncol(x)
if(method == 1)
{
# Euclidean distance
A <- paireddiff(x)
if(is.null(spweight)) {
D <- sqrt(apply(A, 1:2, function(x)sum(x * x)))
}
else if(spweight[1] == "absence") {
W <- ifelse(jointabsence(x)==1, 0, 1)
D <- sqrt(apply((W^2)*A, 1:2, function(x)sum(x * x))) / apply(W, 1:2, sum)
}
else if(length(spweight) == ncol(x)) {
W <- array(rep(spweight, each=nrow(x)^2), dim=c(nrow(x), nrow(x), ncol(x)))
D <- sqrt(apply((W^2)*A, 1:2, function(x)sum(x * x))) / apply(W, 1:2, sum)
}
else {
stop("Unknown weighting method.\n")
}
}
if(method == 2)
{
# Bray-Curtis distance
A <- paireddiff(x)
A <- apply(A, 1:2, function(x)sum(abs(x)))
B <- pairedsum(x)
B <- apply(B, 1:2, sum)
D <- A / B
}
if(method == 3)
{
# unstandardized manhattan distance
A <- paireddiff(x)
if(is.null(spweight)) {
D <- apply(A, 1:2, function(x)sum(abs(x)))
}
else if(spweight[1] == "absence") {
W <- ifelse(jointabsence(x)==1, 0, 1)
D <- apply(W*A, 1:2, function(x)sum(abs(x))) / apply(W, 1:2, sum)
}
else if(length(spweight) == ncol(x)) {
W <- array(rep(spweight, each=nrow(x)^2), dim=c(nrow(x), nrow(x), ncol(x)))
D <- apply(W*A, 1:2, function(x)sum(abs(x))) / apply(W, 1:2, sum)
}
else {
stop("Unknown weighting method.\n")
}
}
if(method == 4)
{
# pairwise Mahalanobis distance
# same as mahal()
icov <- solve(cov(x))
A <- paireddiff(x)
A1 <- apply(A, 1, function(z)(z %*% icov %*% t(z)))
D <- A1[seq(1, N*N, by=(N+1)), ]
}
if(method == 5)
{
# Jaccard distance
A <- jointpresence(x)
A <- apply(A, 1:2, sum)
B <- firstonly(x)
B <- apply(B, 1:2, sum)
C <- secondonly(x)
C <- apply(C, 1:2, sum)
D <- 1 - A / (A + B + C)
}
if(method == 6)
{
# simple difference, NOT symmetric
D <- paireddiff(x)[,,1, drop=TRUE]
}
if(method == 7)
{
# Sorensen distance
A <- jointpresence(x)
A <- apply(A, 1:2, sum)
B <- firstonly(x)
B <- apply(B, 1:2, sum)
C <- secondonly(x)
C <- apply(C, 1:2, sum)
D <- 1 - (2*A) / (2*A + B + C)
}
if(method == 8)
{
# Gower distance
# weighting
A <- paireddiff(x)
if(!is.null(sprange)) {
if(length(sprange) == ncol(x)) {
sprange <- array(rep(sprange, each=nrow(x)^2), dim=c(nrow(x), nrow(x), ncol(x)))
A <- A / sprange
}
else {
stop("sprange not recognized.\n")
}
}
if(is.null(spweight)) {
D <- apply(A, 1:2, function(x)sum(abs(x)))
}
else if(spweight[1] == "absence") {
W <- ifelse(jointabsence(x)==1, 0, 1)
D <- apply(A*W, 1:2, function(x)sum(abs(x))) / apply(W, 1:2, sum)
}
else if(length(spweight) == ncol(x)) {
W <- array(rep(spweight, each=nrow(x)^2), dim=c(nrow(x), nrow(x), ncol(x)))
D <- apply(W*A, 1:2, function(x)sum(abs(x))) / apply(W, 1:2, sum)
}
else {
stop("Unknown weighting method.\n")
}
}
if(method == 9)
{
# modified Gower, base 10
x <- ifelse(x == 0, 0, log10(x))
A <- paireddiff(x)
if(is.null(spweight)) {
D <- apply(A, 1:2, function(x)sum(abs(x)))
}
else if(spweight[1] == "absence") {
W <- ifelse(jointabsence(x)==1, 0, 1)
D <- apply(W*A, 1:2, function(x)sum(abs(x))) / apply(W, 1:2, sum)
}
else if(length(spweight) == ncol(x)) {
W <- array(rep(spweight, each=nrow(x)^2), dim=c(nrow(x), nrow(x), ncol(x)))
D <- apply(W*A, 1:2, function(x)sum(abs(x))) / apply(W, 1:2, sum)
}
else {
stop("Unknown weighting method.\n")
}
}
if(method == 10)
{
# modified Gower, base 2
x <- ifelse(x == 0, 0, log2(x))
A <- paireddiff(x)
if(is.null(spweight)) {
D <- apply(A, 1:2, function(x)sum(abs(x)))
}
else if(spweight[1] == "absence") {
W <- ifelse(jointabsence(x)==1, 0, 1)
D <- apply(W*A, 1:2, function(x)sum(abs(x))) / apply(W, 1:2, sum)
}
else if(length(spweight) == ncol(x)) {
W <- array(rep(spweight, each=nrow(x)^2), dim=c(nrow(x), nrow(x), ncol(x)))
D <- apply(W*A, 1:2, function(x)sum(abs(x))) / apply(W, 1:2, sum)
}
else {
stop("Unknown weighting method.\n")
}
}
## Make the results lower triangular
D <- D[col(D) < row(D)]
## give the results attributes similar to dist()
attr(D, "Size") <- N
attr(D, "Labels") <- rownames(x)
attr(D, "Diag") <- FALSE
attr(D, "Upper") <- FALSE
attr(D, "method") <- METHODS[method]
class(D) <- "dist"
D
}
|