避免 ODE 迭代求解中的循环
我正在将参数模型拟合到一些具有时间相关协变量的生存数据。拟合过程涉及迭代求解一些 ODE - 每个对象每个时间间隔一个 ODE,但当前时间间隔上的 ODE 的初始条件是前一个时间间隔上 ODE 的解的最后一个值。从这个意义上说,ODE 相互依赖。
我的问题归结为:现在,我正在通过循环迭代求解这些 ODE,因为我需要使用前一个解决方案的最后一个值作为下一个解决方案的起点。问题在于,对于大型数据集,这种循环会消耗大量时间。有什么方法可以使用 vapply 或其他矢量化函数来完成同样的事情吗?
我一直在搜索档案,但没有找到任何解决方案来解决依赖于先前值的操作的矢量化问题。
这是一个代码示例,它本身不会产生任何具有统计意义的内容,但说明了我的问题:
require(odeSolve)
param <- c(a=1)
df <- function(t, state, param){
with( as.list(c(state, param)), {dX<-a*X; list(c(dX))} )
}
Data.i <- data.frame( lt=seq(0, 5, length=10)[-10],rt=seq(0, 5, length=10)[2:10], X=rnorm(9) )
Result <- vector(length=10)
Result[1] <- Data.i$X[1]
init <- c(X=Data.i$X[1])
for (k in 1:9){
t.seq <- seq(Data.i$lt[k],Data.i$rt[k],length=10)
sol <- as.numeric(ode(y = init, times = t.seq, func = df, parms = param)[10,-1])
Result[k+1] <- log(sol+X[k+1])
init <- c(X=sol)
}
I'm fitting a parametric model to some survival data with time-dependent covariates. The fitting procedure involves solving some ODEs iteratively - one ODE per time-interval per subject, but such that the initial condition for the ODE on the interval at hand is the last value of the solution to the ODE on the preceding interval. In that sense, the ODEs depend on each other.
My problem boils done to this: Right now, I'm solving these ODEs iteratively through a loop, since I need to use the last value of the previous solution as the starting point for the next. The problem is that this looping consumes a lot of time for large datasets. Is there some way in which I can use, say, vapply, or another vectorized function, to do the same thing?
I've been searching the archives, but nothing comes up as a solution to the problem of vectorizing an operation that depends on the previous value.
Here's a code example, that doesn't produce anything statistically meaningful on its own, but illustrates my problem:
require(odeSolve)
param <- c(a=1)
df <- function(t, state, param){
with( as.list(c(state, param)), {dX<-a*X; list(c(dX))} )
}
Data.i <- data.frame( lt=seq(0, 5, length=10)[-10],rt=seq(0, 5, length=10)[2:10], X=rnorm(9) )
Result <- vector(length=10)
Result[1] <- Data.i$X[1]
init <- c(X=Data.i$X[1])
for (k in 1:9){
t.seq <- seq(Data.i$lt[k],Data.i$rt[k],length=10)
sol <- as.numeric(ode(y = init, times = t.seq, func = df, parms = param)[10,-1])
Result[k+1] <- log(sol+X[k+1])
init <- c(X=sol)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论