我正在使用deSolve软件包为R编写SIR模型。
该模型描述了社区内感染的传播,然后允许引入外部事件 - 这代表了对整个社区的大规模治疗。
这些被建模为在特定时间发生的事件。
对于传输模型本身,我已经能够对其进行编码,以便模型使用基线种群大小和参数(plist和initlist)的一系列值运行大量模拟。
我希望能够对外部事件(社区治疗总数)参数做同样的事情。
目前外部事件对易感人群的影响是: S< -S + 0.95 * L + 0.95 * I1 + 0.95 * I2
但我希望能够指定一系列值,例如0.85-0.99,并让模型在各种可能性范围内运行多个模拟。
我尝试将这些作为附加参数包含在plist中,例如
S <- S + TCT*L + TCT*I1 + TCT*I2
然后将其包含在诸如
之类的plist中plist&lt; - cbind(TCT = runif(100,min = 0.88,max = 0.99)........等)
但我得到的错误是说参数,例如TCT不存在
以下完整代码和欢迎提出建议。
library(deSolve)
#generate an empty list for the data to be put into
simulationlist <- list()
#define the transmission model
SI1I2L <- function(t, y, parms) {
with(as.list(c(parms,y)),{
dS <- - Beta*S*I1 - Beta*S*I2 + treat*I1 + treat*I2 +latenttreat*L
dI1 <- + Beta*S*I1 + Beta*S*I2 - treat*I1 -second*I1 - latent*I1
dI2 <- + second*I1 - latent*I2 - treat*I2 + relapse*L
dL <- +latent*I1 + latent*I2 - relapse*L - latenttreat*L
der <- c(dS, dI1,dI2, dL)
list(der)
})
}
#define Total Community Treatment Parameters
eventtct <- function(t, y, parms){
with (as.list(y),{
S <- S + 0.95*L + 0.95*I1 + 0.95*I2
I1 <- I1*0.05
I2 <- I2*0.05
L <- L*0.05
return(c(S,I1,I2,L))
})
}
#Set the time frame for the model
dt <- seq(0,100,1)
#Define the spectrum of Parameters for transmission with Min/Max values and number of variations
plist <- cbind(Beta = runif(100,min = 0.00000167, max = 0.0000043),second = runif(100,min= 0.0278,max = 0.0556),latent = runif(100,min=0.004, max=0.009), treat = runif(100, min=0.01, max =0.03),latenttreat = runif(100,min=0.004,max=0.009),relapse = runif(100,min=0.012,max=0.028))
for (i in 1:nrow(plist))
#Define the spectrum of inital values for population size
initlist <- cbind(S = runif(100,min = 110681, max = 118636),I1 = runif(100,min=798, max=2926),I2 = runif(100,min=266,max=1463),L=runif(100,min=13300, max=27930))
for (i in 1:nrow(initlist))
#run multiple simulations
simulationlist[[i]] <- as.data.frame(lsoda(initlist[i,], dt, SI1I2L, parms=plist[i,],events = list(func = eventtct, time = c(2,12) )))
答案 0 :(得分:1)
请测试我的解决方案。 认为它有效
#### Clear currrent lists from memory
rm(list=ls())
library(deSolve)
#generate an empty list for the data to be put into
simulationlist <- list()
#define the transmission model
SI1I2L <- function(t, y, parameters) {
with(as.list(c(parameters,y)),{
dS <- - Beta*S*I1 - Beta*S*I2 + treat*I1 + treat*I2 +latenttreat*L
dI1 <- + Beta*S*I1 + Beta*S*I2 - treat*I1 -second*I1 - latent*I1
dI2 <- + second*I1 - latent*I2 - treat*I2 + relapse*L
dL <- +latent*I1 + latent*I2 - relapse*L - latenttreat*L
der <- c(dS, dI1,dI2, dL)
list(der)
})
}
n=10;
#define Total Community Treatment Parameters
TCTlist = runif(n,min = 0.88, max = 0.99);
#eventtct <- function(t, y, parms){
eventtct <- function(t,y,parameters){
with (as.list(y,parameters),{
# eval(TCT=parameters[7])
S <- S + TCT*L + TCT*I1 + TCT*I2
I1 <- I1*(1-TCT)
I2 <- I2*(1-TCT)
L <- L*(1-TCT)
return(c(S,I1,I2,L))
})
}
#Set the time frame for the model
dt <- seq(0,100,1)
#Define the spectrum of Parameters for transmission with Min/Max values and number of variations
plist <- cbind(Beta = runif(n,min = 0.00000167, max = 0.0000043),second = runif(n,min= 0.0278,max = 0.0556),latent = runif(n,min=0.004, max=0.009), treat = runif(n, min=0.01, max =0.03),latenttreat = runif(n,min=0.004,max=0.009),relapse = runif(n,min=0.012,max=0.028))
for (i in 1:n)
#Define the spectrum of inital values for population size
initlist <- cbind(S = runif(n,min = 110681, max = 118636),I1 = runif(n,min=798, max=2926),I2 = runif(n,min=266,max=1463),L=runif(n,min=13300, max=27930))
for (i in 1:n){
#run multiple simulations
parameters = c(plist[i,],TCT=TCTlist[i]);
eventsfunction = list(eventtct, time = c(2,12) );
simulationlist[[i]] <- as.data.frame(ode(initlist[i,], time=dt, func=SI1I2L, parms=parameters,events =eventsfunction ))
}