简体   繁体   English

deSolve for R-参数化事件

[英]deSolve for R - Parameterise Events

I'm writing an SIR Model using the deSolve package for R. 我正在使用用于R的deSolve软件包编写SIR模型。

The model describes transmission of an infection within a community and then allows the introduction of external events - which represent mass treatment of the whole community. 该模型描述了社区内部感染的传播,然后允许引入外部事件-这代表了整个社区的大规模治疗。

These are modelled as events that occur at specific times. 这些被建模为在特定时间发生的事件。

For the transmission model itself I have been able to code it so that the model runs a large number of simulations using a range of values for the baseline population sizes and parameters (plist and initlist). 对于传输模型本身,我已经能够对其进行编码,以便该模型使用一系列用于基线总体大小和参数(plist和initlist)的值来运行大量仿真。

I would like to be able to do the same for the external event (Total Community Treatment) parameters. 我希望能够对外部事件(全面社区治疗)参数执行相同的操作。

At present the impact of the external event on the Susceptible population is: S <- S + 0.95*L + 0.95*I1 + 0.95*I2 目前,外部事件对易感人群的影响为:S <-S + 0.95 * L + 0.95 * I1 + 0.95 * I2

But I would like to be able to specify a range of values eg 0.85-0.99 and have the model run multiple simulations across the full range of possibilities. 但是我希望能够指定一个值范围,例如0.85-0.99,并让模型在所有可能性范围内运行多个模拟。

I tried including these as additional parameters in the plist eg 我尝试将这些作为附加参数包括在plist中,例如

S <- S + TCT*L + TCT*I1 + TCT*I2

and then including it in the plist such as 然后将其包括在plist中,例如

plist <- cbind(TCT = runif(100,min = 0.88, max = 0.99)........etc) plist <-cbind(TCT = runif(100,最小值= 0.88,最大值= 0.99)........等)

But I get errors saying that the parameter eg TCT doesn't exist 但是我收到错误消息,说TCT等参数不存在

Full code below and suggestions welcomed. 完整代码如下,欢迎提出建议。

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) ))) 

Please test my solution. 请测试我的解决方案。 Thinks it works 认为有效

    #### 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 )) 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM