Skip to content

named vectors in Julia function #23

@fkrauer

Description

@fkrauer

Hi

I just started using diffeqr to fit ODE models in R. Is there a way to have named vectors in the function? Indexing works:

library(diffeqr)
theta <-  c(beta=.25, gamma=1/5, mu=1/(70*365), sigma=1/365)
maxtime <- 3650.0
init <- c(100000,0,0)

func_julia <- function(init, param, times) {
  beta <- param[1]
  gamma <- param[2]
  mu <- param[3]
  sigma <- param[4]
  S = state[1]
  I = state[2]
  R = state[3] 
  N = S + I + R
  dSdt = -beta*I*S/N + mu*N - mu*S + sigma*R
  dIdt = beta*I*S/N*S - gamma*I - mu*I 
  dRdt = gamma*I - mu*R - sigma*R
  return(c(dSdt, dIdt, dRdt))
}

enviro <- diffeqr::diffeq_setup()
problem <- enviro$ODEProblem(func_julia, init, c(0.0, maxtime), theta)
solution <- enviro$solve(problem , enviro$AutoVern7(enviro$KenCarp4()), 
                        saveat=1.0, abstol=1e-8, reltol=1e-6)

but using named vectors in R throws an error:

func_julia <- function(init, param, times) {
  beta <- param[["beta"]]
  gamma <- param[["gamma]]
  mu <- param[["mu"]]
  sigma <- param[["sigma"]]
  S = state[1]
  I = state[2]
  R = state[3] 
  N = S + I + R
  dSdt = -beta*I*S/N + mu*N - mu*S + sigma*R
  dIdt = beta*I*S/N*S - gamma*I - mu*I 
  dRdt = gamma*I - mu*R - sigma*R
  return(c(dSdt, dIdt, dRdt))
}
Error in param[["beta"]] : subscript out of bounds
Error: Error happens in Julia.

Named vectors would be great especially for inference and if there are dozens of parameters in the model. Thanks
Fabienne

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions