simulation - R code for simulating an SIR model -
recently learning write r without using packages. , found 1 example similar research doing. however, not know how insert values. tell me how so? 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