Skip to content
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

blog/2024-05-08-user-functions/ #35

Open
utterances-bot opened this issue Aug 9, 2024 · 6 comments
Open

blog/2024-05-08-user-functions/ #35

utterances-bot opened this issue Aug 9, 2024 · 6 comments

Comments

@utterances-bot
Copy link

nlmixr2/rxode2 user functions | nlmixr2

nlmixr: open source nonlinear mixed-effects modeling for R.

https://blog.nlmixr2.org/blog/2024-05-08-user-functions/

Copy link

zebulon2 commented Aug 9, 2024

Thank you for implementing this feature, very useful. One thing though: when using a model in a text file, and loading it using rxode2(filename='my_model.txt') is it possible to declare the user function in the same file than the model itself? Or to import the code defining the r user function from another file? Thanks for your answer.

Copy link
Contributor

I'm pretty sure that you cannot put the model into the text file. The text file ('my_model.txt' in your example) has to be the rxode2 model, itself.

The function would need to be defined in the R environment before loading, something like:

my_function <- function(x, y) x*y
mod <- rxode2(filename='my_model.txt')

Copy link

zebulon2 commented Aug 9, 2024

Thank you. Indeed this is what I suspected. Would be nice if rxode2() could accept not only a model textfile, but also supplementary code, since it now supports user-defined functions, which need to be defined outside of the model.

Copy link
Contributor

A workaround can be to create a .R file that does the following:

my_function <- function(x, y) x/y

writeLines("
C2 <- my_function(centr, V2)

# time-derivative assignment
d/dt(centr) <- F*KA*depot - CL*C2 - Q*C2 + Q*C3;
", "modelFile.rxode2")
mod <- rxode2(filename='modelFile.rxode2')
unlink("modelFile.rxode2")

Then, you can source() the .R file and have a similar effect.

Copy link
Member

mattfidler commented Aug 9, 2024

In C, yes this is available:

If you write your user code in C, and in a separate file, you can specify this too.

In the documention you have

extraC
Extra c code to include in the model. This can be useful to specify functions in the model. These C functions should usually take double precision arguments, and return double precision values.

However it doesn't allow a single file for a model.

In R this isn't available

I have added a feature request here:

nlmixr2/rxode2#761

Since rxode2 supports nlmixr2 style functions as well, another possibility is adding user functions in the user function definition. Probably more thought about the best way to acheive this would be good.

Copy link

Many thanks all for the answers and suggestions, and for opening a feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants