【问题标题】:Trying to use the collin function in the R package FME to identify parameters and then fit them using modFit尝试使用 R 包 FME 中的 collin 函数来识别参数,然后使用 modFit 拟合它们
【发布时间】:2015-12-05 04:03:28
【问题描述】:

所以我有一个 ode 系统和一些数据,我正在使用 R 包 deSolve 和 FME 来使 ode 系统的参数适合数据。当我将完整的参数集拟合到数据时,我得到了一个奇异矩阵结果。因此,我返回并使用所有 FME 包文档中建议的 20 的共线性指数截止值来查看参数的共线性,然后我选择了一些带有要拟合的参数子集的模型。然后当我运行 modFit 我得到这个错误:

近似错误(xMod,yMod,xout = xDat): 至少需要两个非 NA 值进行插值

谁能启发我解决这个问题。其他一切工作正常。所以这不是编码问题。

这是一个最小工作示例(在 modFit 中删除 r=2 会产生错误,我可以在最小工作示例中修复该错误,但不能在我的实际问题中修复,因此我怀疑最小工作示例在这里有帮助):

`## =======================================================================
## Now suppose we do not know K and r and they are to be fitted...
## The "observations" are the analytical solution
## =======================================================================

# You need these packages
library('deSolve')
library('FME')

## logistic growth model
TT <- seq(1, 100, 2.5)
N0 <- 0.1
r <- 0.5
K <- 100

## analytical solution

Ana <- cbind(time = TT, N = K/(1 + (K/N0 - 1) * exp(-r*TT)))

time <- 0:100
parms <- c(r = r, K = K)
x <- c(N = N0)

logist <- function(t, x, parms) {
  with(as.list(parms), {
    dx <- r * x[1] * (1 - x[1]/K)
    list(dx)
  })
}

## Run the model with initial guess: K = 10, r = 2

parms["K"] <- 10
parms["r"] <- 2
init <- ode(x, time, logist, parms)

## FITTING algorithm uses modFit
## First define the objective function (model cost) to be minimised
## more general: using modFit

Cost <- function(P) {
  parms["K"] <- P[1]
  parms["r"] <- P[2]
  out <- ode(x, time, logist, parms)
  return(modCost(out, Ana))
} 
(Fit<-modFit(p = c(K = 10,r=2), f = Cost))
summary(Fit)` 

【问题讨论】:

标签: r parameters


【解决方案1】:

我认为问题出在您的成本函数中。如果您不同时提供 K 和 r,则成本函数会将 r 的起始值覆盖为 NA。你可以测试一下:

Cost <- function(P) {
  parms["K"] <- P[1]
  parms["r"] <- P[2]
  print(parms)
  #out <- ode(x, time, logist, parms)
  #return(modCost(out, Ana))
} 
Cost(c(K=10, r = 2))
Cost(c(K=10))

此功能有效:

Cost <- function(P) {
  parms[names(P)] <- P
  out <- ode(x, time, logist, parms)
  return(modCost(out, Ana))
} 

小插图 FMEDyna 非常有用:https://cran.r-project.org/web/packages/FME/vignettes/FMEdyna.pdf 请参阅第 14 页,了解如何指定目标(成本)函数。

【讨论】:

  • 谢谢 有一段时间没有看这里 我已经解决了这个问题并忘记报告是的,它或多或少就像你说的那样,求解器的选择有很大的不同但我确实让它工作了。谢谢。
猜你喜欢
  • 2016-08-21
  • 2018-11-16
  • 2018-11-26
  • 2018-02-03
  • 2019-01-16
  • 2015-11-09
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多