Chapter 3nlmixr vignettes

Some significant applications are demonstrated in this chapter; the vignettes will be discussing the application in more depth.

3.1nlmixr domain specific language

library(nlmixr)
?nlmixr

Under the hood, nlmixr has five main modules:

dynmodel() and its mcmc cousin dynmodel.mcmc() for nonlinear dynamic models of individual data;

nlme_lin_cmpt()for one to three linear compartment models of population data with first order absorption, or i.v. bolus, or i.v. infusion using the nlme algorithm;

nlme_ode() for general dynamic models defined by ordinary differential equations (ODEs) of population data using the nlme algorithm;

saem_fit for general dynamic models defined by ordinary differential equations (ODEs) of population data by the Stochastic Approximation Expectation-Maximization (SAEM) algorithm;

gnlmm and gnlmm2 for generalized non-linear mixed-models (possibly defined by ordinary differential equations) of population data by the adaptive Gaussian quadrature algorithm.

3.1.1 Rationale

nlmixr estimation routines have their own way of specifying models. Often the models are specified in ways that are most intuitive for one estimation routine, but do not make sense for another estimation routine. Sometimes, legacy estimation routines like [nlme] have their own syntax that is outside of the control of the nlmixr package.

The unique syntax of each routine makes the routines themselves easier to maintain and expand, and allows interfacing with existing packages that are outside of nlmixr (like [nlme]). However, a model definition language that is common between estimation methods, and an output object that is uniform, will make it easier to switch between estimation routines and will facilitate interfacing output with external packages like xpose and other user-written packages.

The nlmixr mini-modeling language (Domain Specific Language) attempts to address this issue by incorporating a common language. This language is inspired by both R and NONMEM, since these languages are familiar to many pharmacometricians.

Initial Estimates and boundaries for population parameters

nlmixr models are contained in a R function with two blocks: ini and model. This R function can be named anything, but is not meant to be called directly from R. In fact, if you try you will likely get an error such as Error: could not find function "ini".

The ini model block

The ini model block is meant to hold the initial estimates for the model, and the boundaries of the parameters for estimation routines that support boundaries (note nlmixr’s saem and [nlme] do not currently support parameter boundaries).

To explain how these initial estimates are specified we will start with an annotated example:

f <-function(){ ## Note the arguments to the function are currently
## ignored by `nlmixr`
ini({
## Initial conditions for population parameters (sometimes
## called theta parameters) are defined by either `<-` or '='
lCl <-1.6#log Cl (L/hr)
## Note that simple expressions that evaluate to a number are
## OK for defining initial conditions (like in R)
lVc =log(90) #log V (L)
## Also a comment on a parameter is captured as a parameter label
lKa <-1#log Ka (1/hr)
## Bounds may be specified by c(lower, est, upper), like NONMEM:
## Residuals errors are assumed to be population parameters
prop.err <-c(0, 0.2, 1)
})
## The model block will be discussed later
model({})
}

As shown in the above examples:

Simple parameter values are specified as a R-compatible assignment

Boundaries may be specified by c(lower, est, upper).

Like NONMEM, c(lower,est) is equivalent to c(lower,est,Inf)

Also like NONMEM, c(est) does not specify a lower bound, and is equivalent to specifying the parameter without R’s c function.

The initial estimates of the between subject variabilities are specified on the variance scale, and in analogy with NONMEM, the square roots of the diagonal elements correspond to coefficients of variation when used in the exponential IIV implementation. This is true, since this is an approximation. When displaying %CV in the nlmixr table, it uses: sqrt(exp(v) - 1) * 100 where v=variance of the Omega formula.

These parameters can be called by almost any R-compatible name. Please note that:

Residual error estimates should be coded as population estimates (i.e. using an ‘=’ or ‘<-’ statement, not a ‘~’). These are specified on a standard deviation scale.

Variable names starting with “_” are not supported. Note that R does not allow variable starting with “_” to be assigned without quoting them.

Variable names starting with “rx_” or “nlmixr_” are not supported, since RxODE and nlmixr use these prefixes internally for certain estimation routines and for calculating residuals.

Variable names are case sensitive, just like they are in R. “CL” is not the same as “Cl”.

