[R] optimisation - Error in checkFunc(Func2, times, y, rho) : The number of derivatives returned by func() (22) must equal the length of the initial conditions vector (2)

Jim Lemon drjimlemon at gmail.com
Wed Feb 22 23:40:22 CET 2017


I'm not really familiar with what you are doing. when I try to debug
something like this, I run each step separately to determine where the
error is. For example, if I clean up the code a bit and run the derivs
function:

 derivs(time,y,parms)
[[1]]
 [1]  429.709540  438.844035  281.741953  404.175692  435.341449  447.532442
 [7]  448.103560  443.722972  419.132716  345.992428  363.259812 -182.179223
[13] -220.477447 -115.238330 -113.710739   -8.218996 -146.384226  -92.999563
[19] -921.027854  -61.074252 -885.550697  -40.281082

Warning messages:
1: In k4 * ((1 + k5 * cP)/(1 + k7 * cE)) * x :
  longer object length is not a multiple of shorter object length
2: In (1/k8) * k4 * ((1 + k5 * cP)/(1 + k7 * cE)) * x :
  longer object length is not a multiple of shorter object length

Obviously there is a problem with the length of "x" as it is recycled
within the function. Also, your cE and cP values are not causing a
problem as the function runs and returns what may be reasonable
values. As I don't have the deSolve package installed (and I have my
own work to do) I can only suggest trying a stepwise debugging
process.

Jim

On Wed, Feb 22, 2017 at 8:58 PM, Malgorzata Wieteska
<g.wieteska at yahoo.ie> wrote:
> Thank you Jim,
>
> I've installed XLConnect using Tools tab (install.packages option didn't
> work for some reason -I've tried before) and fixed the bracket. However, I
> still get the same error message. I've checked what cause this error and is
> caused by external data (cE and cP) fed into equations in derivs function.
>
> Do you have any suggestion how to input those values at corresponding time
> points into equations to make ode in the model_cost to integrate?
>
> Malgosia
>
>
> On Tuesday, 21 February 2017, 22:03:10, Jim Lemon <drjimlemon at gmail.com>
> wrote:
>
>
> Hi Malgorzata,
> Did you try to _install_ rather than just _load_ the XLConnect package?
>
> install.packages("XLConnect")
>
> Sad to say, your code:
>
> time=c(16,17,18,19,20,21,22,23,24,25,26)
> #x=c(20.2,18.9,16.5)
> y=c(7.63,9.22,4.86,4.78,0.38,6.13,3.91,38.41,2.58,36.95,1.73)
> cE=c(15.05,38.01,41.09,31.41,3.54,15.92,24.01,25.29,14.82,43.93,2.45)
> cP=c(0.47,0.43,4.8,1.07,0.38,0.3,0.14,0.29,0.9,2.51,1.94)
> #df<-data.frame(time,y,cE,cP)
> #dfrequire(FME)
> ### this will fail unless XLConnect has been installed
> require(XLConnect)
> #Initial values of the parameters
> parms=c(k1=500, k2=4500, k3=200,k4=2.42, k5=0.26,k6=12.2,k7=0.004,
> k8=55,k9=24,k10=8)
> #definition of the parameters function
> derivs<-function(time,y,pars){
> with(as.list(c(pars,y)),{
>   cE=c(15.05,38.01,41.09,31.41,3.54,15.92,24.01,25.29,14.82,43.93,2.45)
>   cP=c(0.47,0.43,4.8,1.07,0.38,0.3,0.14,0.29,0.9,2.51,1.94)
>   dx=(k1+(k2*cE^k10)/(k3^k10+cE^k10))/(1+cP/k6)-
>   k4*((1+k5*cP)/(1+k7*cE))*x;
>   #dRP_LH/dt
>   dy=(1/k8)*k4*((1+k5*cP)/(1+k7*cE))*x-k9*y
>   #dL/dt
>   list(c(dx,dy))
> })
> }
> initial<-c(x=x[1],y=y[1])
> model_cost<-function(pars){
> out<-ode(y=initial,time=time,func=derivs,parms=pars)
> cost<-modCost(model=out,obs=df,x="time")
> return(cost)
> } ### you seem to be missing a closing brace here
> model_cost(parms)$model
> # model fitting
> model_fit<-modFit(f=model_cost,p=parms)
> } ### maybe this is the missing closing brace
> model_cost(parms)
>
> is pretty messy with several lines commented out that may be
> necessary, and has a number of possible errors. I have pointed out a
> few (see ### comments). If the problem is the missing XLConnect
> package, perhaps installing it will produce some meaningful error
> messages.
>
> Jim
>
> On Tue, Feb 21, 2017 at 10:03 PM, Malgorzata Wieteska via R-help
> <r-help at r-project.org> wrote:
>> Hello,
>> I get an error message:Error in checkFunc(Func2, times, y, rho) :  The
>> number of derivatives returned by func() (22) must equal the length of the
>> initial conditions vector (2)
>> I try to optimise system of differential equations with 2 extra variables
>> derived from the data.frame.
>> I didn't manage to install XLConnect package, so I don't know if this is
>> the source of the problem.
>> Loading required package: XLConnectWarning message:In library(package,
>> lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :  there
>> is no package called ‘XLConnect’
>> I have missing data, so time frame is limited. I haven't got values for
>> solution of the first equation, but I hope that it isn't problem, I've got
>> the same message when putting random numbers as x values.
>
>



More information about the R-help mailing list