nonmem2R goodness of fit plot functions

Magnus Åstrand

January 2019

Introduction

Herein is a demo of functions for GOF plots in the nonmem2R R-package. The functions provide building blocks for doing GOF plots with flexibility for user specific formatting and or layout. There are also functions for combining single GOF’s into one and there are basic GOF’s combining a specific type of GOF’s into one single plot, e.g. the function basic.GOF4 which generated the example below.

## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'

The GOF functions build on ggplot2 and as far as possible return an ggplot-object that can be further modified by adding ggplot formatting. However, some functions use grid.arrange from the R-package gridExtra to combine different type of GOF’s. These functions plot the combined set of GOF’s but does not return an ggplot object, and hence the plots can therefore not be easily further modified/formatted. To still provide options to add ggplot formatting these functions have an argument global.ggplot.options which can be used to add extra ggplot functions on each individual GOF before the GOF’s are combined into one graph by grid.arrange.

Formatting of e.g. lines and plot symbols can also be controlled by the argument control and the function GOF.control or using set.GOF.params to affect all GOF’s.

Axis labels are set for most common NONMEM variables, e.g. CWRES is by default labelled “Cond. weighted res” and TIME is by default labelled “Time after first dose(h)”. See examples in section Labels and formatting options with further details on controling axis labels, changing the caption text and other formatting of the GOF plots.

All GOF functions will by default add a caption in the bottom of the graph to indicate date when figure was generated together with the full path to the script. However, this require that the script name has been set using set.script.name, and then the script name will be pasted together with the date and path as derived from getwd. If script name has not been set no caption will be added. Each GOF plot will have a red dashed reference line and blue solid lines are smoother’s representing the actual data.

All functions require a data.frame as input and thus NONMEM output tables, e.g, sdtab, cotab,.., should be loaded prior using the functions, and if using multiple output tables it’s recommenced to just cbind all of them to a single sdtab data.frame.

Functions

Tailored GOF functions

There are a set of specific and tailored GOF functions.

do.individual.GOF will provide GOF plots of PRED, IPRED and DV vs TIME (default) with one panel per subject This will generate one page/plot per 20 subjects (default) and therefore this function is best used when exporting to a PDF. The X axis is default set to TIME but e.g. time after last dose (TAPD) or any column in the input data.frame. Below is a small example from 6 subjects.

do.individual.GOF(subset(sdtab,DV>0 & ID<7 & TIME<50))

On top of basic.GOF4 with an example in the beginning of this document, there is also basic.GOF6 which adds 2 extra GOF plots showing GOF’s for residuals (CWRES) as histogram and as qqnorm plot. Except for number of bins in the histogram of CWRES both have the same options and parameters, e.g. the X-axis value for 1’st and 2’nd GOF on the bottom row is set by idv1 and idv2respectively. In the example below the defult (IPRED and TIME) is replaced by TAPD and PRED.

basic.GOF6(subset(sdtab,DV>0),idv1="TAPD",idv2="PRED")
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'

There are 4 tailored functions for doing GOF plots for ETA’s: basic.eta.GOF, eta.cov.GOF, eta.cat.GOF, and eta.pairs.GOF. These functions identifies all columns named ETA1,..,ETA9,ET10,.. in the input data.frame and provides different types of GOF plots for these columns. All 4 functions will by default exclude ETA’s which are constant i.e. when set as FIX in your nonmem model (use drop.fixed=FALSE to override) and each ETA is scaled to unit variance (use standardize=FALSE to override). All but the basic.eta.GOF function return an ggplot object and can be further modified as any ggplot object.

basic.eta.GOF will provide GOF plots showing histograms and densities combined with QQ-norm plots.

basic.eta.GOF(sdtab)

eta.cov.GOF will provide GOF plots showing x-y plots of ETA’s vs continuous covariates and eta.cat.GOF will provide box-plots of all ETA’s. Both function have an argument type default set to “all-in-one”. This will generate a ggplot which is formatted by facet_grid with all ETA’s in rows and all covariates in columns, as in the example below for eta.cov.GOF.

eta.cov.GOF(subset(sdtab,DV>0),covariates=c("AGE","BWT"))
## `geom_smooth()` using formula 'y ~ x'

However it’s also possible to get either one page for each ETA (type="eta-by-page") or as in the example below for eta.cat.GOF with one page for each covariate (type="covariate-by-page").

eta.cat.GOF(subset(sdtab,DV>0),covariates=c("gender","eGFR"),type="covariate-by-page")

The last tailored function for doing GOF plots of ETA’s is eta.pairs.GOF with an example below. Default setting will show scatter plots below the diagonal, smooth density on the diagonal, and spearman correlations above the diagonal. A 2D density can also be added on either side of the diagonal. The example below is show the effect of setting labels for ETA using set.GOF.params

set.GOF.params(eta.labels=c("Ka","Vc","CL","F"))
eta.pairs.GOF(sdtab,density2D="lower")

As mentioned some of the functions don’t return and ggplot object and the graph can thus not be further modified by adding existing ggplot functions. The functions do however have a named argument global.ggplot.options which is added to each individual ggplot functions before combining. This functionality is limited to one single additional ggplot function, e.g. facet_grid, facet_wrap or different theme’s such as theme_bw.

In the example below basic.GOF4 is modified be setting global.ggplot.options to facet_wrap(~gender) to get one panel for each gender for each of the four GOF plots.

basic.GOF4(subset(sdtab,DV>0),idv2="TAPD",global.ggplot.options=facet_wrap(~gender))
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'