# R x64 3.2.3 written by Ali Khalighifar and Laura Jiménez on June, 12, 2017 # The last edit was on Oct, 26, 2017 by Ali Khalighifar # This script generates the species population using lognormal distribution in suitable format of EstimateS version 9. ### Generates sampling days, 100 real species, 10 error species, and 100 replicates binsize=c(5,7,10,15,20,25,50,75,100,125,150,175,200,300,400,500,600,700,800,900,1000) nsp <- 100 nerr <- 10 nsim <- 100 # Select the parameters of the lognormal distribution (mean, standard deviation, error abundance value, etc.) meanl <- 1.5 sdl <- 2 lam <- rlnorm(nsp,meanlog=meanl,sdlog=sdl) per.ln <- 0.000001 lam.error <- qlnorm(per.ln, meanlog =meanl, sdlog =sdl) lams <- c(lam,rep(lam.error,nerr)) ##loop over each binsize (sampling days) and export a text file for each one setwd("working Directory") loop=0 for(k in binsize){ input_file <- file(paste0(paste(nsp+nerr,"sp",per.ln, "err", nsim,"sim",k,"days",sep="_"),".txt"), open = "at") loop=loop+1 for(j in 1:nsim){ repmatrix=matrix(nrow=(nsp+nerr),ncol=k) for(i in 1:(nsp+nerr)) # abundance matrix repmatrix[i,]=rpois(k,lams[i]) # generate incidence matrix repmatrix <- (repmatrix>0)*1 if(j==1){ # Design the format of input files for EstimateS cat("*MultipleSampleSets*\t", nsim, "\t", paste0(k, "_days"), "\n", file = input_file, sep = "")} cat(paste("rep", j, sep = " "), "\t*SampleSet*\t", "1\t", "0\t", "0\n", file = input_file, sep = "") cat(nsp+nerr, "\t", k, "\n", file = input_file, sep = "") write.table(repmatrix,file=input_file, sep = "\t", row.names=F, col.names=F, append = T) } flush(input_file) close(input_file) }