【发布时间】:2021-08-21 01:29:47
【问题描述】:
我已经使用函数内的循环构建了一个离散时间 SIR 模型(我在下面添加了我的代码)。
目前迭代的结果以列表形式出现,似乎首先显示所有 S 值,然后是 I 值,然后是 R 值,这是我从值的性质推断出来的。
我需要将输出作为数据框的列名:从左到右的“迭代”、“S”、“I”和“R”以及下面的相应值,这样当读取一行时它会告诉您在该迭代中 S、I 和 R 的迭代和值。
我不知道如何构建一个以这种方式返回输出值的数据框,我几周前才开始学习 R,所以还不精通,因此非常感谢任何帮助。
提前谢谢你。
#INITIAL CONDITIONS
S=999
I=1
R=0
#PARAMETERS
beta = 0.003 # infectious contact rate (/person/day)
gamma = 0.2 # recovery rate (/day)
#SIR MODEL WITH POISSON SAMPLING
discrete_SIR_model <- function(){
for(i in 1:30){ #the number of iterations of loop indicates the
#duration of the model in days
# i.e. 'i in 1:30' constitutes 30 days
deltaI<- rpois(1,beta * I * S) #rate at which individuals in the
#population are becoming infected
deltaR<-rpois(1,gamma * I)#rate at which infected individuals are
#recovering
S[i+1]<-S[i] -deltaI
I[i+1] <-I[i] + deltaI -deltaR
R[i+1]<-R[i]+deltaR
}
}
output <- list(c(S, I, R))
output
【问题讨论】: