data<-read.table(file="PNG_transects.txt",header=TRUE) head(data) tail(data) PA<-replace(PA<-data,PA>1,1) species_recorded <- NULL hold_temp<-NULL for (i in 1:ncol(PA)){ temp<- grep(1,PA[,i]) hold_temp<-c(hold_temp,temp) species_recorded<-c(species_recorded,length(unique(hold_temp))) } plot(species_recorded, xlab= "Transect number", ylab="Total number of species") N<-100 M<-matrix(NA,nrow=N,ncol=ncol(PA)) # to hold the results of N (100) random sequences for (i in 1:N){ to_add_seq<-sample(1:50, 50,replace=FALSE) species_recorded<-NULL hold_temp<-NULL for (j in to_add_seq){ # automatically steps through all values in the list to_add_seq temp<-grep(1,PA[,j]) hold_temp<-c(hold_temp,temp) species_recorded<-c(species_recorded,length(unique(hold_temp))) } M[i,]<- species_recorded } head(M) plot(NULL,xlim=c(1,50),ylim=c(1,93),xlab="Transect number", ylab="Total number of species") for (i in 1:100) points(c(1:50),M[i,],pch= 15,cex=0.2) points(c(1:50),colMeans(M),col="red",pch=16) lines(smooth.spline(c(1:50),colMeans(M), spar=0.4, all.knots=F),lwd=2, lty=1,col="blue") #### Calculation of Shannon and Simpson indices propPNGbutts<-PNG_butterflies/sum(PNG_butterflies) Simpson<-1-sum(propPNGbutts^2) Simpson ShannonH<--sum(propPNGbutts*log(propPNGbutts,2)) ShannonH species_observed<-NULL; Simpson_transect<-NULL; Shannon_transect<-NULL for (i in 1:length(data)){ temp<-data[which(data[,i]>0),i] species_observed<-c(species_observed,length(temp)) Simpson_transect<-c(Simpson_transect,1-sum((temp/sum(temp))^2)) Shannon_transect<-c(Shannon_transect,-sum(sum((temp/sum(temp)) * log(temp/sum(temp),2))))} par(mfrow=c(2,2)) plot(species_observed,Simpson_transect,xlab="Number of butterflies spp in transect",ylab="Simpson's Index",pch=18,col="cyan3") plot(species_observed,Shannon_transect,xlab="Number of butterflies spp in transect",ylab="Shannon's Index",pch=18,col="chocolate") plot(Shannon_transect,Simpson_transect,xlab="Shannon",ylab="Simpson",col="red") #install.packages("vegan") library(vegan) SW<-diversity(propPNGbutts,index="shannon",MARGIN=1,base=exp(1)) SW ShannonH<--sum(propPNGbutts*log(propPNGbutts)) ShannonH