Schefferville transitions - within and between comparisons of unconstrained cluster

Tammy L. Elliott

March 8, 2015

R version 3.01

# set global chunk options: 
library(knitr)
## Warning: package 'knitr' was built under R version 3.1.2
opts_chunk$set(cache=TRUE, fig.align='center')

K-means clustering with 4-6 clusters

#vasc.env.clust.4<-kmeans(site.env.dist,4)
#saveRDS(vasc.env.clust.4, file="vasc.env.clust.4.rds")
vasc.env.clust.4<-readRDS("vasc.env.clust.4.rds")
#vasc.env.clust.5<-kmeans(site.env.dist,5)
#saveRDS(vasc.env.clust.5, file="vasc.env.clust.5.rds")
vasc.env.clust.5<-readRDS("vasc.env.clust.5.rds")
#vasc.env.clust.6<-kmeans(site.env.dist,6)
#saveRDS(vasc.env.clust.6, file="vasc.env.clust.6.rds")
vasc.env.clust.6<-readRDS("vasc.env.clust.6.rds")
# put groups
vasc.5.groups<-vasc.env.clust.5$cluster

Evaluate composition of Sorensen’s beta diversity

# Calculate Sorensen (Podani family, Sorensen-based indices), with quantitative form of replacement, nestedness and D.
beta.div.comp.vasc <- beta.div.comp(abd.sp.allsp, coef="S", quant=TRUE, save.abc=FALSE)
beta.div.comp.vasc$D[1]
## [1] 0.9097744
beta.div.comp.vasc$repl[1]
## [1] 0.8120301
beta.div.comp.vasc$rich[1]
## [1] 0.09774436
vasc.D.m<-as.matrix(beta.div.comp.vasc$D)
vasc.rich.m<-as.matrix(beta.div.comp.vasc$rich)
vasc.repl.m<-as.matrix(beta.div.comp.vasc$repl)

# extract groups from group list

vasc.5.groups<-vasc.env.clust.5$cluster

vasc.group.1<-vasc.5.groups[vasc.5.groups==1]
vasc.group.1
## EA1 EB1 ED1 EE1 EF1 EG1 EI1 EK1 EC2 ED2 EE2 EG2 EA3 EC3 EE3 EF3 EG3 EI3 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## EJ3 EA4 EC4 ED4 EF4 EJ4 ED5 EF5 EH6 EA7 EC7 ED7 EC8 ED8 EF8 EG8 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
vasc.group.2<-vasc.5.groups[vasc.5.groups==2]
vasc.group.3<-vasc.5.groups[vasc.5.groups==3]
vasc.group.4<-vasc.5.groups[vasc.5.groups==4]
vasc.group.5<-vasc.5.groups[vasc.5.groups==5]


vasc.D.plots <- data.frame(t(combn(rownames(abd.sp.allsp),2)), as.numeric(beta.div.comp.vasc$D))
vasc.D.plot.names<-data.frame(t(combn(rownames(abd.sp.allsp),2)))
vasc.D.grps<-cbind(vasc.D.plots, vasc.D.plot.names )
head(vasc.D.grps)
##    X1  X2 as.numeric.beta.div.comp.vasc.D.  X1  X2
## 1 EA1 EB1                        0.9097744 EA1 EB1
## 2 EA1 EC1                        0.9004525 EA1 EC1
## 3 EA1 ED1                        0.5714286 EA1 ED1
## 4 EA1 EE1                        0.8300000 EA1 EE1
## 5 EA1 EF1                        0.5133531 EA1 EF1
## 6 EA1 EG1                        0.8264151 EA1 EG1

Beta diversity within and between cluster comparisons

vasc.D.groups.within.mean<-cbind(vasc.D.grps.within.1.mean, vasc.D.grps.within.2.mean, vasc.D.grps.within.3.mean, vasc.D.grps.within.4.mean, vasc.D.grps.within.5.mean)
vasc.D.groups.within.sd<-cbind(vasc.D.grps.within.1.sd, vasc.D.grps.within.2.sd, vasc.D.grps.within.3.sd, vasc.D.grps.within.4.sd, vasc.D.grps.within.5.sd)
vasc.D.groups.within.nrows<-c(nrow(vasc.D.grps.within.1), nrow(vasc.D.grps.within.2), nrow(vasc.D.grps.within.3),
 nrow(vasc.D.grps.within.4), nrow(vasc.D.grps.within.5))
