##### FOGGING ############################### ##### TESTS FOR STRUCTURE WITHIN TREES ###### rm(list=ls(all=TRUE)) setwd("C:\\Users\\Tom\\Dropbox\\Research\\Submitted manuscripts\\Ant mosaics in the high canopy of tropical rain forest (Kalsum)\\PeerJ submission\\Data and code for uploading") #change the default folder require(vegan) #you need to have the vegan package for this to work data<-read.table(file="Fogging data.txt",header=T) #looks for your file data #shows your data as.factor(data$Tree)->data$Tree as.factor(data$Pan)->data$Pan n.trees<-nlevels(data$Tree) #number of trees n.pans<-nlevels(data$Pan) #pans per tree nsp<-length(data[1,])-2 #number of species cu.obs<-0 #this will work out the number of checkerboard units in the observed data for (i in 1:n.trees){ #across all of the trees - do the following... nestedchecker(data[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.obs->cu.obs #take the pans within a tree and works out the number of checkerboard units (CUs) within each tree then adds them all up } cu.obs # the total number of CUs in the observed data within trees rand<-data #creates another matrix the same as data and names it "rand", this will recieve the randomised data matrixes nrep=1000 # the number of randomisations that you want to use - the higher this is the longer it will take to run, but the more precise the p-value will be null.within<-vector(length=nrep) # creates a vector to receive the CU counts for the simulated (randomised) matrices for (j in 1:nrep){ # for every replicate cu.sim<-0 # take cu.sim back to zero for (i in 1:n.trees){ # for every tree within every replicate commsimulator(data[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2],method="quasiswap")->rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2] #randomise the data matrix for that tree while keeping the number of species in each pan the same, and the number of occurrences of each species also the same nestedchecker(rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.sim->cu.sim #work out the number of CUs for that tree and then go through and add them all up } cu.sim->null.within[j] # for that replication of the simulation, put the number of CUs observed into null.within } #hist(null.within) #draw a histogram of null.within #abline (v=cu.obs) #adds a vertical line where your observed number of CUs is write.table(null.within,file="Mosaics fogging within.out.txt") #writes the simulated CU counts to a text file at this location - you can use this data file to draw a histogram of your results p.upper.within<-length(subset(null.within,null.within>cu.obs))/nrep #works out the p-value for there being significantly more CUs than you would expect at random p.lower.within<-length(subset(null.within,null.withinselected.trees data[selected.trees,]->diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),] } diff.tree.pans cu.between.obs<-0 # the code from here down is just copied from above with some minor changes - it tests the within "tree" cu counts for significance, so depending on how many constructed datasets you use you will end up with that many p-values for (i in 1:n.trees){ nestedchecker(diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.between.obs->cu.between.obs } rand.between<-data null.between<-vector(length=nrep) for (j in 1:nrep){ cu.between.sim<-0 for (i in 1:n.trees){ commsimulator(diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2],method="quasiswap")->rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2] nestedchecker(rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.between.sim->cu.between.sim } cu.between.sim->null.between[j] } p.upper.between[k]<-length(subset(null.between,null.between>cu.between.obs))/nrep p.lower.between[k]<-length(subset(null.between,null.betweendata$Tree as.factor(data$Pan)->data$Pan n.trees<-nlevels(data$Tree) #number of trees n.pans<-nlevels(data$Pan) #pans per tree nsp<-length(data[1,])-2 #number of species data[,3:nsp+2][data[,3:nsp+2]>0]<-1 cu.obs<-0 #this will work out the number of checkerboard units in the observed data for (i in 1:n.trees){ #across all of the trees - do the following... nestedchecker(data[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.obs->cu.obs #take the pans within a tree and works out the number of checkerboard units (CUs) within each tree then adds them all up } cu.obs # the total number of CUs in the observed data within trees rand<-data #creates another matrix the same as data and names it "rand", this will recieve the randomised data matrixes nrep=1000 # the number of randomisations that you want to use - the higher this is the longer it will take to run, but the more precise the p-value will be null.within<-vector(length=nrep) # creates a vector to receive the CU counts for the simulated (randomised) matrices for (j in 1:nrep){ # for every replicate cu.sim<-0 # take cu.sim back to zero for (i in 1:n.trees){ # for every tree within every replicate commsimulator(data[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2],method="quasiswap")->rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2] #randomise the data matrix for that tree while keeping the number of species in each pan the same, and the number of occurrences of each species also the same nestedchecker(rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.sim->cu.sim #work out the number of CUs for that tree and then go through and add them all up } cu.sim->null.within[j] # for that replication of the simulation, put the number of CUs observed into null.within } cu.obs #hist(null.within) #draw a histogram of null.within #abline (v=cu.obs) #adds a vertical line where your observed number of CUs is write.table(null.within,file="Mosaics baiting night within.out.txt") #writes the simulated CU counts to a text file at this location - you can use this data file to draw a histogram of your results p.upper.within<-length(subset(null.within,null.within>cu.obs))/nrep #works out the p-value for there being significantly more CUs than you would expect at random p.lower.within<-length(subset(null.within,null.withinselected.trees data[selected.trees,]->diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),] } diff.tree.pans cu.between.obs<-0 # the code from here down is just copied from above with some minor changes - it tests the within "tree" cu counts for significance, so depending on how many constructed datasets you use you will end up with that many p-values for (i in 1:n.trees){ nestedchecker(diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.between.obs->cu.between.obs } rand.between<-data null.between<-vector(length=nrep) for (j in 1:nrep){ cu.between.sim<-0 for (i in 1:n.trees){ commsimulator(diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2],method="quasiswap")->rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2] nestedchecker(rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.between.sim->cu.between.sim } cu.between.sim->null.between[j] } p.upper.selected[k]<-length(subset(null.between,null.between>cu.between.obs))/nrep p.lower.selected[k]<-length(subset(null.between,null.betweendata$Tree as.factor(data$Pan)->data$Pan n.trees<-nlevels(data$Tree) #number of trees n.pans<-nlevels(data$Pan) #pans per tree nsp<-length(data[1,])-2 #number of species data[,3:nsp+2][data[,3:nsp+2]>0]<-1 cu.obs<-0 #this will work out the number of checkerboard units in the observed data for (i in 1:n.trees){ #across all of the trees - do the following... nestedchecker(data[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.obs->cu.obs #take the pans within a tree and works out the number of checkerboard units (CUs) within each tree then adds them all up } cu.obs # the total number of CUs in the observed data within trees rand<-data #creates another matrix the same as data and names it "rand", this will recieve the randomised data matrixes nrep=1000 # the number of randomisations that you want to use - the higher this is the longer it will take to run, but the more precise the p-value will be null.within<-vector(length=nrep) # creates a vector to receive the CU counts for the simulated (randomised) matrices for (j in 1:nrep){ # for every replicate cu.sim<-0 # take cu.sim back to zero for (i in 1:n.trees){ # for every tree within every replicate commsimulator(data[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2],method="quasiswap")->rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2] #randomise the data matrix for that tree while keeping the number of species in each pan the same, and the number of occurrences of each species also the same nestedchecker(rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.sim->cu.sim #work out the number of CUs for that tree and then go through and add them all up } cu.sim->null.within[j] # for that replication of the simulation, put the number of CUs observed into null.within } #hist(null.within) #draw a histogram of null.within #abline (v=cu.obs) #adds a vertical line where your observed number of CUs is write.table(null.within,file="Mosaics baiting day within.out.txt") #writes the simulated CU counts to a text file at this location - you can use this data file to draw a histogram of your results p.upper.within<-length(subset(null.within,null.within>cu.obs))/nrep #works out the p-value for there being significantly more CUs than you would expect at random p.lower.within<-length(subset(null.within,null.withinselected.trees data[selected.trees,]->diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),] } diff.tree.pans cu.between.obs<-0 # the code from here down is just copied from above with some minor changes - it tests the within "tree" cu counts for significance, so depending on how many constructed datasets you use you will end up with that many p-values for (i in 1:n.trees){ nestedchecker(diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.between.obs->cu.between.obs } rand.between<-data null.between<-vector(length=nrep) for (j in 1:nrep){ cu.between.sim<-0 for (i in 1:n.trees){ commsimulator(diff.tree.pans[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2],method="quasiswap")->rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2] nestedchecker(rand[(i*n.pans-n.pans+1):(i*n.pans),1:nsp+2])$statistic+cu.between.sim->cu.between.sim } cu.between.sim->null.between[j] } p.upper.selected[k]<-length(subset(null.between,null.between>cu.between.obs))/nrep p.lower.selected[k]<-length(subset(null.between,null.between