[R-sig-eco] Stochastic Model Gives Deterministic Output

Jane Shevtsov jane.eco at gmail.com
Sat Dec 25 00:29:00 CET 2010


I'm using R 2.10.1 (running on Ubuntu) to implement a simple null
model. The model (code below) assigns species to plots by shuffling
the numbers 1-# of plots and assigning the species to the first N
plots in the resulting vector, where N is the number of plots at which
the species was found in real life. The code works fine, except for
one problem -- different runs give exactly the same results! (Each
matrix is different, but matrix 1 of run 1 is identical to matrix 1 of
run 2.) How do I fix this? Is it possible to get R to use the time as
the seed for random number generation?

Thanks,
Jane Shevtsov

#input: vector of species occurences, # of iterations, #plots at site

nullModel.spsite <- function(occ, iter, plots) {
	spp <- length(occ)
	#Allocate array for co-occurrence frequencies of each species pair.
	sitespArr <- array(0, c(spp, plots, iter))
	#Pre-allocate other stuff
	sitesp <- array(0, c(plots, spp)) #Preserve matrix orientation to
minimize recoding; transpose later
	order <- array(0, plots)

	for (i in 1:iter) {
		#Zero out site-sp matrix
		sitesp <- array(0, c(plots, spp))
		for (j in 1:spp) {
			#Randomly shuffle plots
			order <- sample(1:plots, plots, replace=FALSE)
			#Put sp i in first k (# plots where species i occurs) plots
according to order vector
			sitesp[order[1:occ[j]], j] = 1
		}
		sitespArr[,,i] = t(sitesp)
	}
	return(sitespArr)	
}



-- 
-------------
Jane Shevtsov
Ecology Ph.D. candidate, University of Georgia
co-founder, <www.worldbeyondborders.org>
Check out my blog, <http://perceivingwholes.blogspot.com>Perceiving Wholes

"The whole person must have both the humility to nurture the
Earth and the pride to go to Mars." --Wyn Wachhorst, The Dream
of Spaceflight



More information about the R-sig-ecology mailing list