Skip to content

[R-sig-dyn-mod] FME Fitting a multicompartmental model using the sum of several variables

4 messages · Jesus Maria Frias Celayeta, Blanco Pérez, Juan Carlos, Thomas Petzoldt

#
I'm new to deSolve and FME.
I'm trying to fit a two-compartment model but the observed data are the sum of the two compartments. Can this be done with FME?. I have tried to compute the sum from the ODE output but then the result is not longer a deSolve object and multiple errors appear.

Juan
#
Hi Juan,

Using the example from
https://www.r-bloggers.com/learning-r-parameter-fitting-for-models-involving-differential-equations/

you could include the sum in your ode system like:

rxnrate=function(t,c,parms){

 # rate constant passed through a list called parms
 k1=parms$k1
 k2=parms$k2

 # 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"] #dcB/dt
 r[3]=k2*c["B"] #dcC/dt
 r[4]= r[1]+r[2]+r[3] # sum of A,B,C

 # 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))

}

all the best,

Jesus
#
Hi Jesus,
Thank you for your solution. It seems a good alternative to using the modCost function of FME, which does not seem to work if all variables are not included.


-----Mensaje original-----
De: R-sig-dynamic-models [mailto:r-sig-dynamic-models-bounces at r-project.org] En nombre de Jesus Maria Frias Celayeta
Enviado el: martes, 17 de septiembre de 2019 12:24
Para: Special Interest Group for Dynamic Simulation Models in R
Asunto: Re: [R-sig-dyn-mod] FME Fitting a multicompartmental model using the sum of several variables

Hi Juan,

Using the example from
https://www.r-bloggers.com/learning-r-parameter-fitting-for-models-involving-differential-equations/

you could include the sum in your ode system like:

rxnrate=function(t,c,parms){

 # rate constant passed through a list called parms
 k1=parms$k1
 k2=parms$k2

 # 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"] #dcB/dt
 r[3]=k2*c["B"] #dcC/dt
 r[4]= r[1]+r[2]+r[3] # sum of A,B,C

 # 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))

}

all the best,

Jesus
#
On 17.09.2019 at 15:02 Blanco P?rez, Juan Carlos wrote:
1) modCost is very flexible and allows also such functionality. It is 
recommended to organize the data in "long format"

2) Instead of? another derivative, you can also to return an auxillary 
variable in the ode function that contains the sum:

  [...]
  r[1]=-k1*c["A"] #dcA/dt
  r[2]=k1*c["A"]-k2*c["B"] #dcB/dt
  r[3]=k2*c["B"] #dcC/dt

  list(r, sum=r[1]+r[2]+r[3])

As the docs tell us: "The return value of?func?should be a list, whose 
first element is a vector containing the derivatives of?y?with respect 
to?time, and whose next elements are global values that are required at 
each point in?times. The derivatives must be specified in the?same 
order?as the state variables?y."

More about this in the examples and slides found at: 
http://desolve.r-forge.r-project.org

Thomas

PS: Please help the moderator and yourself and sign in to the mailing 
list to avoid manual moderation acknowledgment.