Question
Explain the meaning of each line in detail, and find the wrong error place to make sure it could run. It is an R language
Explain the meaning of each line in detail, and find the wrong error place to make sure it could run. It is an R language code.
############################################################ rm(list=ls()) #this clears the workspace to make sure no leftover variables are floating around. Not strictly needed require(deSolve) #loads ODE solver package - make sure you have the package deSolve installed first #graphics.off(); #close all graphics windows
############################################################ #specify the functions first ############################################################ odeequations=function(t,y,pars) #odeequations=function(t,y,pars) { S=y[1]; In=y[2]; #assigning the y/variable vector to each of the compartments - susceptibles (S), infecteds (I), recovereds (R) beta=pars[1]; #assigning the parameter vector to each of the parameter values - level of infectiousness (beta), duration of infectious period (1/gamma) #the ordinary differential equations dS= -beta*S*In; #susceptibles -BSI dIn=beta*S*In; #infected/infectious BSI return(list(c(dS,dIn))); } #end function specifying the ODEs
############################################################ #setting initial conditions for variables #you can change initial conditions by changing the values assigned to PopSize and I0 #PopSize=1000; #try a range of values for this and see how it affects the outbreak S0=1; #initial number of uninfected hosts I0=100000; #initial number of infected hosts, again try a number of different values Y0=c(S0, I0); #combine initial conditions into a vector
############################################################ #specifying maximum simulation/integration time and time vector for which we want solutions #you can change the maximum time for which the simulation is run by changing the value assigned to tmax tmax=70; #maximum simulation time, units depend on choice of units for your parameters dt=1; #time step for which to get results back 1/.01 = 100 time timevec=seq(0,tmax,dt); #vector of times for which solution is returned (not that internal timestep of the integrator is different)
############################################################ #specify values for the model parameters. #transmission and infection duration parameters - you can change the parameters by changing the values assigned to each
beta=1.5; #level of infectiousness, i.e. rate of transmission of pathogen from infected to susceptible host.
#combining parameters into a parameter vector pars=c(beta);
#this line runs the simulation, i.e. integrates the differential equations describing the infection process #the result is saved in the odeoutput matrix, with the 1st column the time, the 2nd, 3rd, 4th column the variables S, I, R #This odeoutput matrix will be re-created every time you run the code, so any previous results will be overwritten odeoutput=lsoda(Y0,timevec,odeequations,parms=pars,atol=1e-7,rtol=1e-7);
windows(width=10,height=10) plot(odeoutput[,1],odeoutput[,2],type="l",xlab="time (years)",ylab="",col="green",lwd=2,log="",xlim=c(0,tmax),ylim=c(1,max(odeoutput[,2])),main="Outbreak Time Series") lines(odeoutput[,1],odeoutput[,3],type="l",col="red",lwd=2) lines(odeoutput[,1],odeoutput[,2]+odeoutput[,3], type="l",col="blue",lwd=2) legend("right", c("Susceptible","Infected","Total"),col = c("green","red","blue"),lwd=2)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started