0

I'm trying to write a basic model that simulates the growth of a population (whose initial size is drawn randomly from a normal distribution) and then grows by a user defined amount each 'year' (currently 2 individuals in the code below for arguments sake). The output that is produced only shows the results of one simulation and, within this simulation, the population hasn't grown at all i.e. for each 'year' the population hasn't grown/doesn't add to the previous 'years' population. I'm assuming that I've stuffed something up in the loop structure and keen for any advice!

n.years <- 3
n.sim <- 5
store.growth <- matrix(ncol=3,nrow= (n.years * n.sim))

for (i in 1:n.sim) {
 init.pop.size <- rnorm(1,100,10)
 for (j in 1:n.years){
    #grow population
    grow.pop <- init.pop.size + 5
    store.growth[j,] <- cbind(grow.pop, n.years, n.sim)
 }
}
store.growth
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • You want to use `c` instead of `cbind` to build your output row. – Thomas Jul 06 '13 at 20:09
  • Also, the way you're storing your results means that the last run of the inner for-loop overwrites any previous results because you're just using `j` as an index, without accounting for which of `n.years` you're in. – Thomas Jul 06 '13 at 20:10
  • Also, if your simulation is supposed to build on the previous run of the simulation, `grow.pop <- init.pop.size + 5` should probably include ` + grow.pop`, with that variable initialized to zero at some point. – Thomas Jul 06 '13 at 20:11

0 Answers0