Initial estimates for between-subject error distributions

In mixture models, multivariate normal individual deviations from the population parameters are estimated (in NONMEM these are called ETA parameters). Additionally, the variance/covariance matrix of these deviations are also estimated (in NONMEM this is the OMEGA matrix). The initial estimates for variance are specified by the ~ operator in nlmixr, which is typically used in R to denote “modeled by”, and was chosen to distinguish these estimates from the population and residual error parameters.

Continuing the prior example, we can annotate the estimates for the between subject error distribution:

f <-function(){
ini({
lCl <-1.6#log Cl (L/hr)
lVc =log(90) #log V (L)
lKa <-1#log Ka (1/hr)
prop.err <-c(0, 0.2, 1)
## Initial estimate for ka IIV variance
## Labels work for single parameters
eta.ka ~0.1#BSV Ka
## For correlated parameters, you specify the names of each
## correlated parameter separated by a addition operator `+`
## and the left handed side specifies the lower triangular
## matrix initial of the covariance matrix.
eta.cl +eta.vc ~c(0.1,
0.005, 0.1)
## Note that labels do not currently work for correlated
## parameters. Also do not put comments inside the lower
## triangular matrix as this will currently break the model.
})
## The model block will be discussed later
model({})
}

As shown in the above examples:

Simple variances are specified by the variable name and the estimate separated by ~.

Correlated parameters are specified by the sum of the variable labels and then the lower triangular matrix of the covariance is specified on the left hand side of the equation. This is also separated by ~. }

Currently the model syntax does not allow comments inside the lower triangular matrix.

Model syntax for ODE-based models

The ini model block Once the initialization block has been defined, you can define a model block in terms of the defined variables in the ini block. You can also mix in RxODE blocks into the model. This section is analogous to NONMEM’s $PK, $PRED, $DES and $ERROR blocks.

The current method of defining an nlmixr model is to specify the parameters, and then possibly the RxODE lines:

Parameters are defined before the differential equations. Currently, defining the differential equations directly in terms of the population parameters is not supported.

The differential equations, parameters and error terms are in a single block, instead of multiple sections.

State names, calculated variables cannot start with either “rx_” or “nlmixr_” since these are reserved terms for internal use in some estimation routines.

Errors are specified using ~. Currently you can use either add(parameter) for additive error, prop(parameter) for proportional error or add(parameter1) + prop(parameter2) for combined additive and proportional error. You can also specify norm(parameter) for the additive error, since it follows a normal distribution.

Some algorithms, like saem, require parameters in terms of Pop.Parameter + Individual.Deviation.Parameter + Covariate*Covariate.Parameter. The order of these parameters do not matter. The approach is similar to NONMEM’s mu-referencing.

The type of parameter (population parameter estimate, or individual parameter deviation) in the model is determined by the initial block; Covariates used in the model are missing in the ini block. These variables need to be present in the modeling dataset for the model to run.

Model Syntax for solved PK systems

Solved PK systems are currently supported by nlmixr with the linCmt() pseudo-function. An annotated example of a solved system is below:

Currently the solved systems support either oral dosing, IV dosing or IV infusion dosing, but does not allow mixing the dosing types.

While RxODE allows mixing of solved systems and ODEs, this has not been implemented in nlmixr yet.

The solved systems implemented are the one-, two- and three-compartment models with or without first-order absorption. Each of the models support a lag time with a tlag parameter (although currently tlag will cause the CWRES/OBJF calculation to fail).

The linCmt() function figures out the model from the parameter names. nlmixr currently knows about numbered volumes, Vc/Vp, clearances in terms of both CL and Q/CLD. Additionally, nlmixr knows about elimination micro-constants (ie k12). Mixing of parameters and microconstants for is currently not supported.

Checking model syntax

After specifying the model syntax you can check that nlmixr is interpreting it correctly by using the nlmixr function on it.

Using the above function we can get:

