Question

I am trying to analyze how varying starting conditions and variable values in a set of differential equations (that describe the progression of a disease through a population) influences the dynamics of the system (as seen via graph). I have written the code and it works perfectly well, but forces me to change a value and then re-run the whole code.

I am therefore trying to put this code inside of manipulate() so I can manipulate the variables and immediately see the effect on the produced graph:

library(deSolve)
library(manipulate)

xyz <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    dt <- 1
    dv <- v0*v1*cos(2*pi*t/33)
    dX <- (v*N-B*X*Y-(mu+N/K)*X)
    dY <- (B*X*Y-(mu+m+g+N/K)*Y)
    dZ <- (g*Y-(mu+N/K)*Z)
    dN <- (v-mu-N/K)*N-m*Y

    return(list(c(dt,dv,dX, dY, dZ, dN)))
  })
}

times <- seq(0,365*3,by = 1)
init <- c(t=0,v=0.02,X=995,Y=5,Z=0,N=1000)
parameters <- c(B=0.14,mu=.01,m=.075,g=.025,K=10000,v0=.02,v1=.5)


manipulate(
  out <- as.data.frame(ode(y = init, times = seq(0,365*3,by=1), func = xyz, parms = parameters)),
  matplot(times,out[4:6],type="l",xlab="Time",ylab="Susceptibles and Recovereds",main="SIR Model",lwd=1,lty=1,bty="l",col=2:4),
  B=slider(0,1,initial=0.14,step=0.01)
)

I keep getting error messages regardless if I have all or part of the code inside manipulate(), define variables outside or inside of it, or anything else. Any help would be greatly appreciated!

Was it helpful?

Solution

When I first ran your code, the error I encountered was:

Error in manipulate(out <- as.data.frame(ode(y = init, times = seq(0,  : 
all controls passed to manipulate must be named

This error occurred because the second argument to manipulate was the matplot() command rather than a named control argument (such as a slider). So I placed the first two lines within curly braces to make them a single expression:

manipulate( {
    out <- as.data.frame(ode(y = init, times = seq(0,365*3,by=1), func = xyz, parms = parameters))
    matplot(times,out[4:6],type="l",xlab="Time",ylab="Susceptibles and Recovereds",main="SIR Model",lwd=1,lty=1,bty="l",col=2:4)
  }, B=slider(0,1,initial=0.14,step=0.01)
)

This eliminates the error, but moving the slider doesn't do anything to the plot. Why? Because the slider named B doesn't refer to anything within the expression passed to manipulate(). I solved that by moving the parameters <- ... line into the manipulate expression and then changing that line so that there was a variable B (not just a name in the list); in other words, we need B=B instead of B=0.14. Now the plot changes when you move the slider, which I believe is what you wanted:

manipulate( {
    parameters <- c(B=B,mu=.01,m=.075,g=.025,K=10000,v0=.02,v1=.5)
    out <- as.data.frame(ode(y = init, times = seq(0,365*3,by=1), func = xyz, parms = parameters))
    matplot(times,out[4:6],type="l",xlab="Time",ylab="Susceptibles and Recovereds",main="SIR Model",lwd=1,lty=1,bty="l",col=2:4)
  }, B=slider(0,1,initial=0.14,step=0.01)
)

OTHER TIPS

times <- seq(0,365*3,by = 1)
init <- c(t=0,v=0.02,X=995,Y=5,Z=0,N=1000)

plot.ode <- function(B.param) {
  parameters <- c(B=B.param,mu=.01,m=.075,g=.025,K=10000,v0=.02,v1=.5)
  out <- as.data.frame(ode(y = init, times = seq(0,365*3,by=1), func = xyz, parms = parameters))
  matplot(times,out[4:6],type="l",xlab="Time",ylab="Susceptibles and Recovereds",main="SIR Model",lwd=1,lty=1,bty="l",col=2:4, ylim=c(0,200))
}
manipulate(plot.ode(B), B=slider(0,1,initial=0.14,step=0.01))

Seems a little odd that only the red curve is influenced by changing B.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top