【发布时间】:2020-05-26 10:23:38
【问题描述】:
我正在模拟种植园的生长。
我必须在每年(第 0 年到第 6 年的列)中填充 10 棵树(每行一棵)的体积。 我有一个来自“growthmodels”包的函数,它计算每年树的体积,或者创建一个包含一系列年份范围内所有值的向量。 我有一个 df 每年每棵树的年龄(有些树死了,必须重新种植,所以它们的年龄从零开始)。
这是问题的简化版本:
volume <- data.frame(matrix(ncol = 6, nrow = 10))
age <- data.frame("y0" = rep(0, 10))
for (y in 2:6){
d <- sample(1:10, 1)
d0 <- 10-d
age <- cbind(age, c(age[1:d, y-1] + rep(1, d), rep(0, d0)))
names(age)[y] <- paste0("y", y-1)
}
library(growthmodels)
growth <- chapmanRichards(t=1:5, alpha=3, m=.4, k=.4, beta=1)
我找到了以下方法,但是执行起来超级超级慢。想象一下,我必须为更多的物种和 100 个具有多种增长速度的随机案例运行它。你有什么建议吗?
#inputing normal growth
for (y in 2:6){
for (t in 1:nrow(volume)) {
volume[t,y] <- chapmanRichards(t=age[t,y], alpha=3*d, m=.4, k=.4*l, beta=1)
}
}
我需要保持循环 for (y in 2:6),因为每年 chapmanRichards 公式中的因子 d 和 l 都有不同的值。
【问题讨论】:
-
我的第一个冲动是使用
chapmanRichards函数的矢量化版本:Vectorize(chapmanRichards, vectorize.args = list("t"))。