I a trying to fit a first order differential model using nlme
and lsoda
.
Here is the basic idea: I first define the function allowing to generate the solution of the differential equation:
library(deSolve)
ODE1 <- function(time, x, parms) {with(as.list(c(parms, x)), {
import <- excfunc(time)
dS <- import*k/tau - (S-yo)/tau
res <- c(dS)
list(res)})}
solution_ODE1 = function(tau1,k1,yo1,excitation,time){
excfunc <- approxfun(time, excitation, rule = 2)
parms <- c(tau = tau1, k = k1, yo = yo1, excfunc = excfunc)
xstart = c(S = yo1)
out <- lsoda(xstart, time, ODE1, parms)
return(out[,2])
}
I then generate data following the equation for two IDs:
time <- 0:49
excitation <- c(rep(0,10),rep(1,10),rep(0,10),rep(1,10),rep(0,10))
simu_data <- data.frame(signal = c(solution_ODE1(3,2,0.1,excitation,time)+rnorm(length(time),0,0.1),
solution_ODE1(3.2,1.5,0.3,excitation,time)+rnorm(length(time),0,0.1)),
time = rep(time,2),
excitation = rep(excitation,2),
ID = rep(c("A","B"),each = length(time)))
Here is what it looks like :
library(ggplot2)
ggplot(simu_data)+
geom_point(aes(time,signal,color = "signal"),size = 2)+
geom_line(aes(time,excitation,color = "excitation"))+
facet_wrap(~ID)
I am then trying to fit using nlme:
fit1 <- nlme(signal ~ solution_ODE1(damping,gain,eq,excitation,time),
data = simu_data,
fixed = damping + gain + eq ~1,
random = damping ~ 1 ,
groups = ~ ID,
start = c(damping = 5, gain = 1,eq = 0))
I am getting this eror, that I don't get:
Error in eval(substitute(expr), data, enclos = parent.frame()) : object 'k' not found
The traceback
shows that the error comes from the ODE1 model, which works when generating values.
16. eval(substitute(expr), data, enclos = parent.frame())
15. eval(substitute(expr), data, enclos = parent.frame())
14. with.default(as.list(c(parms, x)), {
import <- excfunc(time)
dS <- import * k/tau - (S - yo)/tau
res <- c(dS) ...
13. with(as.list(c(parms, x)), {
import <- excfunc(time)
dS <- import * k/tau - (S - yo)/tau
res <- c(dS) ...
12. func(time, state, parms, ...)
11. Func2(times[1], y)
10. eval(Func2(times[1], y), rho)
9. checkFunc(Func2, times, y, rho)
8. lsoda(xstart, time, ODE1, parms)
7. solution_ODE1(damping, gain, eq, excitation, time)
6. eval(model, data.frame(data, pars))
5. eval(model, data.frame(data, pars))
4. eval(modelExpression[[2]], envir = nlEnv)
3. eval(modelExpression[[2]], envir = nlEnv)
2. nlme.formula(signal ~ solution_ODE1(damping, gain, eq, excitation,
time), data = simu_data, fixed = damping + gain + eq ~ 1,
random = damping ~ 1, groups = ~ID, start = c(damping = 5,
gain = 1, eq = 0))
1. nlme(signal ~ solution_ODE1(damping, gain, eq, excitation, time),
data = simu_data, fixed = damping + gain + eq ~ 1, random = damping ~
1, groups = ~ID, start = c(damping = 5, gain = 1, eq = 0))
Does anyone have an idea How I should proceed ?
Edit
I tried to modify following the advise of mikeck:
ODE1 <- function(time, x, parms) {
import <- parms$excfunc(time)
dS <- import*parms$k/parms$tau - (x["S"]-parms$yo)/parms$tau
res <- c(dS)
list(res)}
Generating the data works without problems. But use of nlme
gives now:
Error in checkFunc(Func2, times, y, rho) : The number of derivatives returned by func() (0) must equal the length of the initial conditions vector (100)
with the following traceback:
> traceback()
10: stop(paste("The number of derivatives returned by func() (",
length(tmp[[1]]), ") must equal the length of the initial conditions vector (",
length(y), ")", sep = ""))
9: checkFunc(Func2, times, y, rho)
8: lsoda(xstart, time, ODE1, parms) at #5
7: solution_ODE1(damping, gain, eq, excitation, time)
6: eval(model, data.frame(data, pars))
5: eval(model, data.frame(data, pars))
4: eval(modelExpression[[2]], envir = nlEnv)
3: eval(modelExpression[[2]], envir = nlEnv)
2: nlme.formula(signal ~ solution_ODE1(damping, gain, eq, excitation,
time), data = simu_data, fixed = damping + gain + eq ~ 1,
random = damping ~ 1, groups = ~ID, start = c(damping = 5,
gain = 1, eq = 0))
1: nlme(signal ~ solution_ODE1(damping, gain, eq, excitation, time),
data = simu_data, fixed = damping + gain + eq ~ 1, random = damping ~
1, groups = ~ID, start = c(damping = 5, gain = 1, eq = 0))
nlmeODE
package? – Aspaxstart <- yo1
(then referring directly tox
inODE1
, but I'm stilling getting an "illegal input" message ... – AspaODE1()
to not usewith()
, i.e. instead useparms$k
etc.? The error message looks like it might be a scoping issue that is cropping up somehow. – Ethicizenlme
does internally, but it looks it give vector of initial condition to the function, creating thus errors – Rathe