Hello,
I am using odesolve to simulate a group of people moving through time and transmitting infections to one another.
In Matlab, there is a NonNegative option which tells the Matlab solver to keep the vector elements of the ODE solution non-negative at all times. What is the right way to do this in R?
Thanks,
Jeremy
P.S., Below is a simplified version of the code I use to try to do this, but I am not sure that it is theoretically right
dynmodel <- function(t,y,p)
{
## Initialize parameter values
birth <- p$mybirth(t)
death <- p$mydeath(t)
recover <- p$myrecover
beta <- p$mybeta
vaxeff <- p$myvaxeff
vaccinated <- p$myvax(t)
vax <- vaxeff*vaccinated/100
## If the state currently has negative quantities (shouldn't have), then reset to reasonable values for computing meaningful derivatives
for (i in 1:length(y)) {
if (y[i]<0) {
y[i] <- 0
}
}
S <- y[1]
I <- y[2]
R <- y[3]
N <- y[4]
shat <- (birth*(1-vax)) - (death*S) - (beta*S*I/N)
ihat <- (beta*S*I/N) - (death*I) - (recover*I)
rhat <- (birth*(vax)) + (recover*I) - (death*R)
## Do we overshoot into negative space, if so shrink derivative to bring state to 0
## then rescale the components that take the derivative negative
if (shat+S<0) {
shat_old <- shat
shat <- -1*S
scaled_transmission <- (shat/shat_old)*(beta*S*I/N)
ihat <- scaled_transmission - (death*I) - (recover*I)
}
if (ihat+I<0) {
ihat_old <- ihat
ihat <- -1*I
scaled_recovery <- (ihat/ihat_old)*(recover*I)
rhat <- scaled_recovery +(birth*(vax)) - (death*R)
}
if (rhat+R<0) {
rhat <- -1*R
}
nhat <- shat + ihat + rhat
if (nhat+N<0) {
nhat <- -1*N
}
## return derivatives
list(c(shat,ihat,rhat,nhat),c(0))
}
Using odesolve to produce non-negative solutions
15 messages · Jeremy Goldhaber-Fiebert, Spencer Graves, Martin Henry H. Stevens +3 more
Hello,
I am using odesolve to simulate a group of people moving through time and transmitting infections to one another.
In Matlab, there is a NonNegative option which tells the Matlab solver to keep the vector elements of the ODE solution non-negative at all times. What is the right way to do this in R?
Thanks,
Jeremy
P.S., Below is a simplified version of the code I use to try to do this, but I am not sure that it is theoretically right
dynmodel <- function(t,y,p)
{
## Initialize parameter values
birth <- p$mybirth(t)
death <- p$mydeath(t)
recover <- p$myrecover
beta <- p$mybeta
vaxeff <- p$myvaxeff
vaccinated <- p$myvax(t)
vax <- vaxeff*vaccinated/100
## If the state currently has negative quantities (shouldn't have), then reset to reasonable values for computing meaningful derivatives
for (i in 1:length(y)) {
if (y[i]<0) {
y[i] <- 0
}
}
S <- y[1]
I <- y[2]
R <- y[3]
N <- y[4]
shat <- (birth*(1-vax)) - (death*S) - (beta*S*I/N)
ihat <- (beta*S*I/N) - (death*I) - (recover*I)
rhat <- (birth*(vax)) + (recover*I) - (death*R)
## Do we overshoot into negative space, if so shrink derivative to bring state to 0
## then rescale the components that take the derivative negative
if (shat+S<0) {
shat_old <- shat
shat <- -1*S
scaled_transmission <- (shat/shat_old)*(beta*S*I/N)
ihat <- scaled_transmission - (death*I) - (recover*I)
}
if (ihat+I<0) {
ihat_old <- ihat
ihat <- -1*I
scaled_recovery <- (ihat/ihat_old)*(recover*I)
rhat <- scaled_recovery +(birth*(vax)) - (death*R)
}
if (rhat+R<0) {
rhat <- -1*R
}
nhat <- shat + ihat + rhat
if (nhat+N<0) {
nhat <- -1*N
}
## return derivatives
list(c(shat,ihat,rhat,nhat),c(0))
}
1 day later
On the 'lsoda' help page, I did not see any option to force some
or all parameters to be nonnegative.
Have you considered replacing the parameters that must be
nonnegative with their logarithms? This effective moves the 0 lower
limit to (-Inf) and seems to have worked well for me in the past.
Often, it can even make the log likelihood or sum of squares surface
more elliptical, which means that the standard normal approximation for
the sampling distribution of parameter estimates will likely be more
accurate.
Hope this helps.
Spencer Graves
p.s. Your example seems not to be self contained. If I could have
easily copied it from your email and run it myself, I might have been
able to offer more useful suggestions.
Jeremy Goldhaber-Fiebert wrote:
Hello,
I am using odesolve to simulate a group of people moving through time and transmitting infections to one another.
In Matlab, there is a NonNegative option which tells the Matlab solver to keep the vector elements of the ODE solution non-negative at all times. What is the right way to do this in R?
Thanks,
Jeremy
P.S., Below is a simplified version of the code I use to try to do this, but I am not sure that it is theoretically right
dynmodel <- function(t,y,p)
{
## Initialize parameter values
birth <- p$mybirth(t)
death <- p$mydeath(t)
recover <- p$myrecover
beta <- p$mybeta
vaxeff <- p$myvaxeff
vaccinated <- p$myvax(t)
vax <- vaxeff*vaccinated/100
## If the state currently has negative quantities (shouldn't have), then reset to reasonable values for computing meaningful derivatives
for (i in 1:length(y)) {
if (y[i]<0) {
y[i] <- 0
}
}
S <- y[1]
I <- y[2]
R <- y[3]
N <- y[4]
shat <- (birth*(1-vax)) - (death*S) - (beta*S*I/N)
ihat <- (beta*S*I/N) - (death*I) - (recover*I)
rhat <- (birth*(vax)) + (recover*I) - (death*R)
## Do we overshoot into negative space, if so shrink derivative to bring state to 0
## then rescale the components that take the derivative negative
if (shat+S<0) {
shat_old <- shat
shat <- -1*S
scaled_transmission <- (shat/shat_old)*(beta*S*I/N)
ihat <- scaled_transmission - (death*I) - (recover*I)
}
if (ihat+I<0) {
ihat_old <- ihat
ihat <- -1*I
scaled_recovery <- (ihat/ihat_old)*(recover*I)
rhat <- scaled_recovery +(birth*(vax)) - (death*R)
}
if (rhat+R<0) {
rhat <- -1*R
}
nhat <- shat + ihat + rhat
if (nhat+N<0) {
nhat <- -1*N
}
## return derivatives
list(c(shat,ihat,rhat,nhat),c(0))
}
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
3 days later
Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick and also changing the relative and absolute tolerance inputs but I still get the problem for certain combinations of parameters and initial conditions. It occurs both under MS Windows XP Service Pack 2 and on a Linux cluster so I am pretty sure it is not platform specific. My real question to the group is if there is not a work around in lsoda are there other ode solvers in R that will allow the constraint of solutions to the ODEs remain non-negative? Best regards, Jeremy
Spencer Graves <spencer.graves at pdf.com> 6/8/2007 9:51 AM >>>
On the 'lsoda' help page, I did not see any option to force some
or all parameters to be nonnegative.
Have you considered replacing the parameters that must be
nonnegative with their logarithms? This effective moves the 0 lower
limit to (-Inf) and seems to have worked well for me in the past.
Often, it can even make the log likelihood or sum of squares surface
more elliptical, which means that the standard normal approximation for
the sampling distribution of parameter estimates will likely be more
accurate.
Hope this helps.
Spencer Graves
p.s. Your example seems not to be self contained. If I could have
easily copied it from your email and run it myself, I might have been
able to offer more useful suggestions.
Jeremy Goldhaber-Fiebert wrote:
Hello,
I am using odesolve to simulate a group of people moving through time and transmitting infections to one another.
In Matlab, there is a NonNegative option which tells the Matlab solver to keep the vector elements of the ODE solution non-negative at all times. What is the right way to do this in R?
Thanks,
Jeremy
P.S., Below is a simplified version of the code I use to try to do this, but I am not sure that it is theoretically right
dynmodel <- function(t,y,p)
{
## Initialize parameter values
birth <- p$mybirth(t)
death <- p$mydeath(t)
recover <- p$myrecover
beta <- p$mybeta
vaxeff <- p$myvaxeff
vaccinated <- p$myvax(t)
vax <- vaxeff*vaccinated/100
## If the state currently has negative quantities (shouldn't have), then reset to reasonable values for computing meaningful derivatives
for (i in 1:length(y)) {
if (y[i]<0) {
y[i] <- 0
}
}
S <- y[1]
I <- y[2]
R <- y[3]
N <- y[4]
shat <- (birth*(1-vax)) - (death*S) - (beta*S*I/N)
ihat <- (beta*S*I/N) - (death*I) - (recover*I)
rhat <- (birth*(vax)) + (recover*I) - (death*R)
## Do we overshoot into negative space, if so shrink derivative to bring state to 0
## then rescale the components that take the derivative negative
if (shat+S<0) {
shat_old <- shat
shat <- -1*S
scaled_transmission <- (shat/shat_old)*(beta*S*I/N)
ihat <- scaled_transmission - (death*I) - (recover*I)
}
if (ihat+I<0) {
ihat_old <- ihat
ihat <- -1*I
scaled_recovery <- (ihat/ihat_old)*(recover*I)
rhat <- scaled_recovery +(birth*(vax)) - (death*R)
}
if (rhat+R<0) {
rhat <- -1*R
}
nhat <- shat + ihat + rhat
if (nhat+N<0) {
nhat <- -1*N
}
## return derivatives
list(c(shat,ihat,rhat,nhat),c(0))
}
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi Jeremy, First, setting hmax to a small number could prevent a large step, if you think that is a problem. Second, however, I don't see how you can get a negative population size when using the log trick. I would think that that would prevent completely any negative values of N (i.e. e^-100000 > 0). Can you explain? or do you want to a void that trick? The only other solver I know of is rk4 and it is not recommended. Hank
On Jun 11, 2007, at 11:46 AM, Jeremy Goldhaber-Fiebert wrote:
Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick and also changing the relative and absolute tolerance inputs but I still get the problem for certain combinations of parameters and initial conditions. It occurs both under MS Windows XP Service Pack 2 and on a Linux cluster so I am pretty sure it is not platform specific. My real question to the group is if there is not a work around in lsoda are there other ode solvers in R that will allow the constraint of solutions to the ODEs remain non-negative? Best regards, Jeremy
Spencer Graves <spencer.graves at pdf.com> 6/8/2007 9:51 AM >>>
On the 'lsoda' help page, I did not see any option to force some
or all parameters to be nonnegative.
Have you considered replacing the parameters that must be
nonnegative with their logarithms? This effective moves the 0 lower
limit to (-Inf) and seems to have worked well for me in the past.
Often, it can even make the log likelihood or sum of squares surface
more elliptical, which means that the standard normal approximation
for
the sampling distribution of parameter estimates will likely be more
accurate.
Hope this helps.
Spencer Graves
p.s. Your example seems not to be self contained. If I could have
easily copied it from your email and run it myself, I might have been
able to offer more useful suggestions.
Jeremy Goldhaber-Fiebert wrote:
Hello,
I am using odesolve to simulate a group of people moving through
time and transmitting infections to one another.
In Matlab, there is a NonNegative option which tells the Matlab
solver to keep the vector elements of the ODE solution non-
negative at all times. What is the right way to do this in R?
Thanks,
Jeremy
P.S., Below is a simplified version of the code I use to try to do
this, but I am not sure that it is theoretically right
dynmodel <- function(t,y,p)
{
## Initialize parameter values
birth <- p$mybirth(t)
death <- p$mydeath(t)
recover <- p$myrecover
beta <- p$mybeta
vaxeff <- p$myvaxeff
vaccinated <- p$myvax(t)
vax <- vaxeff*vaccinated/100
## If the state currently has negative quantities (shouldn't
have), then reset to reasonable values for computing meaningful
derivatives
for (i in 1:length(y)) {
if (y[i]<0) {
y[i] <- 0
}
}
S <- y[1]
I <- y[2]
R <- y[3]
N <- y[4]
shat <- (birth*(1-vax)) - (death*S) - (beta*S*I/N)
ihat <- (beta*S*I/N) - (death*I) - (recover*I)
rhat <- (birth*(vax)) + (recover*I) - (death*R)
## Do we overshoot into negative space, if so shrink derivative to
bring state to 0
## then rescale the components that take the derivative negative
if (shat+S<0) {
shat_old <- shat
shat <- -1*S
scaled_transmission <- (shat/shat_old)*(beta*S*I/N)
ihat <- scaled_transmission - (death*I) - (recover*I)
}
if (ihat+I<0) {
ihat_old <- ihat
ihat <- -1*I
scaled_recovery <- (ihat/ihat_old)*(recover*I)
rhat <- scaled_recovery +(birth*(vax)) - (death*R)
}
if (rhat+R<0) {
rhat <- -1*R
}
nhat <- shat + ihat + rhat
if (nhat+N<0) {
nhat <- -1*N
}
## return derivatives
list(c(shat,ihat,rhat,nhat),c(0))
}
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Dr. Hank Stevens, Assistant Professor 338 Pearson Hall Botany Department Miami University Oxford, OH 45056 Office: (513) 529-4206 Lab: (513) 529-4262 FAX: (513) 529-4243 http://www.cas.muohio.edu/~stevenmh/ http://www.muohio.edu/ecology/ http://www.muohio.edu/botany/ "E Pluribus Unum"
<in line>
Martin Henry H. Stevens wrote:
Hi Jeremy, First, setting hmax to a small number could prevent a large step, if you think that is a problem. Second, however, I don't see how you can get a negative population size when using the log trick.
SG: Can lsoda estimate complex or imaginary parameters?
I would think that that would prevent completely any negative values of N (i.e. e^-100000 > 0). Can you explain? or do you want to a void that trick? The only other solver I know of is rk4 and it is not recommended. Hank On Jun 11, 2007, at 11:46 AM, Jeremy Goldhaber-Fiebert wrote:
Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick
<snip>
Hi Spencer, I have copied Woody Setzer. I have no idea whether lsoda can estimate parameters that could take imaginary values. Hank
On Jun 11, 2007, at 12:52 PM, Spencer Graves wrote:
<in line> Martin Henry H. Stevens wrote:
Hi Jeremy, First, setting hmax to a small number could prevent a large step, if you think that is a problem. Second, however, I don't see how you can get a negative population size when using the log trick.
SG: Can lsoda estimate complex or imaginary parameters?
Hmm. I have no idea.
I would think that that would prevent completely any negative values of N (i.e. e^-100000 > 0). Can you explain? or do you want to a void that trick? The only other solver I know of is rk4 and it is not recommended. Hank On Jun 11, 2007, at 11:46 AM, Jeremy Goldhaber-Fiebert wrote:
Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick
<snip>
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Dr. Hank Stevens, Assistant Professor 338 Pearson Hall Botany Department Miami University Oxford, OH 45056 Office: (513) 529-4206 Lab: (513) 529-4262 FAX: (513) 529-4243 http://www.cas.muohio.edu/~stevenmh/ http://www.muohio.edu/ecology/ http://www.muohio.edu/botany/ "E Pluribus Unum"
Hi Jeremy, A smaller step size may or may not help. If the issue is simply truncation error, that is the error involved in discretizing the differential equations, then a smaller step size would help. If, however, the true solution to the differential equation is negative, for some t, then the numerical solution should also be negative. If the negative solution does not make sense, then the system of equation needs to be examined to see when and why negative solutions arise. Perhaps, I am just making this up - there needs to be a "barrier function" that slows down the trajectory as it approaches zero from its initial value. It is also possible that only certain regions of the parameter space are allowed in the sense that only there the solution is feasible for all t. So, in your example, the parameters might not be realistic. In short, if you are sure that the numerical solution is accurate, then you need to go back to your system of equations and analyze them carefully. Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Martin Henry H. Stevens Sent: Monday, June 11, 2007 1:03 PM To: Spencer Graves Cc: Jeremy Goldhaber-Fiebert; R-Help; Setzer.Woodrow at epamail.epa.gov Subject: Re: [R] Fwd: Using odesolve to produce non-negative solutions Hi Spencer, I have copied Woody Setzer. I have no idea whether lsoda can estimate parameters that could take imaginary values. Hank
On Jun 11, 2007, at 12:52 PM, Spencer Graves wrote:
<in line> Martin Henry H. Stevens wrote:
Hi Jeremy, First, setting hmax to a small number could prevent a large step, if you think that is a problem. Second, however, I don't see how you can get a negative population size when using the log trick.
SG: Can lsoda estimate complex or imaginary parameters?
Hmm. I have no idea.
I would think that that would prevent completely any negative values of N (i.e. e^-100000 > 0). Can you explain? or do you want to a void that trick? The only other solver I know of is rk4 and it is not recommended. Hank On Jun 11, 2007, at 11:46 AM, Jeremy Goldhaber-Fiebert wrote:
Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick
<snip>
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Dr. Hank Stevens, Assistant Professor 338 Pearson Hall Botany Department Miami University Oxford, OH 45056 Office: (513) 529-4206 Lab: (513) 529-4262 FAX: (513) 529-4243 http://www.cas.muohio.edu/~stevenmh/ http://www.muohio.edu/ecology/ http://www.muohio.edu/botany/ "E Pluribus Unum" ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Spencer, Lsoda does not "estimate" any parameters (nlmeODE does parameter estimation). It just computes the solution trajectory, at discrete times, of a dynamical systems (i.e. set of differential equations). It only works with real numbers, as far as I know. Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Spencer Graves Sent: Monday, June 11, 2007 12:53 PM To: Martin Henry H. Stevens Cc: Jeremy Goldhaber-Fiebert; r-help at stat.math.ethz.ch Subject: Re: [R] Fwd: Using odesolve to produce non-negative solutions <in line>
Martin Henry H. Stevens wrote:
Hi Jeremy, First, setting hmax to a small number could prevent a large step, if you think that is a problem. Second, however, I don't see how you can get a negative population size when using the log trick.
SG: Can lsoda estimate complex or imaginary parameters?
I would think that that would prevent completely any negative values of N (i.e. e^-100000 > 0). Can you explain? or do you want to a void that trick? The only other solver I know of is rk4 and it is not recommended. Hank On Jun 11, 2007, at 11:46 AM, Jeremy Goldhaber-Fiebert wrote:
Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick
<snip> ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi Jeremy, A smaller step size may or may not help. If the issue is simply truncation error, that is the error involved in discretizing the differential equations, then a smaller step size would help. If, however, the true solution to the differential equation is negative, for some t, then the numerical solution should also be negative. If the negative solution does not make sense, then the system of equation needs to be examined to see when and why negative solutions arise. Perhaps, I am just making this up - there needs to be a "dampening function" that slows down the trajectory as it approaches zero from its initial value. It is also possible that only certain regions of the parameter space (note that initial conditions are also parameters) are allowed in the sense that only there the solution is feasible for all t. So, in your example, the parameters might not be realistic. In short, if you are sure that the numerical solution is accurate, then you need to go back to your system of equations and analyze them carefully. Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jeremy Goldhaber-Fiebert Sent: Monday, June 11, 2007 11:47 AM To: Spencer Graves Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Fwd: Using odesolve to produce non-negative solutions Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick and also changing the relative and absolute tolerance inputs but I still get the problem for certain combinations of parameters and initial conditions. It occurs both under MS Windows XP Service Pack 2 and on a Linux cluster so I am pretty sure it is not platform specific. My real question to the group is if there is not a work around in lsoda are there other ode solvers in R that will allow the constraint of solutions to the ODEs remain non-negative? Best regards, Jeremy
Spencer Graves <spencer.graves at pdf.com> 6/8/2007 9:51 AM >>>
On the 'lsoda' help page, I did not see any option to force some
or all parameters to be nonnegative.
Have you considered replacing the parameters that must be
nonnegative with their logarithms? This effective moves the 0 lower
limit to (-Inf) and seems to have worked well for me in the past.
Often, it can even make the log likelihood or sum of squares surface
more elliptical, which means that the standard normal approximation for
the sampling distribution of parameter estimates will likely be more
accurate.
Hope this helps.
Spencer Graves
p.s. Your example seems not to be self contained. If I could have
easily copied it from your email and run it myself, I might have been
able to offer more useful suggestions.
Jeremy Goldhaber-Fiebert wrote:
Hello, I am using odesolve to simulate a group of people moving through time and
transmitting infections to one another.
In Matlab, there is a NonNegative option which tells the Matlab solver to
keep the vector elements of the ODE solution non-negative at all times. What is the right way to do this in R?
Thanks, Jeremy P.S., Below is a simplified version of the code I use to try to do this,
but I am not sure that it is theoretically right
dynmodel <- function(t,y,p)
{
## Initialize parameter values
birth <- p$mybirth(t)
death <- p$mydeath(t)
recover <- p$myrecover
beta <- p$mybeta
vaxeff <- p$myvaxeff
vaccinated <- p$myvax(t)
vax <- vaxeff*vaccinated/100
## If the state currently has negative quantities (shouldn't have), then
reset to reasonable values for computing meaningful derivatives
for (i in 1:length(y)) {
if (y[i]<0) {
y[i] <- 0
}
}
S <- y[1]
I <- y[2]
R <- y[3]
N <- y[4]
shat <- (birth*(1-vax)) - (death*S) - (beta*S*I/N)
ihat <- (beta*S*I/N) - (death*I) - (recover*I)
rhat <- (birth*(vax)) + (recover*I) - (death*R)
## Do we overshoot into negative space, if so shrink derivative to bring
state to 0
## then rescale the components that take the derivative negative
if (shat+S<0) {
shat_old <- shat
shat <- -1*S
scaled_transmission <- (shat/shat_old)*(beta*S*I/N)
ihat <- scaled_transmission - (death*I) - (recover*I)
}
if (ihat+I<0) {
ihat_old <- ihat
ihat <- -1*I
scaled_recovery <- (ihat/ihat_old)*(recover*I)
rhat <- scaled_recovery +(birth*(vax)) - (death*R)
}
if (rhat+R<0) {
rhat <- -1*R
}
nhat <- shat + ihat + rhat
if (nhat+N<0) {
nhat <- -1*N
}
## return derivatives
list(c(shat,ihat,rhat,nhat),c(0))
}
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi Ravi, Thanks for your response. I tried this in Berkeley Madonna and in Matlab. In Berkeley Madonna I did not have the problem (RK4 solver). In Matlab (ode45 solver), I had the problem if I did not use their NonNegative option. My thought was that NonNegative uses something like an additional piece of logic in modifying step size (maybe something like: if stepsize * derivative + current condition is negative, then reduce step size), but I don't know. My original application was to generate ode output for a variety of unknown parameters, comparing the output to observed data and thereby using a likelihood-based approach to identify unknown parameter combinations that are most consistent with observed data given the uncertainty. First, I wanted to get a sense of how the model performed over a range of parameters that seem plausible based on literature review. I had originally thought to do this in Matlab and built a little proof of concept searching program, but given that R is free and has many other great packages for doing optimization and statistical analysis (and is easy to setup which is great for cluster computing situations), I thought it would be better to do it in R. When I did this, I got negative values and hence sent to the list. Once again, thanks for your help. Not sure if this clarification email will generate other suggestions or thoughts. Best, Jeremy
"Ravi Varadhan" <rvaradhan at jhmi.edu> 6/11/2007 2:11 PM >>>
Hi Jeremy, A smaller step size may or may not help. If the issue is simply truncation error, that is the error involved in discretizing the differential equations, then a smaller step size would help. If, however, the true solution to the differential equation is negative, for some t, then the numerical solution should also be negative. If the negative solution does not make sense, then the system of equation needs to be examined to see when and why negative solutions arise. Perhaps, I am just making this up - there needs to be a "dampening function" that slows down the trajectory as it approaches zero from its initial value. It is also possible that only certain regions of the parameter space (note that initial conditions are also parameters) are allowed in the sense that only there the solution is feasible for all t. So, in your example, the parameters might not be realistic. In short, if you are sure that the numerical solution is accurate, then you need to go back to your system of equations and analyze them carefully. Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jeremy Goldhaber-Fiebert Sent: Monday, June 11, 2007 11:47 AM To: Spencer Graves Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Fwd: Using odesolve to produce non-negative solutions Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick and also changing the relative and absolute tolerance inputs but I still get the problem for certain combinations of parameters and initial conditions. It occurs both under MS Windows XP Service Pack 2 and on a Linux cluster so I am pretty sure it is not platform specific. My real question to the group is if there is not a work around in lsoda are there other ode solvers in R that will allow the constraint of solutions to the ODEs remain non-negative? Best regards, Jeremy
Spencer Graves <spencer.graves at pdf.com> 6/8/2007 9:51 AM >>>
On the 'lsoda' help page, I did not see any option to force some
or all parameters to be nonnegative.
Have you considered replacing the parameters that must be
nonnegative with their logarithms? This effective moves the 0 lower
limit to (-Inf) and seems to have worked well for me in the past.
Often, it can even make the log likelihood or sum of squares surface
more elliptical, which means that the standard normal approximation for
the sampling distribution of parameter estimates will likely be more
accurate.
Hope this helps.
Spencer Graves
p.s. Your example seems not to be self contained. If I could have
easily copied it from your email and run it myself, I might have been
able to offer more useful suggestions.
Jeremy Goldhaber-Fiebert wrote:
Hello, I am using odesolve to simulate a group of people moving through time and
transmitting infections to one another.
In Matlab, there is a NonNegative option which tells the Matlab solver to
keep the vector elements of the ODE solution non-negative at all times. What is the right way to do this in R?
Thanks, Jeremy P.S., Below is a simplified version of the code I use to try to do this,
but I am not sure that it is theoretically right
dynmodel <- function(t,y,p)
{
## Initialize parameter values
birth <- p$mybirth(t)
death <- p$mydeath(t)
recover <- p$myrecover
beta <- p$mybeta
vaxeff <- p$myvaxeff
vaccinated <- p$myvax(t)
vax <- vaxeff*vaccinated/100
## If the state currently has negative quantities (shouldn't have), then
reset to reasonable values for computing meaningful derivatives
for (i in 1:length(y)) {
if (y[i]<0) {
y[i] <- 0
}
}
S <- y[1]
I <- y[2]
R <- y[3]
N <- y[4]
shat <- (birth*(1-vax)) - (death*S) - (beta*S*I/N)
ihat <- (beta*S*I/N) - (death*I) - (recover*I)
rhat <- (birth*(vax)) + (recover*I) - (death*R)
## Do we overshoot into negative space, if so shrink derivative to bring
state to 0
## then rescale the components that take the derivative negative
if (shat+S<0) {
shat_old <- shat
shat <- -1*S
scaled_transmission <- (shat/shat_old)*(beta*S*I/N)
ihat <- scaled_transmission - (death*I) - (recover*I)
}
if (ihat+I<0) {
ihat_old <- ihat
ihat <- -1*I
scaled_recovery <- (ihat/ihat_old)*(recover*I)
rhat <- scaled_recovery +(birth*(vax)) - (death*R)
}
if (rhat+R<0) {
rhat <- -1*R
}
nhat <- shat + ihat + rhat
if (nhat+N<0) {
nhat <- -1*N
}
## return derivatives
list(c(shat,ihat,rhat,nhat),c(0))
}
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi, all.
lsoda can certainly not handle complex parameters. You can try (as Hank
suggested) limiting hmax. You can also crank up relative and absolute
precision by specifying smaller values of rtol and atol. I've seen
similar problems in which the state variable becomes negative, with very
small absolute value, when theoretically, the system has a non-negative
solution. This is certainly due to imprecision in the numerical
solution. Have you tried including an analytic jacobian? That could
improve the numeric properties of the solution.
Woody
R. Woodrow Setzer, Ph. D.
National Center for Computational Toxicology
US Environmental Protection Agency
Mail Drop B205-01/US EPA/RTP, NC 27711
Ph: (919) 541-0128 Fax: (919) 541-1194
"Martin Henry H.
Stevens"
<HStevens at muohio To
.edu> Spencer Graves
<spencer.graves at pdf.com>
06/11/2007 01:02 cc
PM Jeremy Goldhaber-Fiebert
<JGOLDHAB at hsph.harvard.edu>,
R-Help
<r-help at stat.math.ethz.ch>,
Woodrow Setzer/RTP/USEPA/US at EPA
Subject
Re: [R] Fwd: Using odesolve to
produce non-negative solutions
Hi Spencer,
I have copied Woody Setzer. I have no idea whether lsoda can estimate
parameters that could take imaginary values.
Hank
On Jun 11, 2007, at 12:52 PM, Spencer Graves wrote:
<in line> Martin Henry H. Stevens wrote:
Hi Jeremy, First, setting hmax to a small number could prevent a large step, if you think that is a problem. Second, however, I don't see how you can get a negative population size when using the log trick.
SG: Can lsoda estimate complex or imaginary parameters?
Hmm. I have no idea.
I would think that that would prevent completely any negative values of N (i.e. e^-100000 > 0). Can you explain? or do you want to a void that trick? The only other solver I know of is rk4 and it is not recommended. Hank On Jun 11, 2007, at 11:46 AM, Jeremy Goldhaber-Fiebert wrote:
Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick
<snip>
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Dr. Hank Stevens, Assistant Professor 338 Pearson Hall Botany Department Miami University Oxford, OH 45056 Office: (513) 529-4206 Lab: (513) 529-4262 FAX: (513) 529-4243 http://www.cas.muohio.edu/~stevenmh/ http://www.muohio.edu/ecology/ http://www.muohio.edu/botany/ "E Pluribus Unum"
By the way, if someone could forward the original question to me (I'm
subscribed to but not currently receiving R-help, as I found I was
spending too much time reading it!) I might think of something more
useful. (alternatively, when was it posted; I can find it on gmane,
too).
Woody
R. Woodrow Setzer, Ph. D.
National Center for Computational Toxicology
US Environmental Protection Agency
Mail Drop B205-01/US EPA/RTP, NC 27711
Ph: (919) 541-0128 Fax: (919) 541-1194
"Martin Henry H.
Stevens"
<HStevens at muohio To
.edu> Spencer Graves
<spencer.graves at pdf.com>
06/11/2007 01:02 cc
PM Jeremy Goldhaber-Fiebert
<JGOLDHAB at hsph.harvard.edu>,
R-Help
<r-help at stat.math.ethz.ch>,
Woodrow Setzer/RTP/USEPA/US at EPA
Subject
Re: [R] Fwd: Using odesolve to
produce non-negative solutions
Hi Spencer,
I have copied Woody Setzer. I have no idea whether lsoda can estimate
parameters that could take imaginary values.
Hank
On Jun 11, 2007, at 12:52 PM, Spencer Graves wrote:
<in line> Martin Henry H. Stevens wrote:
Hi Jeremy, First, setting hmax to a small number could prevent a large step, if you think that is a problem. Second, however, I don't see how you can get a negative population size when using the log trick.
SG: Can lsoda estimate complex or imaginary parameters?
Hmm. I have no idea.
I would think that that would prevent completely any negative values of N (i.e. e^-100000 > 0). Can you explain? or do you want to a void that trick? The only other solver I know of is rk4 and it is not recommended. Hank On Jun 11, 2007, at 11:46 AM, Jeremy Goldhaber-Fiebert wrote:
Hi Spencer, Thank you for your response. I also did not see anything on the lsoda help page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick
<snip>
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Dr. Hank Stevens, Assistant Professor 338 Pearson Hall Botany Department Miami University Oxford, OH 45056 Office: (513) 529-4206 Lab: (513) 529-4262 FAX: (513) 529-4243 http://www.cas.muohio.edu/~stevenmh/ http://www.muohio.edu/ecology/ http://www.muohio.edu/botany/ "E Pluribus Unum"
Jeremy,
You should examine the steady-state solution to your system of equations, by
setting the time-derivatives to zero and then solving/analyzing the
resulting algebraic equations. This should give you some insights.
Let us say you have 3 groups, A,B, and C, with initial conditions:
N_A(t=0) = N_{A0}, N_B(t=0) = N_{B0}, and N_C(t=0) = N_{C0}, and that people
transition in and out of these 3 states (one of the states could even be
absorbing, e.g. death), but it is true for any time t that N_A(t) + N_B(t) +
N_C(t) = N_{A0} + N_{B0} + N_{C0}. Furthermore, you have 3 diff-equations
that describe the rate of change of N_A, N_B, and N_C, for t > 0. If it
happens that one of the N's, say N_A, becomes negative, you could set it
equal to zero. But then you have to figure out how to re-adjust N_B and N_C
so that they add up to the initial total count. After re-adjustment, you
also have to think about whether the resulting system of equations are
valid, when there are no A people.
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jeremy
Goldhaber-Fiebert
Sent: Monday, June 11, 2007 11:47 AM
To: Spencer Graves
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Fwd: Using odesolve to produce non-negative solutions
Hi Spencer,
Thank you for your response. I also did not see anything on the lsoda help
page which is the reason that I wrote to the list.
From your response, I am not sure if I asked my question clearly.
I am modeling a group of people (in a variety of health states) moving through time (and getting infected with an infectious disease). This means that the count of the number of people in each state should be positive at all times. What appears to happen is that lsoda asks for a derivative at a given point in time t and then adjusts the state of the population. However, perhaps due to numerical instability, it occasionally lower the population count below 0 for one of the health states (perhaps because it's step size is too big or something). I have tried both the logarithm trick and also changing the relative and absolute tolerance inputs but I still get the problem for certain combinations of parameters and initial conditions. It occurs both under MS Windows XP Service Pack 2 and on a Linux cluster so I am pretty sure it is not platform specific. My real question to the group is if there is not a work around in lsoda are there other ode solvers in R that will allow the constraint of solutions to the ODEs remain non-negative? Best regards, Jeremy
Spencer Graves <spencer.graves at pdf.com> 6/8/2007 9:51 AM >>>
On the 'lsoda' help page, I did not see any option to force some
or all parameters to be nonnegative.
Have you considered replacing the parameters that must be
nonnegative with their logarithms? This effective moves the 0 lower
limit to (-Inf) and seems to have worked well for me in the past.
Often, it can even make the log likelihood or sum of squares surface
more elliptical, which means that the standard normal approximation for
the sampling distribution of parameter estimates will likely be more
accurate.
Hope this helps.
Spencer Graves
p.s. Your example seems not to be self contained. If I could have
easily copied it from your email and run it myself, I might have been
able to offer more useful suggestions.
Jeremy Goldhaber-Fiebert wrote:
Hello, I am using odesolve to simulate a group of people moving through time and
transmitting infections to one another.
In Matlab, there is a NonNegative option which tells the Matlab solver to
keep the vector elements of the ODE solution non-negative at all times. What is the right way to do this in R?
Thanks, Jeremy P.S., Below is a simplified version of the code I use to try to do this,
but I am not sure that it is theoretically right
dynmodel <- function(t,y,p)
{
## Initialize parameter values
birth <- p$mybirth(t)
death <- p$mydeath(t)
recover <- p$myrecover
beta <- p$mybeta
vaxeff <- p$myvaxeff
vaccinated <- p$myvax(t)
vax <- vaxeff*vaccinated/100
## If the state currently has negative quantities (shouldn't have), then
reset to reasonable values for computing meaningful derivatives
for (i in 1:length(y)) {
if (y[i]<0) {
y[i] <- 0
}
}
S <- y[1]
I <- y[2]
R <- y[3]
N <- y[4]
shat <- (birth*(1-vax)) - (death*S) - (beta*S*I/N)
ihat <- (beta*S*I/N) - (death*I) - (recover*I)
rhat <- (birth*(vax)) + (recover*I) - (death*R)
## Do we overshoot into negative space, if so shrink derivative to bring
state to 0
## then rescale the components that take the derivative negative
if (shat+S<0) {
shat_old <- shat
shat <- -1*S
scaled_transmission <- (shat/shat_old)*(beta*S*I/N)
ihat <- scaled_transmission - (death*I) - (recover*I)
}
if (ihat+I<0) {
ihat_old <- ihat
ihat <- -1*I
scaled_recovery <- (ihat/ihat_old)*(recover*I)
rhat <- scaled_recovery +(birth*(vax)) - (death*R)
}
if (rhat+R<0) {
rhat <- -1*R
}
nhat <- shat + ihat + rhat
if (nhat+N<0) {
nhat <- -1*N
}
## return derivatives
list(c(shat,ihat,rhat,nhat),c(0))
}
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
2 days later
Dear Jeremy, a few notes about your model: The equations of your derivatives are designed in a way that can lead to negative state variables with certain parameter combinations. In order to avoid this, you are using "if constructions" which are intended to correct this. This method is however (as far as I have learned from theory and own experience ;-) a bad idea and should be strictly avoided. This trick may violate assumptions of the ODE solvers and in many cases also mass-balances (or similar). Instead of this, processes should be modeled in a way that avoids "crossing zero", e.g. exponential decay (x, k > 0): dx = - k * x (1) and not a linear decay like dx = -k (2) which by its nature can lead to negative state values. Case (1) can be managed almost perfectly by lsoda with his automatic internal time step algorithm. hmax is intended for non-autonomous models to ensure that external signals are not skipped by the automatism, which may be appropriate in your case because p seems to contain time dependent functions. As far as I can see, your equations belong to type (1) and should not need any of the if and for constructs, as long as your parameters (which are not given in your post) do have the correct sign and range (for example, vax <= 1, death >= 0 etc.). If you perform optimization, you must ensure that parameters stay in the plausible range is met using transformations of the parameters or constraints in the optimization procedure. Thomas PS: another question, what is the purpose of the state variable N? I guess it can be derived from the other states.
Jeremy Goldhaber-Fiebert wrote:
Hello, I am using odesolve to simulate a group of people moving through time and transmitting infections to one another. In Matlab, there is a NonNegative option which tells the Matlab solver to keep the vector elements of the ODE solution non-negative at all times. What is the right way to do this in R? Thanks, Jeremy
[... code deleted, see original post ...]