有没有一种方法可以使用R中的optim优化ODE的初始值?

时间:2019-11-04 11:38:22

标签: r optimization

我想使用numcaseswk0优化R中的值(optim)。但是,该值不是ODE的参数-只是初始值。

下面显示的代码尝试执行此操作,但是优化过程始终失败,并且仅产生用户提供的上限。我怀疑这可能是由于numcaseswk0不是ODE的参数之一。如果有人可以指出如何解决此问题,我将感到非常高兴。谢谢。


library(deSolve)

### ODE FUNCTION

HAVODE <- function(t, states, parameters){
  with(as.list(c(states,parameters)),
       {
         N    <- S + L + Z + I + R
         dS <- -beta * S * (I/N) 
         dL <-  beta * S * (I/N) - (1/durL)*L
         dI <-  (1/durL)*L +(1/durRel)*R - (1/durI)*I             
         dZ <- (1-propRelapse)*(1/durI)*I
         dR <- propRelapse*(1/durI)*I - (1/durRel)*R 
         return(list(c(dS, dL, dI, dZ, dR)))
       })
}

### COST FUNCTION

calib_function <- function(x, parameters,observed.){

  ## Variable to be optimized

  numcaseswk0 <- x

  initpop = parameters[1]
  durL = parameters[2]
  durI = parameters[3]
  fracImmune = parameters[4]
  durRel = parameters[5]
  propRelapse = parameters[6]
  probdetec = parameters[7]
  beta  = parameters[8]

  ## Starting values for states 
  S. = (1-fracImmune)*initpop
  L. = numcaseswk0              # ***  I want this value to be optimized
  I. = 0
  Z. = fracImmune*initpop
  R. = 0  
  states = c(S=S., L= L. , I=I.,  Z=Z.,   R=R.)

  ## Parameters to be fed into ODE solver
  parameters1 = c(durL = durL, durI = durI,durRel = durRel, propRelapse = propRelapse,  beta = beta  )                 
  tspan = seq(0, length(observed.)+10); 
  # Run the ODE solver
  result <- data.frame(ode(y = states, times = tspan,  func = HAVODE,  parms = parameters1))

  # Calculating model response (number of detected incident cases)

  IncDetec <- probdetec *((1/durL)*result[, 3] + (1/durRel)*result[, 6])

  model_response <- IncDetec[-1][1:length(observed.)]     # exclude initial week


  # Calculate negative log likelihood of model responses

  NLLK <- -sum(dpois(x = floor(model_response), lambda = observed., log = TRUE ))

  if (NLLK == Inf){

    NLLK = 999999    # if NLLK is infinity, replace by a large number
  }
  return(NLLK)  
}

## vector of starting values
x0 <- 2
## set lower and upper bounds for these variables
upper <- 10
lower <- 1

## Call the cost function with optim

calib_parameters <- c(135722, 9.2088, 2.6047, 0.47, 3.930, 7.21, 0.094, 0.517)

optimization_results <- optim(par=x0, lower = lower,  upper = upper, method = 'Brent', fn = calib_function, parameters = calib_parameters,  observed. =  abs(rnorm(50, mean=6, sd=3)))

运行上面的代码将给出:

> optimization_results

$par
[1] 1.000001

$value
[1] 113463174144

$counts
function gradient 
      NA       NA 

$convergence
[1] 0

$message
NULL

optim产生的估计值是所提供的下限(lower=1)的值。您可能还会注意到,没有功能评估。为什么优化不适用于numcaseswk0

2 个答案:

答案 0 :(得分:0)

正如Ben已经指出的那样,该代码示例不可复制。如果我从http://desolve.r-forge.r-project.org/user2014/examples/FME/fit_twocomp.svg

摘录的一个教程示例中发布代码段,也许对您有帮助

该示例使用软件包FME,该软件包包装了optim(和其他优化程序)并提供了一些其他支持。

### ============================================================================
### code snippet from the useR!2014 (Los Angeles) tutorial
###
### Copyright tpetzoldt, license: GPL >= 2.0
### more see: 
###   http://desolve.r-forge.r-project.org/user2014/examples/FME/fit_twocomp.svg
### ============================================================================
library(deSolve)
library(FME)

## A two compartment pharmacokinetic model
twocomp <- function (time, y, parms, ...) {
  with(as.list(c(parms, y)), {
    dCL <- kFL * CF - kLF * CL - ke * CL  # concentration in liver
    dCF <-    kLF * CL  - kFL * CF        # concentration in fat
    list(c(dCL, dCF))
  })
}
parms <- c(ke = 0.2,    kFL = 0.1,  kLF = 0.05)
times <- seq(0, 40, length=200)
y0      <-  c(CL = 1, CF = 0)
out <- ode(y0, times, twocomp, parms)

## -----------------------------------------------------------------------------
## data in database format
## -----------------------------------------------------------------------------
dat2 <- data.frame(
  label = rep(c("CL", "CF"), each=8),  # must be the first column
  time = rep(seq(0, 28, 4), 2),
  value = c(1.31,  0.61,  0.49,  0.41,  0.20,  0.12,  0.16,  0.21,
            0.001, 0.041, 0.050, 0.039, 0.031, 0.025, 0.017, 0.012)
)

