I would like to incoparate a rate that changes with times in a metapopulation model using code by B. Raynor. The code was originally published at https://rpubs.com/bhraynor/MetapopulationModel
In the code below, I want mu to take values in the vector mu = seq(from = 0, to = 1, length = 100)
Below is the code that runs well, but wont run if I uncomment the mu = seq(from = 0, to = 1, length = 100) vector and comment the the vector mu = c(1/1000, 1/1000, 1/1000).
I get error saying: Error in eval(substitute(expr), data, enclos = parent.frame()) : dims [product 3] do not match the length of object [100]
#load libraries
library(dplyr)
library(deSolve)
MODEL <- function(time, state, parameters) {
with(as.list(c(state, parameters)), {
#Define initial conditions
S = matrix(state[1:3], ncol=1)
I = matrix(state[(4:6)], ncol=1)
R = matrix(state[(7:9)], ncol=1)
#Define params
gamma <- matrix(parameters[paste0("gamma", 1:3)], ncol=1)
alpha <- matrix(parameters[paste0("alpha", 1:3)], ncol=1)
mu <- matrix(parameters[paste0("mu", 1:3)], ncol=1)
#mu = seq(from = 0.001, to = 0.0015, length = 100)
dS <- -mu*S + alpha*R
dI <- mu*S - gamma*I
dR <- gamma*I - alpha*R
output <- c(dS, dI, dR)
list(output)
})
}
init <- c(S1 = 100, S2 = 100, S3 = 100, I1 = 10, I2 = 10, I3 = 0, R1 = 0, R2 = 0, R3 = 0 )
#parameters
parms <- c(gamma1 = 0.2000000, gamma2 = 0.2500000, gamma3 = 0.3333333, mu1 = 0.0010000,
mu2 = 0.0010000, mu3 = 0.0010000,alpha1 = 0.200000, alpha2 = 0.3000000, alpha3 = 0.4000000)
Time = 100
dt = 1 #Step size dt
times <- seq(0, Time, by = dt)
#Run simulation
out <- ode(y=init, times=times, func=MODEL, parms=parms)