[R] Problems with the deSolve package

Alexandre Suire alexandresuire at hotmail.fr
Fri Feb 19 09:42:55 CET 2016


Hello R-users,

I'm trying to build a SIR-like model under R, 
using the "deSolve" package. I'm trying to do simulations of its dynamic
 over time, with three differential equations. I'm also looking to 
calculate the equilibrium state. 

So far, my code looks like this 

library(deSolve)
#This is my system, with three differential equations
system<-function(times, init, parameters){
with(as.list(c(init, parameters)),{
di1_dt=(alpha1*(N-i1-i2-i12)*(i1+i12))+(beta2*i12+gamma1*(N-i1-i2-i12))-(beta1*i1)-(delta*alpha2*i1*(i2+i12))
di2_dt=(alpha2*(N-i1-i2-i12)*(i2+i12))+(beta1*i12+gamma2*(N-i1-i2-i12))-(beta2*i2)-(delta*alpha1*i2*(i1+i12))
di12_dt=(delta*alpha2*i1*(i12+i2))+(delta*alpha1*i2*(i12*i1))+(delta*gamma1*i1)+(delta*gamma2*i2)-((beta1+beta2)*i12)
return(list(c(di1_dt,di2_dt,di12_dt)))
})
}

# Initials values and parameters
init<-c(i1=10, i2=10, i12=0) 
parameters<-c(alpha1=0.7, alpha2=0.5, beta1=0.5, beta2=0.3, gamma1=0.5, gamma2=0.5, delta=0.5, N=100) 
times<-seq(0,200, by=1)
simul <- as.data.frame(ode(y = init, times = times, func = system, parms = parameters, method="ode45"))
simul$time <- NULL
head(simul,200) 

#Plotting the results
matplot(times,
 simul, type = "l", xlab = "Time", ylab = "i1 i2 i12", main = 
"Simulation", lwd = 2, lty = 2, bty = "l", col=c("darkblue", 
"darkred","mediumseagreen"))
legend("bottomright", c("i1", "i2","i12"), lty=2,lwd=2, col = c("darkblue", "darkred", "mediumseagreen"))

At
 first, I just tried studying with only the first two equations, and it 
seems to work completely fine, but when I wanted to add the 3rd 
equation, I sometimes get this message, even when I juggle the 
parameters, when i launch the line:
#simul <- as.data.frame(ode(y = init, times = times, func = system, parms = parameters))
Warning messages:
1: In lsode(y, times, func, parms, mf = 10, ...) :
  an excessive amount of work (> maxsteps ) was done, but integration was not successful - increase maxsteps
2: In lsode(y, times, func, parms, mf = 10, ...) :
  Returning early. Results are accurate, as far as they go

Have I overlooked something ? I tried to use methods="ode45" and methods="adams", without any sucess.
Thank you for your time
Alex
	   		 	   		  
	[[alternative HTML version deleted]]



More information about the R-help mailing list