【问题标题】:Simple loop coding issue简单的循环编码问题
【发布时间】:2011-08-22 12:21:57
【问题描述】:

我是 R 的新用户,我尝试编写一个脚本来模拟物种入侵和社区稳定。我几乎完成了它,我在一个循环中只有一个小问题。

我有 40 个物种(1,2,...),我通过连续入侵创建了一个社区。除非灭绝,否则社区中的物种会离开入侵者池(我设置了密度阈值)。

我想要很多入侵 (>4000),所以我创建了一个 4000 数字在 1 到 40 之间的向量(random.order),但我遇到了一个问题,因为我的物种密度矩阵(init.x)没有与我的向量相同数量的元素。

time<- list(start=0,end=4000,steps=100)
# Initial conditions (set all species to zero in the beginning)
init.x <- runif(n)*0
# generate random order in which species are introduced
init.order<- sample(1:n)
order<-rep(order,100)
random.order<-sample(order,size=length(order))
outt <- init.x
**for (i in 1:4000){
    # Introduce 1 new species (according to vector "random.order") with freq 1000*tol
                # if the species is not yet in the init.x matrix  
    if (init.x[random.order[i]]<tol) {init.x[random.order[i]] <- 1000*tol}**
                # integrate lvm model
    out <-n.integrate(time=time,init.x=init.x,model=lvm)
    # save out and attach it to outt
                  outt <- rbind(outt,out)
    # generate new time window to continue integration
                  time <- list(start=time$end, end = time$end+time$end-time$start,
                     steps=100)
}       

我知道这可能非常简单,但我找不到一种方法来编写循环以使入侵次数超过物种数量(我的矩阵中的原始数量)。

非常感谢,

【问题讨论】:

  • order&lt;-rep(order,100) 行可能会给您带来问题,因为您似乎没有在任何地方初始化 order
  • 另外,如果您知道outt 最终应该得到的大小,最好预先分配它而不是使用rbind 在循环中增加它。
  • 我不理解你的代码。例如,什么是 n? n 是 4000?什么是收费? n.integrate 是什么?如果您提供的信息,也许外面的人可以帮助您,但我需要更多信息来帮助您。

标签: r loops size sample


【解决方案1】:

你可能想要改变

# Initial conditions (set all species to zero in the beginning)
init.x <- runif(n)*0
# generate random order in which species are introduced
init.order<- sample(1:n)
order<-rep(order,100)
random.order<-sample(order,size=length(order))

进入

# Initial conditions (set all species to zero in the beginning)
init.x <- rep.int(0, n) #should be a lot faster
# generate random order in which species are introduced
random.order<-sample.int(n,size=4000, replace=TRUE)

...解决您的主要问题(检查 ?sample)。我没有检查你们其余的代码,但可能还有更多优化的空间。

【讨论】:

    【解决方案2】:

    我不清楚您的问题是什么,以及它会进入outt。你可能想用list()初始化它。

    至于随机选择入侵者你可以试试:

    init.x[sample(which(init.x<tol),1)] <- 1000*tol
    

    这避免了if 声明和预先计算的随机试验的需要(如果选择社区物种,这可能无法产生入侵)。

    【讨论】:

      【解决方案3】:
      time<- list(start=0,end=1000,steps=1000)
      # Initial conditions (set all species to zero in the beginning)
      init.x <- runif(n)*0
      # generate random order in which species are introduced
      order <- sample(1:n)
      outt <- init.x
      for (i in 1:n){
          # Introduce 1 new species (according to vector "order") with freq 1000*tol
          init.x[order[i]] <- 1000*tol
          # integrate lvm model
          out <-n.integrate(time=time,init.x=init.x,model=lvm)
          # save out and attach it to outt
              outt <- rbind(outt,out)
          # generate new time window to continue integration
              time <- list(start=time$end, end = time$end+time$end-time$start,
                           steps=1000)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-05
        • 2011-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多