# [R] How to change parameter values as a function to time with the package "deSolve"

Marine Regis marine.regis at hotmail.fr
Fri Mar 24 15:28:48 CET 2017

```Thanks very much David for your answer. Sorry ! here is the code without the error:

library(deSolve)
param <- c(a = 0.1, b = 1)
yini <- c(alpha0 = 6, beta0 = 2)

mod <- function(times, yini, param) {

with(as.list(c(yini, param)), {

gamma0 <- ifelse(times %in% seq(0,10,1), 5, 0)

## print(gamma0)

dalpha0 <- - a*alpha0 + gamma0
dbeta0 <- a*alpha0 - b*beta0
return(list(c(dalpha0, dbeta0)))

})}

times <- seq(from = 0, to = 10, by = 1/24)
out <- ode(func = mod, times = times, y = yini, parms = param)
plot(out, lwd = 2, xlab = "day")

Marine

________________________________
De : David Winsemius <dwinsemius at comcast.net>
Envoyé : vendredi 24 mars 2017 06:01
À : Marine Regis
Cc : r-help at r-project.org
Objet : Re: [R] How to change parameter values as a function to time with the package "deSolve"

> On Mar 23, 2017, at 2:59 PM, Marine Regis <marine.regis at hotmail.fr> wrote:
>
> Hello,
>
> I am trying to solve an ODE in R using deSolve. With the following code, I expected the parameter �gamma0� takes the values 5 at time step 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10, and 0 otherwise. However, the print(gamma0) shows that �gamma0� stays at 0.
>
> Here is my ODE:
>
> param <- c(a = 0.1, b = 1)
> yini <- c(alpha0 = 0, beta0 = 0)
>
> mod <- function(times, yini, param) {
>
>  with(as.list(c(yini, parameters)), {
>
>  gamma0 <- ifelse(times %in% seq(0,10,1), 5, 0)
>
>  # print(gamma0)
>
>  dalpha0 <- - a*alpha0 + gamma0
>  dbeta0 <- a*alpha0 - b*beta0
>  return(list(c(dalpha0, dbeta0)))
>
>  })}
>
> times <- seq(from = 0, to = 10, by = 1/24)
> out <- ode(func = mod, times = times, y = yini, parms = param)
> plot(out, lwd = 2, xlab = "day")
>
>
> What am I doing wrong? Thanks in advance for your help!

When I execute that code I get:

(The mistake seems fairly obvious when you look are the parameter list for your `mod` function.)

Additionally: It's not generally good practice to use `with` inside functions, but that's not the problem here.

--
David.

> Marine
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
R-help Info Page - Homepage - SfS – Seminar for Statistics<https://stat.ethz.ch/mailman/listinfo/r-help>
stat.ethz.ch
The main R mailing list, for announcements about the development of R and the availability of new code, questions and answers about problems and solutions using R ...