>nlmixr(f)
## 1-compartment model with first-order absorption in terms of Cl
## Initialization:
################################################################################
Fixed Effects ($theta):lCl lVc lKA
1.600004.499810.10000Omega ($omega):[,1] [,2] [,3]
[1,] 0.10.00.0
[2,] 0.00.10.0
[3,] 0.00.00.1
## Model:
################################################################################
Cl <-exp(lCl +eta.Cl)
Vc <-exp(lVc +eta.Vc)
KA <-exp(lKA +eta.KA)
## Instead of specifying the ODEs, you can use
## the linCmt() function to use the solved system.
##
## This function determines the type of PK solved system
## to use by the parameters that are defined. In this case
## it knows that this is a one-compartment model with first-order
## absorption.
linCmt() ~prop(prop.err)

In general this gives you information about the model (what type of solved system/RxODE), initial estimates as well as the code for the model block.

Using the model syntax for estimating a model

Once the model function has been created, you can use it to estimate the parameters for a model given a dataset.

This dataset has to have RxODE compatible event IDs. Both Monolix and NONMEM use a different dataset description. You may convert these datasets to RxODE-compatible datasets with the nmDataConvert function. Note that steady state doses are not supported by RxODE, and therefore not supported by the conversion function.

As an example, we can use a simulated rich 1-compartment oral PK dataset.

d <-Oral_1CPT
d <-d[,names(d) != "SS"];
d <-nmDataConvert(d);

Once the data has been converted to the appropriate format, you can use the nlmixr function to run the appropriate code.

The options for saem are controlled by saemControl. You may wish to make sure the minimization is complete in the case of saem. You can do that with traceplot, which shows the iteration history with burn-in and EM phases. In this case, the burn-in seems reasonable; you may wish to increase the number of iterations in the EM phase of the estimation. Overall it is probably a semi-reasonable solution. (xpose.nlmixr’s prm_vs_iteration() can also do this.)

nlmixr output objects

In addition to unifying the modeling language sent to each of the estimation routines, the outputs currently have a unified structure.

This example shows the standard printout of an nlmixr fit object. The elements of the fit are:

The type of fit (nlme, saem, etc)

Metrics of goodness of fit (AIC, BIC, and logLik).

To align the comparison between methods, the FOCEi likelihood objective is calculated regardless of the method used and used for goodness of fit metrics.

This FOCEi likelihood has been compared to NONMEM’s objective function and gives the same values (based on the data in Wang 2007(2007))

Note that FOCEi is used to calculate the objective function for a saem fit.

Even though the objective functions are calculated in the same manner, caution should be used when comparing fits from different estimation routines.

The next item is the timing of each of the steps of the fit.

These can be accessed by (fit.s$time).

As a mnemonic, the access for this item is shown in the printout. This is true for almost all of the other items in the printout.

After the timing of the fit, the parameter estimates are displayed (can be accessed by fit.s$par.fixed

While the items are rounded for R printing, each estimate without rounding is still accessible by the $ syntax. For example, the $Untransformed gives the untransformed parameter values.

The Untransformed parameter takes log-space parameters and back-transforms them to normal parameters. Not the CIs are listed on the back-transformed parameter space.

Proportional Errors are converted to %CV on the untransformed space

Omega block (accessed by fit.s$omega)

A table of fit data is also supplied. Please note:

An nlmixr fit object is actually a data frame. Saving it as a Rdata object and then loading it without nlmixr will just show the data by itself. The additional fit information is still present, but nlmixr must be loaded in order to see it.

Special access to fit information (like the $omega) needs nlmixr to extract the information.

If you use the $ to access information, the order of precedence is:

Fit data from the overall data.frame

Information about the parsed nlmixr model (via $uif)

Parameter history if available (via $par.hist and $par.hist.stacked)

Fixed effects table (via $par.fixed)

Individual differences from the typical population parameters (via $eta)

Fit information from the list of information generated during the post-hoc residual calculation.

Fit information from the environment where the post-hoc residual were calculated

Fit information about how the data and options interacted with the specified model (such as estimation options or if the solved system is for an infusion or an IV bolus).

While the printout may display the data as a data.table object or tbl object, the data is NOT any of these objects, but rather a derived data frame. - Since the object **is*}** a data.frame, you can treat it like one.

In addition to the above properties of the fit object, there are a few additional that may be helpful for the modeler:

$theta gives the fixed effects parameter estimates (in NONMEM the thetas). This can also be accessed in nlme function. Note that the residual variability is treated as a fixed effect parameter and is included in this list.

