[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
Tue Feb 21 22:56:14 CET 2017


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