-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
named vectors in Julia function #23
Comments
@Non-Contradiction how do named vectors convert in JuliaCall? |
Currently, named vectors will have their names dropped by JuliaCall, and I'm not sure whether Julia has any data structure (similar) corresponding to named vectors in R? I also wonders whether it is possible to use named list instead of named vectors in this example. For example, the following seems to work for me and have the same result with the unnamed version:
Notice the usage of |
Thanks. It works for me as well if I solve the problem as is. But if I "accelerate" the problem with diffeqr::jitoptimize_ode() it throws me an error:
Is there a way around this? This would be a huge advantage when fitting complex models. |
|
@fkrauer I have some intuition for what causes the error: the key "beta" of the list in R (will be dict in Julia) somehow gets iterated and be decomposed into 'b'+'e'+'t'+'a', and diffeqr looks for elements with keys 'b', 'e', 't', and 'a' in the list, which does not exist... so the error occurs. The behavior makes sense, because by numerical index, like |
I implemented an "uggly" version using
|
Running: library(diffeqr)
enviro <- diffeqr::diffeq_setup()
theta <- c(beta=.25, gamma=1/5, mu=1/(70*365), sigma=1/365)
maxtime <- 3650.0
init <- c(100000,0,0)
func_julia_LabelledArrays <- function(init, param, times) {
beta <- julia_call("getindex", param, quote(beta)) ## param[["beta"]]
gamma <- julia_call("getindex", param, quote(gamma)) ## param[["gamma"]]
mu <- julia_call("getindex", param, quote(mu)) ## param[["mu"]]
sigma <- julia_call("getindex", param, quote(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))
}
library(JuliaCall)
julia_command("using LabelledArrays")
autowrap("LabelledArrays.LArray")
problem_LabelledArrays <- enviro$ODEProblem(func_julia_LabelledArrays, init, c(0.0, maxtime),
julia_eval("LVector(beta=.25, gamma=1/5, mu=1/(70*365), sigma=1/365)"))
solution_LabelledArrays <- enviro$solve(problem_LabelledArrays, enviro$AutoVern7(enviro$KenCarp4()),
saveat=1.0, abstol=1e-8, reltol=1e-6)
diffeqr::jitoptimize_ode(enviro,problem_LabelledArrays) fails with Error: Error happens in Julia.
ArgumentError: invalid index: :beta of type Symbol
Stacktrace:
[1] to_index(::Symbol) at .\indices.jl:297
[2] to_index(::Array{Operation,1}, ::Symbol) at .\indices.jl:274
[3] to_indices at .\indices.jl:325 [inlined]
[4] to_indices at .\indices.jl:322 [inlined]
[5] getindex(::Array{Operation,1}, ::Symbol) at .\abstractarray.jl:1060
[6] docall(::Ptr{Nothing}) at D:\OneDrive\Computer\Documents\R\win-library\4.0\JuliaCall\julia\setup.jl:168
[7] (::RCall.var"#32#33"{LangSxp,Ptr{LangSxp},Ptr{EnvSxp},Base.RefValue{Int32}})() at C:\Users\accou\.julia\packages\RCall\Qzssx\src\eval.jl:84
[8] disable_sigint at .\c.jl:446 [inlined]
[9] tryEval at C:\Users\accou\.julia\packages\RCall\Qzssx\src\eval.jl:81 [inlined]
[10] reval_p(::Ptr{LangSxp}, ::Ptr{EnvSxp}) at C:\Users\accou\.julia\packages\RCall\Qzssx\src\eval.jl:96
[11] reval_p at C:\Users\accou\.julia\packages\RCall\Qzssx\src\eval.jl:95 [inlined]
[12] rcall_p(::RObject{ClosSxp}, ::Array{Operation,1}, : The error seems to be because |
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:
but using named vectors in R throws an error:
Named vectors would be great especially for inference and if there are dozens of parameters in the model. Thanks
Fabienne
The text was updated successfully, but these errors were encountered: