Skip to content

[R-sig-dyn-mod] Help to fit a dynamic model - irregular time

6 messages · Bernardo Rangel Tura, Soetaert, Karline, Setzer.Woodrow@epamail.epa.gov

#
Hi R-masters


I need help to fit a dynamic model. In my assumption the model is

rate(t+1) ~ A(t)*rate(t)*Error1
A(t+1) ~ A(t) + b + Error2

In attached file I put my 45 points of information, but the time of
observation is irregular. Look the 3 initial observations

time
rate
                                0.16
                                0.09
                                0.25
                                0.23
                                0.38
                                0.28


Is possible fit this model?
#
On Wed, 2009-12-09 at 06:46 -0200, Bernardo Rangel Tura wrote:
Sorry malformation

  
    
#
Bernardo,

You question is quite vague, so my answer will be quite vague also.  

Fitting a dynamic model in R, and using unevenly spaced times in R is
not a problem. This is what you have to do:

1.	You make a model function, call it "func", put the parameters in
a vector called "parms", put the initial values in a vector called "y" 

2.	You solve this model, using e.g. function ode from package
deSolve. See help files in deSolve or the package vignette.
You request a solution only at the points of observation, for instance
something like this:

  times <- c(0.16, 0.25 )   # the output times
  out <- as.data.frame(ode(func = func, times =Times , parms =parms, y=
y))    # solves the model


3.	You make a "cost" function that:
	a. has as input argument(s) the parameter(s) to be fitted, 
 	b. puts these parameters in "parms"
	c. solves the model, as outlined in (2), and 
	d. that returns the sum of squared residuals. 

For instance, if your model variable is called "Y", and the data are in
"data$Y", then your cost function should return:
sum((out$Y - data$Y)^2)

4.	You use one of R's optimization algorithms, e.g. optim to fit
the model to the data. See help of the optimization function.

Hope this helps,

karline

-----Original Message-----
From: r-sig-dynamic-models-bounces at r-project.org
[mailto:r-sig-dynamic-models-bounces at r-project.org] On Behalf Of
Bernardo Rangel Tura
Sent: Wednesday, December 09, 2009 9:50 AM
To: Special Interest Group for Dynamic Simulation Models in R
Subject: Re: [R-sig-dyn-mod] Help to fit a dynamic model - irregular
time
On Wed, 2009-12-09 at 06:46 -0200, Bernardo Rangel Tura wrote:
Sorry malformation

  
    
#
Dr. Tura,
I wonder if you need to specify the model a little more clearly.  As
written, A() is an unknown function such that A(t+1) - A(t) = b +
Error2.  But, you have times on a finer time scale, since the first
three times do not even span the time from 0 to 1.  Your model does not
give any way to interpolate to shorter time intervals.
I can see how you might modify the recursion for A() to small time
increment dt (A(t + dt) ~ A(t) + b*dt + Error2), but it is not so clear
how rate should change over a short interval.
Maybe something like:

log(rate(t+dt)) - log(rate(t))= log(A(t))*dt + log(Error1)

When dt = 1, this gives you the expression you gave us.

You need to decide how or whether the error variances should depend on
time, too.  If they are the result of some process that you are thinking
of as stochastic, then the variance probably depends on the time
interval.

You also need to tell us what A(0) ought to be, though I guess that
could be estimated, also.

Woody

R. Woodrow Setzer, Ph. D.
National Center for Computational Toxicology
http://www.epa.gov/comptox
US Environmental Protection Agency
Mail Drop B205-01/US EPA/RTP, NC 27711
Ph: (919) 541-0128    Fax: (919) 541-1194

r-sig-dynamic-models-bounces at r-project.org wrote on 12/09/2009 03:46:55
AM:
in R
#
On Wed, 2009-12-09 at 11:11 +0100, Soetaert, Karline wrote:
Karline,

I don't think your answer is vague! Your answer is very usefull.

Tank you!

I will look de package deSolve ...
#
On Wed, 2009-12-09 at 08:27 -0500, Setzer.Woodrow at epamail.epa.gov wrote:
Hi Woody,

First i extract the data from a plot published. 

This work is about 180-days follow-up  of patients with a cardiovascular
disease so 0.16 is same 5 days (30 days * .16), .25 is same 8 days (30
days*.25), 1.3 is same 39 days (30*1.3) etc

Is very difficult have precision of information in scale less  than 1
day in this type of study.

I think the risk of clinical event will modify with the time (likely a
log curve), but the information is cumulative incidence of event.
Because this i suggest the model

rate(t+1) ~ A(t)*rate(t)*Error1 --> d rate/dt ~ A(t)*Error1
A(t+1) ~ A(t) + b + Error2 -> dA/dt ~ b + Error2

In early time (first days) the rate (cumulative incidence) will grow
fast - so b is positive, after this  the rate will grow slow - b is
positive but decreasing in relation a initial times

Well, the number of patients in the study will reduce over time - by
death, other clinical events or lost of follow-up - so the error
variances will increase with time  but is reasonable to think that
correlate