rinterpolationdifferential-equationsdesolve

deSolve in R: error with number of derivatives returned


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.


Solution

  • 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
    

    simulation results

    A few additional hints