$eta gives the random effects parameter estimates, or in NONMEM the etas. This can also be accessed in using the random.effects function.

3.1.2 Some examples

3.1.2.1 A two-compartment PK model

Now let’s have a look at some examples to demonstrate the syntax. Here’s a relatively straightforward two-compartmental PK model with covariate effects.

Objective function (OBJF), Akaike information criterion (AIC), Bayesian information criterion (BIC), the log-likelihood of the model given the data, and the condition number are provided at the top of the output block. This more, judging from the condition number (the ratio between the largest and smallest eigenvalues), may be over-parameterized. For this SAEM model, the OBJF has been calculated using the FOCEi method.

The next block provides parameter estimates and associated precisions, between subject variability (BSV) where appropriate, and exponentiated versions of the parameter estimates together with 95% confidence intervals (“back-transformed”). (nlmixr assumes that all parameters are log-transformed. Sometimes this is not the case, as illustrated here for wteff and sexeff.) Shrinkages are also supplied. The fit object has properties that can be interrogated:

3.3shinyMixR

3.3.1 Introduction

The nlmixr package can be accessed through command line in R and RStudio, and through an interface shinyMixR supported by the nlmixr team. Both command line and interface support the integration with xpose.nlmixr.

shinyMixR aims to provide a user-friendly tool for nlmixr based on Shiny, which facilitates a workflow around an nlmixr project. The shinydashboard package was used to set up a structure for controlling and tracking runs with an nlmixr project, and was the basis for setting up the modular interface.

This project tool enhances the usability and attractiveness of nlmixr, facilitating dynamic and interactive use in real-time for rapid model development. In this project-oriented structure, the command line and dashboard can be used independently and/or interdependently. This means that many of the functions within the package can also be used outside the interface within an interactive R session.

Using shinyMixR, the user specifies and controls an nlmixr project workflow entirely in R. Within a project folder, a structure can be created to include separate folders for models, data and runs. Functionality is available to edit and execute a model code, summarize and compare model outputs in a tabular fashion, and view and compare model development using a tree paradigm. Inputs, outputs and metadata are stored in relation to the model code within the project structure (a discrete R object) to ensure traceability.

