library(paleotree) library(ape) # Requires a file of time bin ranges called "Ranges.txt" # Requires a file of taxon first and last appearance bins based on the Ranges file called "Taxa.txt" ##### DATING USING CAL3 ##### # Load in geological stages Ranges<-read.table("Ranges.txt", header=TRUE, row.names=1) # Load in First and Last Appearance dates taxa<-read.table("Taxa.txt", header=TRUE, row.names=1) # Get into the right format data<-list(Ranges, taxa) # ML optimisation for sampling probability and extinction rates SPres<-getSampProbDisc(data) # Establish means for interval length intLength<--apply(data[[1]],1,diff) meanInt<-mean(intLength) # Calculate sampling rate sRate<-sProb2sRate(SPres[[2]][2], int.length=meanInt) # Calculate diversification and extinction rate divRate<-SPres[[2]][1]/meanInt # Load trees for cal3 trees<-read.nexus("trees.nex") # Get consensus of trees contree<-consensus(trees) # Date trees with cal3 datedtrees<-bin_cal3TimePaleoPhy(CFO, data, brRate=divRate, extRate=divRate, sampRate=sRate, ntrees=1000, plot=FALSE) #### NB This will randomise polytomies 1000 times #### ########## RATES ANALYSIS ########### # Read Matrix matrix<-ReadMorphNexus("Discrete Matrix.nex") # Ensure no falsely uncoded characters if (any(matrix$max.vals == NA)=TRUE) { matrix$max.vals[is.na(x)] <- 1 matrix$min.vals[is.na(x)] <- 0 } # Ensure no zero-length branches by adding smallest possible duration (0.01 years, or 3.65 days). This allows the function to work without really affecting the dating. for (i in 1:1000) { datedtrees[[i]]$edge.length<-(datedtrees[[i]]$edge.length+0.00000001) } # Generate lists in which the rates and significances are going to go tree.per.bin.rates<-list tree.per.bin.sig<-list # Calculate rates for each tree at alpha level of 0.01 for (i in 1:1000) { treerates<-DiscreteCharacterRate(datedtrees[[i]], matrix, Ranges[,1], alpha=0.01) tree.per.bin.rates<-cbind(tree.per.bin.rates, treerates$per.bin.rates[,4]) tree.per.bin.sig<-cbind(tree.per.bin.sig, treerates$per.bin.rates[,8]) }