d<-dist(X)
stree<-spantree(d)
depths<-spandepth(stree)
plot(stree,type="t",label=depths)
sum(stree[[2]])
StatsMST<-function(X,perm=TRUE,tobeshuffled=NULL,Nperm=1000,dist.method="euclid"){
library(vegan)
Nnode<-length(X[,1])
distM<-dist(X,method=dist.method)
MST<-spantree(distM)
if(!perm){
return(list(Nnode=Nnode,distM=distM,MST=MST,L=sum(MST[[2]]),PermL=NULL,perm=perm,Nperm=Nperm,dist.method=dist.method))
}else{
if(is.null(tobeshuffled)){
tobeshuffled<-2:length(X[1,])
}
PermL<-rep(0,Nperm)
for(i in 1:Nperm){
tmpdistM<-dist(ShuffleForPerm(X,tobeshuffled),method=dist.method)
tmpMST<-spantree(tmpdistM)
PermL[i]<-sum(tmpMST[[2]])
}
return(list(Nnode=Nnode,distM=distM,MST=MST,L=sum(MST[[2]]),PermL=PermL,perm=perm,Nperm=Nperm,dist.method=dist.method))
}
}
sMST<-StatsMST(X)
plot.MST.L<-function(mst){
ylim<-range(c(mst$L,mst$PermL))
plot(sort(mst$PermL),ylim=ylim)
abline(h=mst$L)
}
plot.MST.L(sMST)
matplot(X[,1:3],type="l")
plot(as.data.frame(X[,1:3])
plot3d(X[,1:3])