pearselab / r-intro-Spencerbrucehudson

r-intro-Spencerbrucehudson created by GitHub Classroom
0 stars 0 forks source link

How I would simulate a community #6

Open willpearse opened 7 years ago

willpearse commented 7 years ago
sim.comm <- function(spp.lam, spp.p, spp.names, n.sites){
    #Make a matrix that you're going to output
    #Loop over all the species
        #Use your species simulation function and put that in the right column in your matrix
    #End loop
    #Do any cleanup you want on the matrix and return
}
willpearse commented 7 years ago
matrix[rows, col]

...can be used to subset

Spencerbrucehudson commented 7 years ago

Hey Dr. Pearse,

I know this isn't what you suggested, since I didn't quite understand the matrix approach, but is it suitable?

Spencerbrucehudson commented 7 years ago

Still not entirely comfortable with its assumption that if a species is not present at one site, then it's absent at all the others. Any recommendations for that?

willpearse commented 7 years ago

Your approach is fine. I am surprised you know how to use do.call, but I suppose this is what comes from using plyr et al.!

As far as your assumption, I wouldn't be either - you've made a slight slip. It's not whether a species is present at a single site that determines all sites. The sequence is:

  1. Pick a site
  2. Is it present? (rbinom)
  3. If it's present, what's its abundance (rpois)
  4. Go back to 1 - start over on the next site.

...thus you go through the whole 'hurdle' process for each site. Does that make sense? I'm also online now on appear.in if you want to talk with me.

Spencerbrucehudson commented 7 years ago

Yeah I think so. Would a repeat loop work if I implemented it into the original function?

Spencerbrucehudson commented 7 years ago

Nevermind, fixed it. Is this is alright?

willpearse commented 7 years ago
hurdle2 <- function(sitenum, spnum){
  r <- matrix(nrow=sitenum, ncol=spnum)
  for(i in 1:spnum){
   r[,i] <- hurdle1(sitenum)
    }
  return(r)
    }
hurdle2(10, 7)

...right? Looks to be! Welcome to the matrix club. Now, isn't that easier than dplyr?... :p