vasc.D.groups.within<-rbind(vasc.D.groups.within.mean, vasc.D.groups.within.sd, vasc.D.groups.within.nrows)
colnames(vasc.D.groups.within)<-c("1", "2", "3", "4", "5")
rownames(vasc.D.groups.within)<-c("Mean", "SD", "n")
signif(vasc.D.groups.within,digits=3)
##            1       2       3        4       5
## Mean   0.796   0.783   0.801    0.793   0.797
## SD     0.144   0.222   0.184    0.165   0.170
## n    561.000 406.000 406.000 1380.000 465.000
# Extract non-similar values from group vectors of matrix
vasc.D.grps.diff.1.2<-apply(as.matrix(1:15400),1,function(x) vasc.D.grps$Grp1[x]==1&vasc.D.grps$Grp2[x]==2|vasc.D.grps$Grp1[x]==2&vasc.D.grps$Grp2[x]==1)
vasc.D.grps.diff.1.2<-vasc.D.grps[which(vasc.D.grps.diff.1.2==TRUE),]
nrow(vasc.D.grps.diff.1.2)
## [1] 986
vasc.D.groups.diff<-rbind(vasc.D.groups.diff.mean, vasc.D.groups.diff.sd, vasc.D.groups.diff.nrows)
colnames(vasc.D.groups.diff)<-c("1x2","1x3", "1x4", "1x5", "2x3", "2x4", "2x5", "3x4", "3x5", "4x5")
rownames(vasc.D.groups.diff)<-c("Mean", "SD", "n")
signif(vasc.D.groups.diff,digits=3)
##           1x2     1x3      1x4      1x5     2x3      2x4     2x5      3x4
## Mean   0.9150   0.920    0.802    0.896   0.836    0.915   0.857    0.915
## SD     0.0943   0.104    0.161    0.110   0.192    0.112   0.165    0.118
## n    986.0000 986.000 1800.000 1050.000 841.000 1540.000 899.000 1540.000
##          3x5      4x5
## Mean   0.895    0.878
## SD     0.135    0.127
## n    899.000 1640.000
vasc.rich.groups.within.mean<-cbind(vasc.rich.grps.within.1.mean, vasc.rich.grps.within.2.mean, vasc.rich.grps.within.3.mean, vasc.rich.grps.within.4.mean, vasc.rich.grps.within.5.mean)
vasc.rich.groups.within.sd<-cbind(vasc.rich.grps.within.1.sd, vasc.rich.grps.within.2.sd, vasc.rich.grps.within.3.sd, vasc.rich.grps.within.4.sd, vasc.rich.grps.within.5.sd)
vasc.rich.groups.within.nrows<-c(nrow(vasc.rich.grps.within.1), nrow(vasc.rich.grps.within.2), nrow(vasc.rich.grps.within.3),
 nrow(vasc.rich.grps.within.4), nrow(vasc.rich.grps.within.5))
vasc.rich.groups.within<-rbind(vasc.rich.groups.within.mean, vasc.rich.groups.within.sd, vasc.rich.groups.within.nrows)
colnames(vasc.rich.groups.within)<-c("1", "2", "3", "4", "5")
rownames(vasc.rich.groups.within)<-c("Mean", "SD", "n")
signif(vasc.rich.groups.within,digits=3)
##            1       2       3        4       5
## Mean   0.224   0.296   0.358    0.233   0.322
## SD     0.165   0.203   0.265    0.165   0.202
## n    561.000 406.000 406.000 1380.000 465.000
vasc.rich.groups.diff<-rbind(vasc.rich.groups.diff.mean, vasc.rich.groups.diff.sd, vasc.rich.groups.diff.nrows)
colnames(vasc.rich.groups.diff)<-c("1x2","1x3", "1x4", "1x5", "2x3", "2x4", "2x5", "3x4", "3x5", "4x5")
rownames(vasc.rich.groups.diff)<-c("Mean", "SD", "n")
signif(vasc.rich.groups.diff,digits=3)
##          1x2     1x3      1x4      1x5     2x3      2x4     2x5      3x4
## Mean   0.304   0.293    0.226    0.275   0.356    0.293   0.323    0.298
## SD     0.182   0.230    0.167    0.186   0.244    0.179   0.203    0.233
## n    986.000 986.000 1800.000 1050.000 841.000 1540.000 899.000 1540.000
##          3x5      4x5
## Mean   0.335    0.277
## SD     0.240    0.186
## n    899.000 1640.000
# Do the same with the replacement values
vasc.repl.plots <- data.frame(t(combn(rownames(abd.sp.allsp),2)), as.numeric(beta.div.comp.vasc$repl))
vasc.repl.plot.names<-data.frame(t(combn(rownames(abd.sp.allsp),2)))
vasc.repl.grps<-cbind(vasc.repl.plots, vasc.repl.plot.names )

Grp1<-vector()
for(i in 1:length(vasc.5.groups)){
  to.change<-names(vasc.5.groups[i])
  index<-which(as.character(vasc.repl.grps[,4])==to.change)
  Grp1[index]<-as.numeric(vasc.5.groups[i])
}

Grp2<-vector()
for(i in 1:length(vasc.5.groups)){
  to.change<-names(vasc.5.groups[i])
  index<-which(as.character(vasc.repl.grps[,5])==to.change)
  Grp2[index]<-as.numeric(vasc.5.groups[i])
}

vasc.repl.grps<-cbind(vasc.repl.plots, as.factor(Grp1), as.factor(Grp2))
names(vasc.repl.grps)<-c("Plot1", "Plot2", "repl", "Grp1", "Grp2")

# Extract matching groups from Group 1 and Groups 2
vasc.repl.grps.within.1<-apply(as.matrix(1:15400),1,function(x) vasc.repl.grps$Grp1[x]==1 & vasc.repl.grps$Grp2[x]==1)
vasc.repl.grps.within.1<-vasc.repl.grps[which(vasc.repl.grps.within.1==TRUE),]
nrow(vasc.repl.grps.within.1)
## [1] 561
vasc.repl.grps.within.1.mean<-mean(vasc.repl.grps.within.1$repl)
vasc.repl.grps.within.1.mean
## [1] 0.5716533
vasc.repl.grps.within.1.sd<-sd(vasc.repl.grps.within.1$repl)
vasc.repl.grps.within.1.sd
## [1] 0.1863324
vasc.repl.grps.within.2<-apply(as.matrix(1:15400),1,function(x) vasc.repl.grps$Grp1[x]==2 & vasc.repl.grps$Grp2[x]==2)
vasc.repl.grps.within.2<-vasc.repl.grps[which(vasc.repl.grps.within.2==TRUE),]
nrow(vasc.repl.grps.within.2)
## [1] 406
vasc.repl.grps.within.2.mean<-mean(vasc.repl.grps.within.2$repl)
vasc.repl.grps.within.2.mean
## [1] 0.4872342
vasc.repl.grps.within.2.sd<-sd(vasc.repl.grps.within.2$repl)
vasc.repl.grps.within.2.sd
## [1] 0.2297573
vasc.repl.grps.within.3<-apply(as.matrix(1:15400),1,function(x) vasc.repl.grps$Grp1[x]==3 & vasc.repl.grps$Grp2[x]==3)
vasc.repl.grps.within.3<-vasc.repl.grps[which(vasc.repl.grps.within.3==TRUE),]
nrow(vasc.repl.grps.within.3)
## [1] 406
vasc.repl.grps.within.3.mean<-mean(vasc.repl.grps.within.3$repl)
vasc.repl.grps.within.3.mean
## [1] 0.4426757
vasc.repl.grps.within.3.sd<-sd(vasc.repl.grps.within.3$repl)
vasc.repl.grps.within.3.sd
## [1] 0.2513197
vasc.repl.grps.within.4<-apply(as.matrix(1:15400),1,function(x) vasc.repl.grps$Grp1[x]==4 & vasc.repl.grps$Grp2[x]==4)
vasc.repl.grps.within.4<-vasc.repl.grps[which(vasc.repl.grps.within.4==TRUE),]
nrow(vasc.repl.grps.within.4)
## [1] 1378
vasc.repl.grps.within.4.mean<-mean(vasc.repl.grps.within.4$repl)
vasc.repl.grps.within.4.mean
## [1] 0.5601164
vasc.repl.grps.within.4.sd<-sd(vasc.repl.grps.within.4$repl)
vasc.repl.grps.within.4.sd
## [1] 0.2160498
vasc.repl.grps.within.5<-apply(as.matrix(1:15400),1,function(x) vasc.repl.grps$Grp1[x]==5 & vasc.repl.grps$Grp2[x]==5)
vasc.repl.grps.within.5<-vasc.repl.grps[which(vasc.repl.grps.within.5==TRUE),]
nrow(vasc.repl.grps.within.5)
## [1] 465
vasc.repl.grps.within.5.mean<-mean(vasc.repl.grps.within.5$repl)
vasc.repl.grps.within.5.mean
## [1] 0.4748534
vasc.repl.grps.within.5.sd<-sd(vasc.repl.grps.within.5$repl)
vasc.repl.grps.within.5.sd
## [1] 0.213798
vasc.repl.groups.within.mean<-cbind(vasc.repl.grps.within.1.mean, vasc.repl.grps.within.2.mean, vasc.repl.grps.within.3.mean, vasc.repl.grps.within.4.mean, vasc.repl.grps.within.5.mean)
vasc.repl.groups.within.sd<-cbind(vasc.repl.grps.within.1.sd, vasc.repl.grps.within.2.sd, vasc.repl.grps.within.3.sd, vasc.repl.grps.within.4.sd, vasc.repl.grps.within.5.sd)
vasc.repl.groups.within.nrows<-c(nrow(vasc.repl.grps.within.1), nrow(vasc.repl.grps.within.2), nrow(vasc.repl.grps.within.3),
 nrow(vasc.repl.grps.within.4), nrow(vasc.repl.grps.within.5))