Results are visualized using modifications of existing packages (such as xpose.nlmixr, user-written functions and packages, or pre-existing plotting functionality included in the shinyMixR package. Results are reported using the R3port package in pdf and html format.

How the package can be used and what the most important functions are, is described in this section. When working with this package, there are two important assumptions:

A specific folder structure is in place. The package uses this structure to read and write certain files (the folder structure can be generated automatically using the create_proj function).

Within a folder structure, multiple models can be present and is considered a “project”. The package creates and manages a project object which is available in the global environment.

An introduction on how to use the shinyMixR interface can be found here. To get started, first install the package and use the library.

3.3.2 Getting started

To get started, first install the package using:

devtools::install_github("richardhooijmaijers/shinyMixR")

Be aware that the nlmixr, nlmixr.xpose and R3port packages should be installed before installing shinyMixR, e.g.:

The easiest way to get to know the package is to create the folder structure (which includes some example models):

library(shinyMixR, quietly =TRUE)
create_proj()

By default, a folder structure is created within the current directory. The following folders are created:

analysis: in this folder all plots and tables are saved in a structured way to make them accesible to the interface

data: data files used by the models in R data format (.rds) or Excel format (.csv).

models: models, available as separate R scripts according to the nlmixr unified-user-interface R model functions.

scripts: generic analysis scipts made available in the interface

? shinyMixR: folder used by the interface to store temporarly files and results files

The interface monitors what happens in these folders. This is important to know because new models and/or data can be copied into these folders or files can be removed. This will then be recognized within the app (after refreshing the overview). This way it is possible to work on data and models separately and plug them in to shinyMixR at a later stage.

Once there is a folder structure present the interface can be started:

run_shinymixr(launch.browser =TRUE)

The interface will be started and a project object will be created in the global environment in which all information is kept/managed. If correct, the interface will open in the default browser and the following will be seen:

Figure 3.1: shinyMixR interface

On the left side, there is a sidebar with various menu items. The content of the main body will open with the model overview but will change based on the selected menu item in the sidebar. The sidebar can be collapsed by clicking the three lines in the top bar, providing more room for the main body.

3.3.3 Overview

The overview page can be used to see which models are present in a project, the relationship between models and to adapt model meta data. The overview can be exported to a CSV file and a selection of columns can be made that should be displayed (all using the DT package).

3.3.4 Edit models

Figure 3.2: Editing model code

The edit tab can be used to edit existing models within an editor including syntax coloring (using shinyAce). It is also possible to create new models using various templates or to duplicate existing models.

3.3.5 Run models

Figure 3.3: Executing model code

The run tab can be used to execute models within a project. It is possible to run one or multiple models at once. Also it is possible to assess the intermediate output or progress of an nlmixr run.

3.3.6 Parameter estimates

Figure 3.4: View parameter estimates

The parameter estimates tab can be used to generate a table with parameter estimates. In case multiple models are selected the table will show the results of each run in a separate column. This page is reactive which means that in case a different model is selected, the table is directly updated. There is the possiblity to save the table to a latex/pdf or html file.

3.3.7 Goodness-of-fit

Figure 3.5: Creating goodness-of-fit plots

The goodness-of-fit tab can be used to generate a combination of 4 goodness-of-fit plots combined. By default nlmixr.xpose is used but direct ggplot2 can also be used by specifying this in the settings. Plots can be saved to a LaTeX/pdf or html file.

3.3.8 Fit plots

Figure 3.6: Creating individual fit plots

The fit plots tab can be used to generate individual fit plots. The same options are present here as for the goodness-of-fit plots.

3.3.9 Scripts

Figure 3.7: Run user defined scripts

It is possible to write your own scripts that can be used to analyse model results. This script can be used to process the result for one or multiple models at once (the interface will include the name of the selected models in the script). An example of how such a script will look like is included in the package.

3.3.10 Results

Figure 3.8: View and combine modeling results

It is possible to view and combine the results from the models within a project within the last tab.

3.3.11 Interactive session

When working in an interactive R session, many functions used by the interface are available from an interactive R session. When working outside the interface it is important to know how to interact with the project object. This object should be created as one of the first steps because other function rely on the availability of this object:

proj_obj <-get_proj()

This function will look in the folder structure to create or update the available information. The result is a list that is build-up as follows:

In case this object is not present it will be created by looking at the files present in the current folder structure. In case the object is already present it will check if newer files are present in the current folder and will update the object with this information. Therefore one has to be aware that this function should be submitted manually in case new information is present in one of the folders.

Once a folder structure is in place and the project object is created, an overview can be created for the available models and when models are executed and running high level results can be viewd. It is also possible to display a collapsible d3 tree view of the models. This is mainly useful in case reference to models is provided to show the hierachy of the models within a project:

overview()
tree_overview()

Although the nlmixr package has the possibilty to run nlmixr models, the shinyMixR package has a function available named run_nmx. This function allows to run the model in an external R session when executing a run through shinyMixR. This is necessary within the interface to overcome the application from freezing when a model is submitted. Also within an interactive R session it is convenient because you do not have to wait for a run to finish. An example on how this can be used is given below:

#proj_obj <- get_proj()#run_nmx("run1",proj=proj_obj)

run_nmx("run1",proj=proj_obj)
# progress of a run is written to external text file# this can be read-in for intermediate assessmentreadLines("shinyMixR/temp/run1.prog.txt")

The current version of the package has three functions for assessing the model results: par_table, gof_plot and fit_plot. The first function is to create a simple parameter table par_table. By default this function returns a data.frame. In case multiple models are selected, each column will have the results of the selected model. The results can also be written to a PDF (using LaTeX) or html file using the R3port package:

For assessing the goodness of fit, the gof_plot function can be used. This function will by default use the nlmixr.xpose package to create 4 different types of plots. It is also possible to directly create ggplot2 types of plots. By default the plots will be created within the R session but can also be written to pdf/html using the R3port package:

The last plot is an individual fit plot fit_plot. This function will by default use the nlmixr.xpose package to create a plot per individual including the observed data, indivual and population predictions. Also here it is possible to create ggplot2 types of plots and plots can be outputted to pdf/html: