我一直在尝试将 nlme 包用于微分方程,并且一直在努力让代码正常工作。我知道有一个 nlmeODE 包,但我希望避免使用它。我想知道是否有人知道一些将 nlme 或 nlmer 与 desolve 包结合的示例代码?
所以我已经能够解决这个问题:
# My data takes the following form
ID DAYS DOSE BIO
1 0 0 8
1 0.1 1 NA
1 7 1 9
# time-series measurements of a biomarker (BIO) for a number of patients (ID)
# remember this is a dummy example
# My ODE a simple example
simple<-function(t,x,parms) {
with(as.list(c(parms, x)), {
dX <- kin
res <- c(dX)
list(res)
})
}
# it's call within a function thats is passed to nlme
run_mod<-function(kin,r0,DOSE,DAYS){
out<-array(0,dim=c(1,length(kin)))
for (ii in 1:length(out)){
if (DAYS[ii]==0){
out[ii]<-r0[ii]
}else{
parms <- c(kin=kin[ii], r0=r0[ii])
xstart <- c(X=r0[ii])
times<-c(0,DAYS[ii])
vals<-lsoda(xstart, times, simple, parms)
out[ii]<-as.numeric(vals[2,2])
}
}
return(out)
}
# finally my call to nlme
file2.grp<-groupedData(BIO~DAYS|ID,data=file2)
na.include <- function(x) x
fit<-nlme(BIO~run_mod(kin,r0,DOSE,DAYS),
fixed = kin+r0~1, random = pdDiag(kin+r0 ~1),
data = file2.grp, start = c(7,0.01),method='ML',
na.action=na.include, naPattern = ~ !is.na(BIO))