i have been trying to use the nlme package for differential equations and have struggled to get the code to work. I am aware there is an nlmeODE package but i was hoping to avoid using it. I was wondering if anyone knows of some example code combining nlme or nlmer with the desolve package?
So i've been able to solve the problem:
# 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))