最近我正在学习在不使用包的情况下编写 R。我发现了一个与我正在做的研究相似的例子。但是,我不知道如何插入这些值。谁能告诉我该怎么做?
set.seed(18746)
Simulations<- 1000
Nsteps<- 100
S<- 25
I<- 1
R<- 0
alpha<- 0.8
beta<- 0.6
a<- 2
singlesim <- function(alpha,beta,s,i,r){
a<- (alpha/(s+i+r))*s*i
b<- beta*i
d<- runif(1)
ran <- max(a*d,beta*i*d)
z<-c(s,i,r)
j<- ifelse(c(ran>a&ran<=a+b, ran>a&ran<=a+b,ran>a&ran<=a+b),c (s,i-1,r+1),z)
l<- ifelse(c(ran<=a,ran<=a,ran<=a),c(s-1,i+1,r),z)
x<- ifelse(c(ran<=a,ran<=a,ran<=a),l,j)
q<- ifelse(j==z&l==z,z,x)
g<- ifelse(c(ran==0,ran==0,ran==0),z,q)
g}
onecompsim <- function(Nsteps,S,I,R,alpha,beta){
P<- array(0,c(Nsteps,3))
Z<- array(0,c(Nsteps,3))
Y<- array(0,c(Nsteps,3))
P[1,]<-c(S,I,R)
for(i in 2:Nsteps){
P[i,]<- singlesim(alpha,beta,P[i-1,1],P[i-1,2],P[i-1,3])}
P}
fisim<- function(Simulations,Nsteps,S,I,R,alpha,beta){
Q<-array(0,c(Nsteps,3,Simulations))
for(i in 1:Simulations){
Q[,,i] <- onecompsim(Nsteps,S,I,R,alpha,beta)}
Q}
#a is whether we want the graph to be of the number of susceptibles a=1, the number of infectives a=2 or the number of recovereds a=3
graph<-function(Simulations, Nsteps,S,I,R,alpha,beta,a){
plot(fisim(Simulations,Nsteps,S,I,R,alpha,beta) [,a,1],axes=FALSE,type="l",ylim=c(0,S+I+R))
axis(2,0:(S+I+R))
axis(1)
for(i in 2:Simulations){
lines(fisim(Simulations, Nsteps, S, I, R,alpha,beta)[,a,i])}
}
rm(.Random.seed)
mean<-function(Simualtions,Nsteps,S,I,R,alpha,beta,a){
Q<- 1:Nsteps*0
for(i in 1:Nsteps){
Q[i]<- sum(fisim(Simulations,Nsteps,S,I,R,alpha,beta) [i,a,])/Simulations}
Q}
meangraph<-function(Simulations,Nsteps,S,I,R,alpha,beta,a){
plot(mean (Simulations,Nsteps,S,I,R,alpha,beta,a),type="l",col="red",ylim=c (0,S+I+R),xlab="Time", ylab="Number", main="The number of susceptibles, infectives and recovereds with respect to time")}
allgraph<-function(Simulations,Nsteps,S,I,R,alpha,beta){
plot(mean (Simulations,Nsteps,S,I,R,alpha,beta,a),type="l",col="red",ylim=c (0,S+I+R),xlab="Time", ylab="Number", main="The number of susceptibles, infectives and recovereds with respect to time")
lines(mean(Simulations, Nsteps, S,I,R,alpha,beta,2),col="blue")
lines(mean(Simulations, Nsteps, S,I,R,alpha,beta,3),col="green")}
我想让 Simulations=1000, Nsteps=100, S=25,I=1,R=0,alpha=0.8,beta=0.6。所以我把它放在第一位。而且S、I、R的值应该随时间变化,我先赋值对吗?