Loop structure for basic simulation model in R

孤街浪徒 提交于 2019-12-24 23:22:40

问题


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

来源:https://stackoverflow.com/questions/17101738/loop-structure-for-basic-simulation-model-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!