Skip to content

Instantly share code, notes, and snippets.

@rBatt
Last active April 1, 2017 01:50
Show Gist options
  • Save rBatt/7faba570cfe1b31b4ce6416d9414833f to your computer and use it in GitHub Desktop.
Save rBatt/7faba570cfe1b31b4ce6416d9414833f to your computer and use it in GitHub Desktop.
If all species occupy only 1 site, does beta diversity depend on whether they occupy different sites or overlap?
nspp <- 10
nsites <- 10
Y_1sppPer <- diag(x=1, nrow=nspp, ncol=nsites)
Y_allSpp1Site <- rbind(rep(1,nspp), matrix(rep(0,nspp*nsites-nspp),nrow=nsites-1))
# euclidean distance
euc <- function(x){
sum(scale(x,scale=FALSE)^2)/(nrow(x)-1)
}
euc(Y_1sppPer) # 1
euc(Y_allSpp1Site) # 1
# jaccard
jaccard <- function(x){
n <- nrow(x)
d <- ade4::dist.binary(x, method=1) # need package ade4
sstot <- sum(d^2)/n
sstot/(n-1)
}
jaccard(Y_1sppPer) # 0.5
jaccard(Y_allSpp1Site) # NaN
# hellinger
hellinger <- function(x){
n <- nrow(x)
Y <- vegan::decostand(x, "hellinger") # requires vegan package
s <- scale(Y, center=TRUE, scale=FALSE)^2
sum(s) /(n-1)
}
hellinger(Y_1sppPer) # 1
hellinger(Y_allSpp1Site) # 0.1
@rBatt
Copy link
Author

rBatt commented Mar 31, 2017

> Y_allSpp1Site
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    1    1    1    1    1    1    1    1     1
 [2,]    0    0    0    0    0    0    0    0    0     0
 [3,]    0    0    0    0    0    0    0    0    0     0
 [4,]    0    0    0    0    0    0    0    0    0     0
 [5,]    0    0    0    0    0    0    0    0    0     0
 [6,]    0    0    0    0    0    0    0    0    0     0
 [7,]    0    0    0    0    0    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    0    0     0
> Y_1sppPer
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    0    0    0    0    0    0    0    0     0
 [2,]    0    1    0    0    0    0    0    0    0     0
 [3,]    0    0    1    0    0    0    0    0    0     0
 [4,]    0    0    0    1    0    0    0    0    0     0
 [5,]    0    0    0    0    1    0    0    0    0     0
 [6,]    0    0    0    0    0    1    0    0    0     0
 [7,]    0    0    0    0    0    0    1    0    0     0
 [8,]    0    0    0    0    0    0    0    1    0     0
 [9,]    0    0    0    0    0    0    0    0    1     0
[10,]    0    0    0    0    0    0    0    0    0     1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment