#Appendix S4: R code for MCMCglmm analyses #set working directory setwd("file/path/here") ########## #LIBRARIES ########## library(MCMCglmm) #ape is required ##### #DATA ##### #read in data (appendix S1) TDdata<-read.csv("PartitioningDynamics2.csv",header=T) TDdata<-TDdata[-which(TDdata$Type=="Alga"|TDdata$Type=="Epiphyte"),] #read in phylogenetic tree (appendix S2) fulltree<-read.nexus("Compadre_bl_280714.nex") all(TDdata$Species%in%fulltree$tip.label) #We have a problem. A bunch of Species aren't in the phylogeny. Which ones? (missing<-as.character(unique(TDdata$Species[which(!TDdata$Species%in%fulltree$tip.label)]))) #can we find any using fuzzy matching? Maybe there are typos fuzzymatches<-as.list(numeric(length(missing))) for(i in 1:length(missing)){ if(length(agrep(missing[i],fulltree$tip.label))>0){ fuzzymatches[[i]]<-fulltree$tip.label[agrep(missing[i],fulltree$tip.label)] } } fuzzymatches #there's more than one match per species (fuzzymatches<-lapply(fuzzymatches,sort)) #the first one of each set works fuzzymatches<-sapply(fuzzymatches,function(x){x<-x[1]}); fuzzymatches[fuzzymatches=="0"]<-NA Original<-missing Alternative<-fuzzymatches #There are still some species missing. Which ones? stillmissing<-as.character(Original[is.na(Alternative)]) #Maybe they have matching Genuses stillmissingGenuses<-sapply(strsplit(stillmissing,"_"),function(x){x[1]}) Genusmatches<-as.list(numeric(length(stillmissing))) for(i in 1:length(stillmissingGenuses)){ if(length(agrep(stillmissingGenuses[i],fulltree$tip.label))>0){ Genusmatches[[i]]<-fulltree$tip.label[agrep(stillmissingGenuses[i],fulltree$tip.label)] } } Genusmatches #Maximum one match per species this time Genusmatches<-paste(Genusmatches); Genusmatches[Genusmatches=="0"]<-NA for(i in 1:length(stillmissing)){ Alternative[Original==stillmissing[i]]<-Genusmatches[i] } #There are STILL some species missing. Which ones? (STILLmissing<-as.character(Original[is.na(Alternative)])) #Googling informs that this is a fern genus #It has some synonyms length(grep("Camptosorus",fulltree$tip.label)) length(grep("Ceterach",fulltree$tip.label)) length(grep("Loxoscaphe",fulltree$tip.label)) length(grep("Phyllitis",fulltree$tip.label)) length(grep("Tarachia",fulltree$tip.label)) #Nope, it's not there. Let's take it out the data TDdata<-TDdata[-which(TDdata$Species%in%STILLmissing),] (Original<-Original[-which(Original%in%STILLmissing)]) (Alternative<-Alternative[-which(is.na(Alternative))]) Speciesnew<-as.character(TDdata$Species) for(i in 1:length(Alternative)){ Speciesnew[Speciesnew==Original[i]]<-Alternative[i] } TDdata<-cbind(TDdata,Speciesnew,animal=Speciesnew) all(TDdata$animal%in%fulltree$tip.label) #We need to drop the Species that aren't in our data. drop<-fulltree$tip.label[which(!fulltree$tip.label%in%TDdata$animal)] length(drop)+length(unique(TDdata$animal))==length(fulltree$tip.label) #Trim tree of Species we don't need smalltree<-drop.tip(fulltree, drop) all(sort(levels(TDdata$animal))==sort(smalltree$tip)) #Finally we have a little tree smalltree$node.label<-as.character(1:length(smalltree$node.label)) #There is a problem with MCMCglmm if the node labels aren't unique any(TDdata[,c("Rel.Cont.Transients","Rel.Cont.VR","Mean.Prop.Transients")]==1) which(TDdata[,c("Rel.Cont.Transients","Rel.Cont.VR","Mean.Prop.Transients")]==1,arr.ind=T) any(TDdata[,c("Rel.Cont.Transients","Rel.Cont.VR","Mean.Prop.Transients")]==1) #There is one entry for Rel.Cont.Transients which equals 1 #This creates problems for the analysis #We will correct to approximately 1 TDdata$Rel.Cont.Transients[172]<-0.999 nitt<-500000 burnin<-0.1*nitt thin<-0.0005*nitt ######################## #WITH PHYLOGENY (mX.1.X) ######################## logit<-function(x){log(x/(1-x))} #Rel.Cont.Transients prior1.1<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100), G2=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m1.1.1<-MCMCglmm(logit(Rel.Cont.Transients) ~ Dim.matrices + Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior1.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m1.1.2<-MCMCglmm(logit(Rel.Cont.Transients) ~ Dim.matrices, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior1.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m1.1.3<-MCMCglmm(logit(Rel.Cont.Transients) ~ Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior1.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m1.1.4<-MCMCglmm(logit(Rel.Cont.Transients) ~ 1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior1.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Rel.Cont.VR prior2.1<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100), G2=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m2.1.1<-MCMCglmm(logit(Rel.Cont.VR) ~ Dim.matrices + Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior2.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m2.1.2<-MCMCglmm(logit(Rel.Cont.VR) ~ Dim.matrices, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior2.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m2.1.3<-MCMCglmm(logit(Rel.Cont.VR) ~ Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior2.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m2.1.4<-MCMCglmm(logit(Rel.Cont.VR) ~ 1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior2.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Mean.Prop.Transients prior3.1<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100), G2=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m3.1.1<-MCMCglmm(logit(Mean.Prop.Transients) ~ Dim.matrices + Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior3.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m3.1.2<-MCMCglmm(logit(Mean.Prop.Transients) ~ Dim.matrices, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior3.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m3.1.3<-MCMCglmm(logit(Mean.Prop.Transients) ~ Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior3.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m3.1.4<-MCMCglmm(logit(Mean.Prop.Transients) ~ 1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior3.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Stochastic.Growth prior4.1<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100), G2=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m4.1.1<-MCMCglmm(Stochastic.Growth ~ Dim.matrices + Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior4.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m4.1.2<-MCMCglmm(Stochastic.Growth ~ Dim.matrices, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior4.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m4.1.3<-MCMCglmm(Stochastic.Growth ~ Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior4.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m4.1.4<-MCMCglmm(Stochastic.Growth ~ 1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior4.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Tot.dynamics prior5.1<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100), G2=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m5.1.1<-MCMCglmm(Tot.dynamics ~ Dim.matrices + Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior5.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m5.1.2<-MCMCglmm(Tot.dynamics ~ Dim.matrices, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior5.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m5.1.3<-MCMCglmm(Tot.dynamics ~ Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior5.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m5.1.4<-MCMCglmm(Tot.dynamics ~ 1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior5.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Mean.Transient prior6.1<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100), G2=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m6.1.1<-MCMCglmm(Mean.Transient ~ Dim.matrices + Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior6.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m6.1.2<-MCMCglmm(Mean.Transient ~ Dim.matrices, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior6.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m6.1.3<-MCMCglmm(Mean.Transient ~ Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior6.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m6.1.4<-MCMCglmm(Mean.Transient ~ 1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior6.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Mean.Vital.Rate prior7.1<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100), G2=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m7.1.1<-MCMCglmm(Mean.Vital.Rate ~ Dim.matrices + Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior7.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m7.1.2<-MCMCglmm(Mean.Vital.Rate ~ Dim.matrices, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior7.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m7.1.3<-MCMCglmm(Mean.Vital.Rate ~ Type -1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior7.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m7.1.4<-MCMCglmm(Mean.Vital.Rate ~ 1, random=~Speciesnew + animal, family="gaussian", data=TDdata, pedigree=smalltree, nodes="TIPS", prior=prior7.1, thin=thin, nitt=nitt, burnin=burnin, verbose=F) ########################### #WITHOUT PHYLOGENY (mX.2.X) ########################### #Rel.Cont.Transients prior1.2<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m1.2.1<-MCMCglmm(logit(Rel.Cont.Transients) ~ Dim.matrices + Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior1.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m1.2.2<-MCMCglmm(logit(Rel.Cont.Transients) ~ Dim.matrices, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior1.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m1.2.3<-MCMCglmm(logit(Rel.Cont.Transients) ~ Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior1.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m1.2.4<-MCMCglmm(logit(Rel.Cont.Transients) ~ 1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior1.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Rel.Cont.VR prior2.2<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m2.2.1<-MCMCglmm(logit(Rel.Cont.VR) ~ Dim.matrices + Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior2.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m2.2.2<-MCMCglmm(logit(Rel.Cont.VR) ~ Dim.matrices, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior2.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m2.2.3<-MCMCglmm(logit(Rel.Cont.VR) ~ Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior2.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m2.2.4<-MCMCglmm(logit(Rel.Cont.VR) ~ 1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior2.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Mean.Prop.Transients prior3.2<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m3.2.1<-MCMCglmm(logit(Mean.Prop.Transients) ~ Dim.matrices + Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior3.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m3.2.2<-MCMCglmm(logit(Mean.Prop.Transients) ~ Dim.matrices, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior3.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m3.2.3<-MCMCglmm(logit(Mean.Prop.Transients) ~ Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior3.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m3.2.4<-MCMCglmm(logit(Mean.Prop.Transients) ~ 1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior3.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Stochastic.Growth prior4.2<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m4.2.1<-MCMCglmm(Stochastic.Growth ~ Dim.matrices + Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior4.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m4.2.2<-MCMCglmm(Stochastic.Growth ~ Dim.matrices, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior4.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m4.2.3<-MCMCglmm(Stochastic.Growth ~ Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior4.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m4.2.4<-MCMCglmm(Stochastic.Growth ~ 1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior4.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Tot.dynamics prior5.2<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m5.2.1<-MCMCglmm(Tot.dynamics ~ Dim.matrices + Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior5.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m5.2.2<-MCMCglmm(Tot.dynamics ~ Dim.matrices, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior5.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m5.2.3<-MCMCglmm(Tot.dynamics ~ Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior5.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m5.2.4<-MCMCglmm(Tot.dynamics ~ 1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior5.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Mean.Transient prior6.2<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m6.2.1<-MCMCglmm(Mean.Transient ~ Dim.matrices + Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior6.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m6.2.2<-MCMCglmm(Mean.Transient ~ Dim.matrices, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior6.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m6.2.3<-MCMCglmm(Mean.Transient ~ Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior6.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m6.2.4<-MCMCglmm(Mean.Transient ~ 1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior6.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #Mean.Vital.Rate prior7.2<-list(R = list(V = 1, nu=0.001), G = list(G1=list(V = 1, nu=0.001, alpha.mu=0, alpha.V=100))) m7.2.1<-MCMCglmm(Mean.Vital.Rate ~ Dim.matrices + Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior7.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m7.2.2<-MCMCglmm(Mean.Vital.Rate ~ Dim.matrices, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior7.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m7.2.3<-MCMCglmm(Mean.Vital.Rate ~ Type -1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior7.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) m7.2.4<-MCMCglmm(Mean.Vital.Rate ~ 1, random=~Speciesnew, family="gaussian", data=TDdata, nodes="TIPS", prior=prior7.2, thin=thin, nitt=nitt, burnin=burnin, verbose=F) #results of these analyses are in appendix S5 #(subsets of models presented in the manuscript are not presented)