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,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 whether want graph of number of  susceptibles  a=1, number of infectives a=2 or 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 , recovereds respect 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 , recovereds respect 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")} 

i want let simulations=1000, nsteps=100, s=25,i=1,r=0,alpha=0.8,beta=0.6. assigned @ first place. , s, , r values should varying time, correct if assigned first?


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -