
setwd("C:/Users/Mike/Dropbox/To do/Chapter 1/R")


require(convevol)
require(ape)
require(MASS)
require(geiger)
require(phytools)


####Data######
tree<- read.nexus("Chapter 1_trees.nexus") #phylogenetic tree
dat <- read.csv("Chapter 1_data_all PCs.csv",row.names=1)#Morphological 
phendata<- as.matrix(dat[,1:2])
convtips<- row.names(dat[which(dat[,7] == "red"),])

TreeOnly <- setdiff(tree$tip.label,rownames(dat)) #checks to make sure the tips of the phylogeny matches the morpho data

DataOnly <- setdiff(rownames(dat), tree$tip.label) #checks to make sure the tips of the phylogeny matches the morpho data

#########################################
#Prune Tree
########################################

phyl <- drop.tip(tree,TreeOnly)

####Test number of lineages that convergence######
 convnum(phyl, phendata, convtips, plot = TRUE, ellipse = NULL, plotellipse = NULL)


####convergence significance piscivores
convtips<- row.names(dat[which(dat[,7] == "red"),])
convrat(phyl, phendata, convtips)
convratsig(phyl, phendata, convtips, 500)

####convergence significance detritivores
convtipsp<- row.names(dat[which(dat[,7] == "purple"),])
p<- convrat(phyl, phendata, convtipsp)
psig<-convratsig(phyl, phendata, convtipsp, 500)

####convergence significance piscivores
convtipso<- row.names(dat[which(dat[,7] == "orange"),])
o<- convrat(phyl, phendata, convtipso)
osig<- convratsig(phyl, phendata, convtipso, 500)

####convergence significance piscivores
convtipsy<- row.names(dat[which(dat[,7] == "yellow"),])
y<- convrat(phyl, phendata, convtipsy)
ysig<- convratsig(phyl, phendata, convtipsy, 500)

####convergence significance piscivores
convtipsg<- row.names(dat[which(dat[,7] == "green"),])
g<- convrat(phyl, phendata, convtipsg)
gsig<- convratsig(phyl, phendata, convtipsg, 500)

####convergence significance piscivores
convtipsb<- row.names(dat[which(dat[,7] == "blue"),])
b<-convrat(phyl, phendata, convtipsb)
bsig<-convratsig(phyl, phendata, convtipsb, 500)




answer2<-convnumsig(phyl, phendata, convtips, 1000, ellipse = NULL, plot = TRUE, plotellipse = NULL)

convrat(phyl, phendata, convtips)

answerpurple<- convratsig(phyl, phendata, convtips, 1000)
	