2
試圖將我的思維包圍矢量化,試圖使一些模擬更快我發現這非常基本的流行模擬。該代碼是從書http://www.amazon.com/Introduction-Scientific-Programming-Simulation-Using/dp/1420068725/ref=sr_1_1?ie=UTF8&qid=1338069156&sr=8-1向量化模擬
#program spuRs/resources/scripts/SIRsim.r
SIRsim <- function(a, b, N, T) {
# Simulate an SIR epidemic
# a is infection rate, b is removal rate
# N initial susceptibles, 1 initial infected, simulation length T
# returns a matrix size (T+1)*3 with columns S, I, R respectively
S <- rep(0, T+1)
I <- rep(0, T+1)
R <- rep(0, T+1)
S[1] <- N
I[1] <- 1
R[1] <- 0
for (i in 1:T) {
S[i+1] <- rbinom(1, S[i], (1 - a)^I[i])
R[i+1] <- R[i] + rbinom(1, I[i], b)
I[i+1] <- N + 1 - R[i+1] - S[i+1]
}
return(matrix(c(S, I, R), ncol = 3))
}
模擬的核心是for
循環。我的問題是,由於代碼產生S[i]
和R[i]
值中的S[i+1]
和R[i+1]
值,是否可以使用apply函數對其進行矢量化?
非常感謝
'apply'函數大多是''for'循環周圍的句法糖,你可能不會以這種方式獲得很多速度。你可以嘗試編譯器包或Rcpp。 – baptiste