## -----------------------------------------------------------------------------
## fit parameters and initial values
## -----------------------------------------------------------------------------

parms <- c(CL = 1.0, CF = 0.0, ke = 0.2,    kFL = 0.1,  kLF = 0.05)

cost <- function(p, data, ...) {
  yy <- p[c("CL", "CF")]           # initial values
  pp <-  p[c("ke", "kFL", "kLF")]  # start parameters
  out  <-  ode(yy, times, twocomp, pp)
  modCost(out, data, y="value", ...)
}

## The default Marq optimizer fails here, so we use another, e.g. Port        
fit6  <- modFit(f = cost, p = parms, data=dat2, weight="std",
                lower=rep(0, 5), upper=c(2,2,1,1,1), method="Port")

summary(fit6)        
y0 <- coef(fit6)[c("CL", "CF")]
pp <- coef(fit6)[c("ke", "kFL", "kLF")]
out6 <- ode(y0, times, twocomp, pp)

plot(out, out6, obs=dat2)

答案 1 :(得分:0)

我认为您的模型规格有问题。优化程序可以正常运行(从技术角度而言),并且最佳参数为零。

请查看评论,更改内容:

  • 惩罚值更大
  • 将成本值重新缩放到可行范围
  • 对成本函数的一些手动测试调用
  • 一些有助于调试的cat()函数
  • 还请注意,“。”语法(L.,R。等)不是必需的

以下内容可能仍然不是您想要的,但希望可以帮助使其正常运行。祝你好运!

    library(deSolve)

    ### ODE FUNCTION

    HAVODE <- function(t, states, parameters){
      with(as.list(c(states,parameters)),
           {
             N    <- S + L + Z + I + R
             #cat("N=", N, "\n")
             dS <- -beta * S * (I/N) 
             dL <-  beta * S * (I/N) - (1/durL)*L
             dI <-  (1/durL)*L +(1/durRel)*R - (1/durI)*I             
             dZ <- (1-propRelapse)*(1/durI)*I
             dR <- propRelapse*(1/durI)*I - (1/durRel)*R 
             return(list(c(dS, dL, dI, dZ, dR)))
           })
    }

    ### COST FUNCTION

    calib_function <- function(x, parameters,observed.){

      ## Variable to be optimized
      #cat("x=", x, "\n")

      numcaseswk0 <- x

      initpop = parameters[1]
      durL = parameters[2]
      durI = parameters[3]
      fracImmune = parameters[4]
      durRel = parameters[5]
      propRelapse = parameters[6]
      probdetec = parameters[7]
      beta  = parameters[8]

      ## Starting values for states 
      S. = (1-fracImmune)*initpop
      L. = numcaseswk0              # ***  I want this value to be optimized
      I. = 0
      Z. = fracImmune*initpop
      R. = 0  
      states = c(S=S., L= L. , I=I.,  Z=Z.,   R=R.)
      #cat(states, "\n")

      ## Parameters to be fed into ODE solver
      parameters1 = c(durL = durL, durI = durI,durRel = durRel, propRelapse = propRelapse,  beta = beta  )                 
      tspan = seq(0, length(observed.)+10); 
      # Run the ODE solver
      result <- data.frame(ode(y = states, times = tspan,  func = HAVODE,  parms = parameters1))

      # Calculating model response (number of detected incident cases)

      IncDetec <- probdetec *((1/durL)*result[, 3] + (1/durRel)*result[, 6])

      model_response <- IncDetec[-1][1:length(observed.)]     # exclude initial week


      # Calculate negative log likelihood of model responses

      NLLK <- -sum(dpois(x = floor(model_response), lambda = observed., log = TRUE ))

      ## tpe: set it to a really large value
      if (!is.finite(NLLK)){
        NLLK = 0.1 * .Machine$double.xmax    # if NLLK is infinity, replace by a large number
      }

      ## tpe: re-scale return value to a numerically feasible range
      return(NLLK * 1e-10)  
    }

    ## vector of starting values
    x0 <- 2
    ## set lower and upper bounds for these variables
    upper <- 10
    lower <- 0

    ## Call the cost function with optim

    calib_parameters <- c(135722, 9.2088, 2.6047, 0.47, 3.930, 7.21, 0.094, 0.517)

    ## tpe: reproducible comparison data
    set.seed(42)
    observed <- abs(rnorm(50, mean=6, sd=3))

    ## test manually
    calib_function(1, calib_parameters, observed)
    calib_function(0, calib_parameters, observed)
    calib_function(10, calib_parameters, observed)

    ## tpe: we see that zero *is* the best among these

    ## optimize automatically
    optimization_results <- optim(par=x0, lower = lower,  upper = upper, 
                                  method = 'L-BFGS-B', fn = calib_function, 
                                  parameters = calib_parameters,  
                                  observed. =  observed,
                                  control=list(trace=TRUE))


    optimization_results
    ## tpe: optimized par is again zero, that confirms the manual test