pmxNODE is a package to facilitate the application of neural ordinary differential equations and the integration of neural networks in pharmacometric modeling software, including Monolix, NONMEM, and nlmixr2. It allows to utilize NN functions that are not commonly available in said pharmacometric software. The way pmxNODE works is that it translates NN functions into explicit equations that describe the calculations within a neural network, described in the publication Low-dimensional Neural ODEs accounting for inter-individual variability implemented in Monolix and NONMEM by Bräm, Steiert, Steffens, Pfister, and Koch (doi:…). It further allows to automatically initialize the neural network parameters, following the initialization approach presented in the publication above.
The general workflow for NONMEM with pmxNODE consists of few steps:
Write a NONMEM model file, as you would do normaly. However, you can utilize NN functions in your model code, e.g., for complex or unknown model parts.
Convert the written model file with the nn_converter_nm function from the pmxNODE
package.
We suggest to fit the model to the data first without
inter-individual variability on neural networks parameters (argument
pop = True in the nn_converter_nm function) and add the random
effects in a second run, where parameters were initialized with last
estimates.
We first need to load the pmxNODE package. Since we don’t rely on other R-packages (except ggplot for plotting), nothing more needs to be done.
Some examples are available in the pmxNODE package. To see all
example files, you can use the get_example_list function. To copy an
example to a folder of your choice, you can use the copy_example function. After calling the
copy_examples function, two files
should be in the target folder, a data file and a NONMEM model file.
get_example_list()
copy_examples(
target_folder = "~/pmxNODE",
example_nr = 1,
example_software = "NONMEM"
)Let’s have a look at the model file:
Before fitting the model, it needs to be converted with the nn_converter_nm function. In addition to the
path/file name of the unconverted NONMEM model, the argument on
including inter-individual variability for the neural network parameters
is required. The name of the converted NONMEM model file is
automatically generated based on the unconverted NONMEM model file. Note
that a suffix is added to the file name, either _converted_pop
if pop_only = TRUE or _converted_ind if pop_only =
FALSE.
In order to find the path to the NONMEM executable, you can utilize
the find_nmfe function.
Now the converted model file has included all the code needed for the NODE:
...
$DES
A1 = A(1)
hc_1 = 0
hc_2 = 0
hc_3 = 0
hc_4 = 0
hc_5 = 0
hc_1_thres = Wc_11 * A1 + bc_11
hc_2_thres = Wc_12 * A1 + bc_12
hc_3_thres = Wc_13 * A1 + bc_13
hc_4_thres = Wc_14 * A1 + bc_14
hc_5_thres = Wc_15 * A1 + bc_15
IF (hc_1_thres.GT.hc_1) hc_1 = hc_1_thres
IF (hc_2_thres.GT.hc_2) hc_2 = hc_2_thres
IF (hc_3_thres.GT.hc_3) hc_3 = hc_3_thres
IF (hc_4_thres.GT.hc_4) hc_4 = hc_4_thres
IF (hc_5_thres.GT.hc_5) hc_5 = hc_5_thres
NNc = Wc_21 * hc_1 + Wc_22 * hc_2 + Wc_23 * hc_3 + Wc_24 * hc_4 + Wc_25 * hc_5 + bc_21
ht_1 = 0
ht_2 = 0
ht_3 = 0
ht_4 = 0
ht_5 = 0
ht_1_thres = -(Wt_11**2) * T + bt_11
ht_2_thres = -(Wt_12**2) * T + bt_12
ht_3_thres = -(Wt_13**2) * T + bt_13
ht_4_thres = -(Wt_14**2) * T + bt_14
ht_5_thres = -(Wt_15**2) * T + bt_15
IF (ht_1_thres.GT.ht_1) ht_1 = ht_1_thres
IF (ht_2_thres.GT.ht_2) ht_2 = ht_2_thres
IF (ht_3_thres.GT.ht_3) ht_3 = ht_3_thres
IF (ht_4_thres.GT.ht_4) ht_4 = ht_4_thres
IF (ht_5_thres.GT.ht_5) ht_5 = ht_5_thres
NNt = Wt_21 * ht_1 + Wt_22 * ht_2 + Wt_23 * ht_3 + Wt_24 * ht_4 + Wt_25 * ht_5
DADT(1) = NNc + DOSE * NNt
... The model can be automatically run from R with the function run_nm from the pmxNODE package. If multiple
cores are available, you can run the model in parallel with the
parallel_command argument. If you would like to save the
results in a new folder, you can set create_dir = TRUE and give
the path and name of the data file.
In order to get the parameter estimations from the population fit
(without inter-individual variability), the pre_fixef_extractor_nm function can be
utilized.
These parameter estimates can be given as additional argument
pre_fixef to the nn_converter_nm function. To include
inter-individual variability, the population argument is set to false
(pop_only = FALSE) in the nn_converter_nm function.
The final model with inter-individual variability can then be fitted
again with the run_nm function.
est_parms <- pre_fixef_extractor_nm("~/pmxNODE/nm_example1_model_converted_pop/nm_example1_model_converted_pop.res")
nn_converter_nm(ctl_path = "~/pmxNODE/nm_example1_model.ctl",
pop_only = FALSE,
pre_fixef = est_parms)
run_nm(ctl_file = "~/pmxNODE/nm_example1_model_converted_ind.ctl",
nm_path = nmfe_path,
parralel_command = "-parafile=C:/nm75g64/run/mpiwini8.pnm [nodes]=30",
create_dir = TRUE,
data_file = "~/pmxNODE/data_example1_nm.csv")We can check now the predictions from the NODE model.
predictions <- read.table("~/pmxNODE/nm_example1_model_converted_ind/nm_example1.tab", header = T, skip = 1)
predictions <- predictions[predictions$AMT == 0,]predictions <- predictions[predictions$AMT == 0,]
ggplot(predictions) +
geom_point(aes(x = TIME, y = DV)) +
geom_line(aes(x = TIME, y = IPRED), color = "blue") +
geom_line(aes(x = TIME, y = PRED), color = "red") +
facet_wrap(~ID)Now if we want to investigate what the NNs in the NODE have learned, we can plot the derivatives versus the states. This visualizes the dynamics on ODE-level identified by the NODE. To functions are available for this, either the der_state_plot_nm or the rhs_plot_nm functions. The first one generates the derivative vs. state plot for a single NN. The name of the NN, the minimal and maximal input to the NN, and either the estimated parameters or directly the NONMEM results file must be given. Additionally, it needs to be specified if the NN is a time-dependent NN.
der_state_plot_nm("c", min_state = 0, max_state = 10, nm_res_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res", plot_type = "ggplot")
der_state_plot_nm("ct", min_state = 0, max_state = 24, nm_res_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res", time_nn = TRUE, plot_type = "ggplot")With the second one, the entire right-hand side of a differential equation can be plotted, e.g., the combination of multiple NNs or NNs combined with mechanistic parts. The right-hand side equation must be given as a string, the variable for the x-axis needs to be defined, and the inputs must be given as a dataframe with columns for each variable in the right-hand side equation. Additionally, a vector with information concerning time-dependency of the NNs in the right-hand side equation mus be provided or else all NNs are assumed to be non-time-depenedent. Note that for NNc inputs, the predictions need to be multiplied with the volume of distribution since the inputs to NNc in the model is amount and not concentration.
est_parms <- pre_fixef_extractor_nm("~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res")
rhs_inputs <- data.frame(id = predictions$ID,
NNc = predictions$PRED*as.numeric(est_parms["V"]),
NNt = predictions$TIME,
dose = 10)
rhs_plot_nm("NNc + dose * NNct", x_var = "NNc", inputs = rhs_inputs, nm_res_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res", time_nn = c(FALSE, TRUE))
rhs_plot_nm("NNc + dose * NNct", x_var = "NNct", inputs = rhs_inputs, nm_res_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res", time_nn = c(FALSE, TRUE))Similar plots can also be generated on individual level.
ind_der_state_plot_mlx("c", min_state = 0, max_state = 10,
nm_res_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res",
nm_phi_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.phi",
plot_type = "ggplot")
ind_der_state_plot_mlx("ct", min_state = 0, max_state = 24,
nm_res_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res",
nm_phi_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.phi",
time_nn = TRUE, plot_type = "ggplot")
ind_rhs_plot_mlx("NNc + dose * NNt", x_var = "NNc", group = "id", inputs = rhs_inputs,
nm_res_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res",
nm_phi_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.phi",
time_nn = c(FALSE, TRUE))
ind_rhs_plot_mlx("NNc + dose * NNt", x_var = "NNt", group = "id", inputs = rhs_inputs,
nm_res_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.res",
nm_phi_file = "~/pmxNODE/nm_example1_model_converted_ind/nm_example1_model_converted_ind.phi",
time_nn = c(FALSE, TRUE))