[R-sig-eco] Stochastic Model Gives Deterministic Output
Ben Bolker
bbolker at gmail.com
Mon Dec 27 03:50:12 CET 2010
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Works for me?
occ <- c(2,5,3)
n1 <- nullModel.spsite(occ, iter=5, plots=10)
n2 <- nullModel.spsite(occ, iter=5, plots=10)
n1[,,1]
n2[,,1]
all(n1[,,1]==n2[,,1]) ## FALSE
Can you show an example that doesn't work?
(This is with R 2.12.1, but you're using pretty basic/fundamental
components: I'd be surprised if R had changed in such a way as to change
the function of this code in the meantime.)
You can use the time as the seed for random number generation as follows:
set.seed(as.integer(Sys.time()))
... but it's probably a much better idea to set a particular (known)
random number seed at the beginning of each run, so that the runs are
reproducible. If you really want to do this I would recommend something
like
cat(st <- as.integer(Sys.time()),"\n")
set.seed(st)
so that you can reproduce results if necessary.
On 10-12-24 06:29 PM, Jane Shevtsov wrote:
> 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)
> }
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk0X/uQACgkQc5UpGjwzenNDBACbB9uXtM4voD6jNef+6PF7Y+En
V8gAnRHQvMUhfShYkvf806wnwMCKVuMQ
=ANB2
-----END PGP SIGNATURE-----
More information about the R-sig-ecology
mailing list