最近、パッケージを使わずに R を書くことを学んでいます。そして、私が行っている研究に似た例を1つ見つけました。ただし、値を挿入する方法がわかりません。その方法を教えてもらえますか?
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 の値は時間とともに変化するはずですが、最初に割り当てたのは正しいですか?