1

我正在尝试使用 deSolve 拟合一个相当基本的 ODE 模型,包括在模型中随时间变化的参数(感染力;FOI)。虽然在没有此参数的情况下运行模型可以正常工作,但包含时间相关参数会产生错误(见下文)。

我对 R 和数学建模相对较新,并且已经尝试解决这个问题一段时间了。

我已经将 FOI 参数创建为值矩阵,然后使用 approxfun 函数进行插值(我已经看到这适用于强制函数,例如https://rdrr.io/rforge/deSolve/man/forcings.html)。

没有这个与时间相关的参数的模型运行没有任何错误,但尝试包含它会给出错误:

Error in checkFunc(Func2, times, y, rho) : 
  The number of derivatives returned by func() (200) must equal the 
length of the initial conditions vector (2)

我不知道如何解决这个错误,因为我只有 2 个初始条件,而且似乎包含这个与时间相关的 FOI 参数会产生更多的导数。

我知道其他人也问过类似的问题,但我没有发现这个关于强制函数的问题。

非常感谢您的任何建议。

# Forcing function data

foi <- matrix(ncol=2,byrow=TRUE,data=c(
  0, 0.003, 2, 0.03, 3, 0.08, 4,0.1,  5, 0.12, 6, 0.15, 
  8, 0.16, 10, 0.14,12, 0.12,14,0.08,15, 0.06,16, 0.03,
  17, 0.01,18,0.003,19,0.003,20,0.003,30,0.003,40,0.003,
  50,0.003,60,0.003,65,0.01, 70,0.08,  72,0.095,74,0.10, 
  76,0.1, 78,0.08,  80,0.06))

age <- seq(0, 80, by = 1)

input <- approxfun(x = foi[,1], y = foi[,2], method = "constant", rule = 2)

# Function
ab <- function(time, state, pars) {
  with(as.list(c(state, pars)), {

import<-c(input(t))

diggP<- (import *iggN) - iggR*iggP   
diggN<- (-import*iggN) + iggR*iggP

return(list(c(diggP, diggN))) 
  })
}


# Initial values
yini  <- c(iggP=0, iggN=1) 

# Parameters
pars  <- c(iggR = 0, import)

# ODE solver
results<- ode(y=yini, times=age, func=foi_model, pars)

我希望制作一个模型,其中在每个时间点(或在这种情况下是年龄),FOI 根据我在 FOI 矩阵中输入的值而变化。因此,我想看看随年龄变化的 FOI 如何影响微分方程的输出。

4

1 回答 1

2

您的主要问题是您将参数传递tinput,但该变量在您的代码中不存在。时间作为参数传递给您的模型,称为time. (此外,您的模型被称为abnot foi_model,如对 的调用中所述ode,加上pars不需要import并且应该传递给ode。)

# Load library
library(deSolve)

# Create FOI matrix
foi <- matrix(ncol=2,byrow=TRUE,data=c(
  0, 0.003, 2, 0.03, 3, 0.08, 4,0.1,  5, 0.12, 6, 0.15, 
  8, 0.16, 10, 0.14,12, 0.12,14,0.08,15, 0.06,16, 0.03,
  17, 0.01,18,0.003,19,0.003,20,0.003,30,0.003,40,0.003,
  50,0.003,60,0.003,65,0.01, 70,0.08,  72,0.095,74,0.10, 
  76,0.1, 78,0.08,  80,0.06))

# Times for model solution
age <- seq(0, 80, by = 1)

# Linear interpolation function from FOI data
input <- approxfun(x = foi[,1], y = foi[,2], method = "constant", rule = 2)

# Model to be integrated
ab <- function(time, state, parms) {
  with(as.list(c(state, parms)), {

    ##### IMPORTANT #####
    import<-input(time) #<- 'time' was previously 't'
    #####################

    # Derivatives
    diggP<- (import *iggN) - iggR*iggP   
    diggN<- (-import*iggN) + iggR*iggP

    # Return results
    return(list(c(diggP, diggN))) 
  })
}

# Initial values
yini  <- c(iggP=0, iggN=1) 

# Parameters
pars  <- c(iggR = 0)

# Solve model
results<- ode(y=yini, times=age, func=ab, parms = pars)

# Plot results
plot(results)

reprex 包(v0.2.1)于 2019 年 3 月 27 日创建

于 2019-03-27T16:33:51.220 回答