我试图创建一个流行病学SEIR模型,并模拟模型隔间随着时间的推移.更具体地说,下面的代码定义了一个名为Metapolitude_SEIR的函数.这个函数代表一个房室模型,称为元群体SEIR模型,通常用于流行病学中模拟传染病在多个相互关联的人群中的传播.参见代码:
library(deSolve)
library(ggplot2)
# Metapopulation SEIR model function
Metapopulation_SEIR <- function(time, current_state, params, connectivity_matrix){
with(as.list(c(current_state, params)),{
# Calculate total population size in each subpopulation
N <- apply(current_state, 1, sum)
# Initialize rate of change vectors
dS <- rep(0, nrow(current_state))
dE <- rep(0, nrow(current_state))
dI <- rep(0, nrow(current_state))
dR <- rep(0, nrow(current_state))
dM <- rep(0, nrow(current_state))
# Loop through each subpopulation
for (i in 1:nrow(current_state)) {
# Calculate total number of individuals in subpopulation i
Ni <- N[i]
# Calculate rates of change for each compartment in subpopulation i
dSi <- -beta * S[i] * I[i] / Ni
dEi <- (beta * S[i] * I[i] / Ni) - sigma * E[i]
dIi <- sigma * E[i] - gamma * I[i] - mu * I[i]
dRi <- gamma * I[i]
dMi <- mu * I[i]
# Update rate of change vectors
dS[i] <- dSi
dE[i] <- dEi
dI[i] <- dIi
dR[i] <- dRi
dM[i] <- dMi
# Calculate movement of individuals between subpopulations
for (j in 1:nrow(current_state)) {
dS[i] <- dS[i] + connectivity_matrix[i, j] * (beta * S[j] * I[j] / Ni)
dE[i] <- dE[i] + connectivity_matrix[i, j] * (sigma * E[j])
dI[i] <- dI[i] + connectivity_matrix[i, j] * (gamma * I[j])
dR[i] <- dR[i] + connectivity_matrix[i, j] * (gamma * R[j])
dM[i] <- dM[i] + connectivity_matrix[i, j] * (mu * I[j])
}
}
# Return a list of rates of change for each compartment
return(list(c(dS, dE, dI, dR, dM)))
})
}
# Example inputs
# Number of subpopulations
num_subpopulations <- 3
# Initial compartment counts for each subpopulation
initial_state <- matrix(c(
S1 = 900, E1 = 10, I1 = 5, R1 = 85,
S2 = 950, E2 = 5, I2 = 2, R2 = 43,
S3 = 850, E3 = 15, I3 = 7, R3 = 100
), ncol = 4, byrow = TRUE)
# Parameters
params <- c(beta = 0.5, sigma = 0.25, gamma = 0.2, mu = 0.001)
# Connectivity matrix (specifies the movement of individuals between subpopulations)
# Example: Fully connected network where individuals can move between all subpopulations
connectivity_matrix <- matrix(0.1, nrow = num_subpopulations, ncol = num_subpopulations)
diag(connectivity_matrix) <- 1 # Individuals stay within their own subpopulation
# Time points
times <- seq(0, 365, by = 1)
# Solve the model
model <- ode(initial_state, times, Metapopulation_SEIR, params, connectivity_matrix)
在最后一行给出以下错误:
match.arg(方法)中的错误:'arg'必须为NULL或字符向量
我试着把这个论点命名如下:
model <- ode(initial_state, times, Metapopulation_SEIR, parms=params, connectivity_matrix)
但还是犯了同样的错误.