vasc.repl.groups.within<-rbind(vasc.repl.groups.within.mean, vasc.repl.groups.within.sd, vasc.repl.groups.within.nrows)
colnames(vasc.repl.groups.within)<-c("1", "2", "3", "4", "5")
rownames(vasc.repl.groups.within)<-c("Mean", "SD", "n")
signif(vasc.repl.groups.within,digits=3)
##            1       2       3        4       5
## Mean   0.572   0.487   0.443    0.560   0.475
## SD     0.186   0.230   0.251    0.216   0.214
## n    561.000 406.000 406.000 1380.000 465.000
vasc.repl.groups.diff<-rbind(vasc.repl.groups.diff.mean, vasc.repl.groups.diff.sd, vasc.repl.groups.diff.nrows)
colnames(vasc.repl.groups.diff)<-c("1x2","1x3", "1x4", "1x5", "2x3", "2x4", "2x5", "3x4", "3x5", "4x5")
rownames(vasc.repl.groups.diff)<-c("Mean", "SD", "n")
signif(vasc.repl.groups.diff,digits=3)
##          1x2     1x3      1x4      1x5     2x3      2x4     2x5      3x4
## Mean   0.611   0.627    0.577    0.621   0.479    0.621   0.533    0.617
## SD     0.184   0.227    0.205    0.196   0.240    0.193   0.216    0.234
## n    986.000 986.000 1800.000 1050.000 841.000 1540.000 899.000 1540.000
##          3x5      4x5
## Mean   0.560    0.601
## SD     0.237    0.201
## n    899.000 1640.000
#Compare within and among groups
vasc.D.groups.within
##                1           2          3            4           5
## Mean   0.7958000   0.7831778   0.800797    0.7929639   0.7965634
## SD     0.1436872   0.2219213   0.184044    0.1651796   0.1704064
## n    561.0000000 406.0000000 406.000000 1378.0000000 465.0000000
vasc.rich.groups.within
##                1           2           3            4           5
## Mean   0.2241467   0.2959436   0.3581213    0.2328475   0.3217101
## SD     0.1654654   0.2029446   0.2645945    0.1647176   0.2015790
## n    561.0000000 406.0000000 406.0000000 1378.0000000 465.0000000
vasc.repl.groups.within
##                1           2           3            4           5
## Mean   0.5716533   0.4872342   0.4426757    0.5601164   0.4748534
## SD     0.1863324   0.2297573   0.2513197    0.2160498   0.2137980
## n    561.0000000 406.0000000 406.0000000 1378.0000000 465.0000000
vasc.D.groups.diff
##               1x2         1x3          1x4          1x5         2x3
## Mean   0.91526347   0.9197237    0.8024350    0.8959609   0.8357421
## SD     0.09432423   0.1041256    0.1611749    0.1104454   0.1923259
## n    986.00000000 986.0000000 1802.0000000 1054.0000000 841.0000000
##               2x4         2x5          3x4         3x5         4x5
## Mean    0.9146473   0.8565390    0.9145783   0.8952288    0.878292
## SD      0.1120741   0.1646083    0.1184867   0.1347141    0.126833
## n    1537.0000000 899.0000000 1537.0000000 899.0000000 1643.000000
vasc.rich.groups.diff
##              1x2         1x3          1x4          1x5         2x3
## Mean   0.3040599   0.2928844    0.2257138    0.2753732   0.3564609
## SD     0.1822009   0.2301109    0.1670974    0.1864531   0.2435215
## n    986.0000000 986.0000000 1802.0000000 1054.0000000 841.0000000
##               2x4         2x5          3x4         3x5          4x5
## Mean    0.2932989   0.3233046    0.2977389   0.3347957    0.2774573
## SD      0.1791971   0.2032539    0.2330723   0.2399900    0.1861403
## n    1537.0000000 899.0000000 1537.0000000 899.0000000 1643.0000000
vasc.repl.groups.diff
##              1x2         1x3          1x4          1x5         2x3
## Mean   0.6112035   0.6268393    0.5767212    0.6205877   0.4792812
## SD     0.1836065   0.2272347    0.2046841    0.1960249   0.2400030
## n    986.0000000 986.0000000 1802.0000000 1054.0000000 841.0000000
##               2x4         2x5          3x4         3x5          4x5
## Mean    0.6213484   0.5332343    0.6168393   0.5604331    0.6008347
## SD      0.1927200   0.2164440    0.2343640   0.2372252    0.2008297
## n    1537.0000000 899.0000000 1537.0000000 899.0000000 1643.0000000
vasc.within.rich.repl<-rbind( vasc.rich.groups.within[1,], vasc.repl.groups.within[1,])
rownames(vasc.within.rich.repl)<-c("Rich", "Repl")

