I am trying to model a series of difference equations in R using the deSolve package and method = "iteration", but the output is just returning the initial values with no changes, and I do not understand why. This is my code:
library(deSolve)
travelmodel <- function(t,y,parms) {
with(as.list(c(y,parms)), {
dImVillage <- Im*Village*(1-a) + ImField
dImField <- Im*Village*a + Ih*(1-Im)*Village*a
ImFlying <- ImField + Im*Village*a + Ih*(1-Im)*Village*a
ImStaying <- 1- ImFlying
dSmVillage <- (1-Im)*Village*(1-a) + SmField
dSmField <- (1-Ih)*(1-Im)*Village*a
SmFlying <- SmField + (1-Ih)*(1-Im)*Village*a
SmStaying <- 1- ImFlying
Village = ImField + Im*Village*a + Ih*(1-Im)*Village*a + SmField + (1-Ih)*(1-Im)*Village*a
list(c(ImVillage, ImField, ImFlying, ImStaying, SmVillage, SmField, SmFlying, SmStaying, Village))
})
}
parmstrav <- c(Ih = 0.5,
Im = 0.5,
a = 0.15)
times <- seq(0, 365, by = 1) statetrav <- c(ImVillage = 0.9,
ImField = 0.1,
ImFlying = 0,
ImStaying = 0,
SmVillage = 0.9,
SmField = 0.1,
SmFlying = 0,
SmStaying = 0,
Village = 0)
difference_output <- ode(y = statetrav, times = times, func = travelmodel, parms = parmstrav, method = "iteration")
I would really appreciate any help with this, thanks a lot.
Here a small example how to formulate a difference equation model by specifying either the differences or the new states in the model function. In the first case,
method="euler"is used whilemethod = "iteration"is needed in the second case. Both formulations below return identical results:Two additional notes:
method = "lsoda".