简单的循环编码问题

发布于 2024-11-06 08:38:53 字数 1278 浏览 3 评论 0原文

我是 R 的新用户,我尝试编写一个脚本来模拟物种入侵和群落稳定性。我几乎已经完成了,并且循环中只有一个小问题。

我有一个包含 40 个物种的池(1,2,...),我通过连续的入侵创建了一个群落。群落中的物种离开入侵者池,除非它们灭绝(我设置了密度阈值)。

我想要大量的入侵(> 4000),所以我创建了一个包含 1 到 40 之间的 4000 个数字的向量(随机顺序),但我有一个问题,因为我的物种密度矩阵(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)
}       

我知道这可能非常简单,但我找不到一种方法来编写我的循环,使其具有比物种数量(矩阵中的原始数量)更多的入侵。

多谢,

I am a new user of R and I have tried to write a script for similuting species invasion and community stability. I have almost finished it and I have only one tiny problem in a loop.

I have a pool of 40 species (1,2,...) and I create a community by successive invasions. Species in the community leave the invaders pool unless they go extinct(i put a density threshold value).

I want a lot of invasions (>4000) so I created a vector with 4000 number between 1 and 40 (random.order) but I have a problem because my matrix with the species density (init.x) has not the same number of elements as my vector.

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)
}       

I know this is probably very simple but I can't find out a way to write my loop to have more invasions than the number of species (number of raws in my matrix).

Thanks a lot,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神也荒唐 2024-11-13 08:38:53

您可能想更改

# 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)

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

You probably want to change

# 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))

Into

# 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)

...to solve your main problem (check ?sample). I have not checked the rest of you code, but there may be room for more optimization.

从﹋此江山别 2024-11-13 08:38:53

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

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

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

这避免了 if 语句和预先计算的随机试验的需要(如果选择了群落物种,则可能无法产生入侵)。

I'm not clear on what your problem is, and what it going into outt. You may want to initalise it with list().

As for choosing a random invader you could try:

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

This avoids the if statement and the need for the pre-computed random trials (which may fail to produce an invasion if a community species is selected).

画▽骨i 2024-11-13 08:38:53
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)
}
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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文