vasc.diff.rich.repl<-rbind( vasc.rich.groups.diff[1,], vasc.repl.groups.diff[1,])
rownames(vasc.diff.rich.repl)<-c("Rich", "Repl")

Relationships within and between different clusters

#dev.new(width=11.8, height=6)
layout(matrix(1:2,ncol=2), width = c(1,1.75),height = c(1,1))
#par(mfrow=c(1,2))
barplot(as.matrix(vasc.within.rich.repl), cex.lab=1.25, ylim=c(0,1), ylab="Relative contribution to beta diversity", xlab="Within cluster")
legend("topleft", c("Richness", "Replacement"), col = c("grey85", "grey35" ), cex=1.1,
      lty = c(1, 1), pch = c(NA, NA), lwd=10 , bg = "white", bty="n")
barplot(as.matrix(vasc.diff.rich.repl), cex.lab=1.25, ylim=c(0,1), ylab="Relative contribution to beta diversity", xlab="Cluster combinations")

#dev.new(width=5.9, height=8)
par(mfrow=c(3,2))
barplot(as.matrix(vasc.within.between.1), cex.lab=1.25, ylim=c(0,1), ylab="Relative contribution to beta diversity", xlab="Clusters")
barplot(as.matrix(vasc.within.between.2), cex.lab=1.25, ylim=c(0,1), ylab="Relative contribution to beta diversity", xlab="Clusters")
barplot(as.matrix(vasc.within.between.3), cex.lab=1.25, ylim=c(0,1), ylab="Relative contribution to beta diversity", xlab="Clusters")
barplot(as.matrix(vasc.within.between.4), cex.lab=1.25, ylim=c(0,1), ylab="Relative contribution to beta diversity", xlab="Clusters")
barplot(as.matrix(vasc.within.between.5), cex.lab=1.25, ylim=c(0,1), ylab="Relative contribution to beta diversity", xlab="Clusters")
plot.new()
legend("topleft", c("Richness", "Replacement"), col = c("grey85", "grey35" ), cex=1.1,
      lty = c(1, 1), pch = c(NA, NA), lwd=10 , bg = "white", bty="n")