I am currently trying to create a function that will solve a compartment model using the deSolve function. The parameters are variable over time so I have used approxfun() to interpolate the parameter estimates. I have 4 variables and currently 122 time steps in my dummy data set. When I try to run the function, I get this error:
Error in checkFunc(Func2, times, y, rho) : The number of derivatives returned by func() (488) must equal the length of the initial conditions vector (4)
I will paste my code below! Any help is greatly appreciated.
I am expecting to get an output which is a dataframe of the value of each variable (E, L, L3f, L3p) at each time point.
The parameter names in the equations are mistyped and refer to the global vectors, not the interpolated values. A corrected version should look like this:
# Differential equations
dE <- -(mu1 + (2 * dev1)) * E + 100
dL <- -(mu2 + (2 * dev1)) * L + (2 * dev1) * E
dL3f <- -(mu3 + mig1) * L3f + (2 * dev1) * L
dL3p <- -mu4 * (L3p * (1 - mig2)) - mu5 * (mig2 * L3p) + mig1 * L3f
time
in the model function (i.e. the actual time step) instead of times
that is the vector of all time steps. It is not an error, but would improve readability.<-
for assignments, not sometimes =
and sometimes <-
. The equal =
-sign should only be used for named parameter matching.print
or cat
statements within your code, or (even better) set breakpoints in RStudio with "Debug - Toggle Breakpoints".