Hong
Hong

Reputation: 31

Parameter and initial conditions fitting ODE models with nls.lm

I am currently trying to fit ODE functional responses using the Levenberg-Marquardt routine (nls.lm) in pkg-minpack.lm following the tutorial here (http://www.r-bloggers.com/learning-r-parameter-fitting-for-models-involving-differential-equations/).

In the example, he fits the data by first setting up a function rxnrate which I modified shown below:

library(ggplot2) #library for plotting
library(reshape2) # library for reshaping data (tall-narrow <-> short-wide)
library(deSolve) # library for solving differential equations
library(minpack.lm) # library for least squares fit using levenberg-marquart algorithm
# prediction of concentration
# rate function
rxnrate=function(t,c,parms){
  # rate constant passed through a list called parms
  k1=parms$k1
  k2=parms$k2
  k3=parms$k3

 # c is the concentration of species

 # derivatives dc/dt are computed below
  r=rep(0,length(c))
  r[1]=-k1*c["A"]  #dcA/dt
  r[2]=k1*c["A"]-k2*c["B"]+k3*c["C"] #dcB/dt
  r[3]=k2*c["B"]-k3*c["C"] #dcC/dt

  # the computed derivatives are returned as a list
  # order of derivatives needs to be the same as the order of species in c
  return(list(r))

}  

My problem is that the initial condition of each states can be also considered as the estimated parameters. However, it does not work properly at the moment. Below is my code:

# function that calculates residual sum of squares
ssq=function(myparms){

  # inital concentration 
  cinit=c(A=myparms[4],B=0,C=0)

  # time points for which conc is reported
  # include the points where data is available
  t=c(seq(0,5,0.1),df$time)
  t=sort(unique(t))
  # parms from the parameter estimation routine
  k1=myparms[1]
  k2=myparms[2]
  k3=myparms[3]
  # solve ODE for a given set of parameters
   out=ode(y=cinit,times=t,func=rxnrate,parms=list(k1=k1,k2=k2,k3=k3))


  # Filter data that contains time points where data is available
  outdf=data.frame(out)
  outdf=outdf[outdf$time %in% df$time,]
  # Evaluate predicted vs experimental residual
  preddf=melt(outdf,id.var="time",variable.name="species",value.name="conc")
  expdf=melt(df,id.var="time",variable.name="species",value.name="conc")
  ssqres=preddf$conc-expdf$conc

  # return predicted vs experimental residual
  return(ssqres)

}

# parameter fitting using levenberg marquart algorithm
# initial guess for parameters
 myparms=c(k1=0.5,k2=0.5,k3=0.5,A=1)

# fitting
fitval=nls.lm(par=myparms,fn=ssq)

Once I run this, an error comes out like this

Error in chol.default(object$hessian) : 
  the leading minor of order 1 is not positive definite

Upvotes: 3

Views: 1873

Answers (1)

J_F
J_F

Reputation: 10362

The problem of your code is the following one:

In the code-line cinit=c(A=myparms[4],B=0,C=0) you gave A the value of myparms[4] AND the name of myparms[4]. Let's see:

myparms=c(k1=0.5,k2=0.5,k3=0.5,A=1)
cinit=c(A=myparms[4],B=0,C=0)
print(cinit)
A.A   B   C 
1   0   0 

to solve this problem, you can do this:

myparms=c(k1=0.5,k2=0.5,k3=0.5,A=1)
cinit=c(A=unname(myparms[4]),B=0,C=0)
print(cinit)
A   B   C 
1   0   0 

or this:

myparms=c(k1=0.5,k2=0.5,k3=0.5,1)
cinit=c(A=unname(myparms[4]),B=0,C=0)
print(cinit)
A   B   C 
1   0   0 

Then your code will work!

Best regards, J_F

Upvotes: 3

Related Questions