我正在繪制具有多個回圈(for和while家族走向滅亡。我想在圖中添加一條線,代表所有模擬家庭的兔子中位數和代表四分位距的帶。
我特別需要幫助來確定回圈結構中我可以存盤每個模擬家庭的最終家庭規模的位置,這是每一代的初始家庭規模bunnies和所有后續家庭規模的總和。bunnies_produced
下面我提供了我擁有的當前數字(左)和我想要的數字(右;中位數和 IQR 的組成值)。
我嘗試過啟動一個變數totalbunnies并在不同的地方添加totalbunnies或totalbunnies[i],但它從來沒有正確計算每個家庭的總和(見注釋掉的行)。
我不需要實際可視化的幫助,我可以添加線條/帶,只需要具體的幫助來存盤每個模擬的最終家庭大小。
# Initiate plot
plot(NA, xlim = c(0, 10), ylim = c(0, 25),
xlab="Duration", ylab = "Number of Bunnies", frame = FALSE)
totalbunnies <- c() # initiate to store total number of bunnies in each sim
# Run simulations
set.seed(0506)
for(i in 1:100) {
bunnies <- 1
t <- rep(0, 1)
duration <- t
while(bunnies > 0) {
bunnies_produced <- rnbinom(bunnies, mu = 0.5, size = 0.25)
t.new <- numeric()
for(j in 1:length(bunnies_produced)) {
t.new <- c(t.new, t[j] rgamma(bunnies_produced[j], shape = 0.25, rate = 0.5))
#totalbunnies <- sum(totalbunnies, bunnies_produced)
}
bunnies <- length(t.new)
t <- t.new
duration <- c(duration, t.new)
# totalbunnies[i] <- sum(bunnies, bunnies_produced)
# totalbunnies <- sum(totalbunnies, bunnies_produced)
}
#totalbunnies[I] <- max(duration)
lines(sort(duration), 1:length(duration), col = "maroon", lwd = 1)
points(max(duration), length(duration), col = "maroon", pch = 16)
}

uj5u.com熱心網友回復:
兔子總數的總和是通過添加totalbunnies[i] <- length(duration)而不是max(duration)在lines()引數之前找到的
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/412367.html
標籤:
