## Annotated (##) code to run the simple random sampling null model ## (Model 1) in the R statistical package (http://www.r-project.org/) ## ## Read presence (1) absence (0) matrix of species occurrence, ## with species in rows and samples in columns ## (in the current case, samples are islands). ## One row is reserved for headers. newdata <- read.csv("datafile.csv") ## Create vector of sample totals: total number of species to be drawn. SpeciesSample <- colSums(newdata) ## Display vector of species totals by sample (island). SpeciesSample ## Set the number of iterations: number of times to conduct the ## sampling. Iterations <- 1000 ## Create a vector that stores the number of endemics drawn in ## iteration. HistoResults <- rep(0,Iterations) ## Create a matrix with rows for the SpeciesSamples, and columns for ## the mean (1), 95% low (2) and 95% high (3) confidence intervals, and ## the observed species counts (4). Summ <- matrix(0,nrow=length(SpeciesSample),ncol=4) ## Create a vector of species occurrences. The "1"s represent endemics ## in the system. This works for abundance, presence-absence and ## archipelago data in the same way. weights <- rowSums(newdata) weights ## Begin iterations to resample each sample (island) in the matrix. ## ----------------------------------------------------------------## for (j in 1:length(SpeciesSample)) { ## loop for each sample. ## FracEndemic is used for counts, but can rescale for ## proportions. FracEndemic <- 0 ## Sampling without replacement, draw the appropriate ## number of species, using weights to scale probabilities. for (i in 1:Iterations) { ## loop for each iteration. RandomDraw <- sample(weights, size=SpeciesSample[j], replace=F, prob=weights) ## Evaluate inequality to tally up the 1's (endemics). FracEndemic <- sum(RandomDraw == 1) ## Arcsine transformation of endemic proportions. FracEndemic <- asin(FracEndemic/SpeciesSample[j]) ## Store result and move to next iteration. HistoResults[i] <- FracEndemic } Endemism in the Hawaiian biota - 22 Summ[j,] <- c(mean(HistoResults),quantile(HistoResults, c(0.025,0.975)),0) } ## Loop through stored results for each sample and iteration to extract ## summary data. for (j in 1:ncol(newdata)) Summ[j,4] <- 0 ## Extract overall means and confidence intervals. endemics <- rowSums(newdata) for (j in 1:ncol(newdata)) { for (i in 1:nrow(newdata)) { if(newdata[i,j] == 1 & (endemics[i] == 1)) Summ[j,4] <- Summ[j,4] + 1.0 } } ##------------------------------------------------------------------## ## Extract column and row names to print into output matrix. newdata <- data.frame(newdata) ISLE <- names(newdata) SpeciesSample1 <- data.frame(SpeciesSample) row.names(SpeciesSample1) <- ISLE print(row.names(SpeciesSample1)) ## Create summary matrix to store results. Summ1 <- matrix(data=Summ,nrow=length(ISLE),ncol=4, dimnames= list(ISLE, c("exp_asinprop", "a95low", "a95high", "endemics"))) ## Display summary matrix. Summ1 ## Export summary table to comma delimited file. write.csv(Summ1, file = "C:/directory/file.csv") ## end ##