Guided-demo: Exploring 'ddmore' R functionality with the warfarin model

UseCase1 : ODE model for warfarin population pharmacokinetics

This example script is intended to illustrate how to use the 'ddmore' R package to perform a M&S workflow using the DDMoRe Standalone Execution Environment (SEE).

The following steps are implemented in this workflow:

To run a task, select with the cursor any code lines you wish to execute and press CTRL+R+R in your keyboard. An HTML file containing the commands in this file and associated output will be provided to allow the user to compare the results

Initialisation

Clear workspace and set working directory under 'UsesCasesDemo' project

rm(list=ls(all=FALSE))
mydir <- file.path(Sys.getenv("MDLIDE_WORKSPACE_HOME"),"UseCasesDemo")
setwd(mydir)

Set name of .mdl file and dataset for future tasks

uc <- "UseCase1"
datafile <- "warfarin_conc.csv"
mdlfile <- paste0(uc,".mdl")

Create a new folder to be used as working directory and where results will be stored

wd <- file.path(mydir,uc)
dir.create(wd)
## Warning in dir.create(wd):
## 'C:\SEE\MDL_IDE\workspace\UseCasesDemo\UseCase1' already exists

Copy the dataset and the .mdl file available under “models” into the working directory

file.copy(file.path(mydir,"models", datafile),wd)
## [1] TRUE
file.copy(file.path(mydir,"models",mdlfile),wd)
## [1] TRUE

Set the working directory.

setwd(file.path(mydir,uc))

The working directory needs to be set differently when knitr R package is used to spin the file

library(knitr)
opts_knit$set(root.dir = file.path(mydir,uc)) 

List files available in working directory

list.files()
##  [1] "Bootstrap"              "GOF_MLX.pdf"           
##  [3] "GOF_NM.pdf"             "GOF_NM_FOCEI.pdf"      
##  [5] "Monolix"                "NONMEM"                
##  [7] "NONMEM_FOCEI"           "UseCase1.mdl"          
##  [9] "UseCase1.xml"           "UseCase1_Bootstrap.pdf"
## [11] "UseCase1_EGA.pdf"       "UseCase1_FOCEI.mdl"    
## [13] "UseCase1_tr.txt"        "UseCase1_VPC.mdl"      
## [15] "UseCase1_VPC.pdf"       "VPC"                   
## [17] "warfarin_conc.csv"

Introduction to 'ddmore' R package

View objects within the .mdl file

Use 'ddmore' function getMDLObjects() to retrieve model object(s) from an existing .mdl file. This function reads the MDL in an .mdl file and parses the MDL code for each MDL Object into an R list of objects of appropriate types with names corresponding to the MDL Object names given in the file.

myMDLObj <- getMDLObjects(mdlfile)
length(myMDLObj)
## [1] 4
names(myMDLObj)
## [1] "warfarin_PK_ODE_dat"  "warfarin_PK_ODE_par"  "warfarin_PK_ODE_mdl" 
## [4] "warfarin_PK_ODE_task"
str(myMDLObj)
## List of 4
##  $ warfarin_PK_ODE_dat :Formal class 'dataObj' [package "ddmore"] with 5 slots
##   .. ..@ SOURCE                :List of 1
##   .. .. ..$ srcfile:List of 2
##   .. .. .. ..$ file       : chr "warfarin_conc.csv"
##   .. .. .. ..$ inputFormat: chr "nonmemFormat"
##   .. ..@ DECLARED_VARIABLES    :List of 2
##   .. .. ..$ :List of 3
##   .. .. .. ..$ .subtype: chr "EquationDef"
##   .. .. .. ..$ name    : chr "GUT"
##   .. .. .. ..$ typeSpec: chr "dosingTarget"
##   .. .. ..$ :List of 3
##   .. .. .. ..$ .subtype: chr "EquationDef"
##   .. .. .. ..$ name    : chr "Y"
##   .. .. .. ..$ typeSpec: chr "observation"
##   .. ..@ DATA_INPUT_VARIABLES  :List of 8
##   .. .. ..$ ID    :List of 1
##   .. .. .. ..$ use: chr "id"
##   .. .. ..$ TIME  :List of 1
##   .. .. .. ..$ use: chr "idv"
##   .. .. ..$ WT    :List of 1
##   .. .. .. ..$ use: chr "covariate"
##   .. .. ..$ AMT   :List of 2
##   .. .. .. ..$ use     : chr "amt"
##   .. .. .. ..$ variable: chr "GUT"
##   .. .. ..$ DVID  :List of 1
##   .. .. .. ..$ use: chr "dvid"
##   .. .. ..$ DV    :List of 2
##   .. .. .. ..$ use     : chr "dv"
##   .. .. .. ..$ variable: chr "Y"
##   .. .. ..$ MDV   :List of 1
##   .. .. .. ..$ use: chr "mdv"
##   .. .. ..$ logtWT:List of 1
##   .. .. .. ..$ use: chr "covariate"
##   .. ..@ DATA_DERIVED_VARIABLES: list()
##   .. ..@ name                  : chr "warfarin_PK_ODE_dat"
##  $ warfarin_PK_ODE_par :Formal class 'parObj' [package "ddmore"] with 4 slots
##   .. ..@ DECLARED_VARIABLES: list()
##   .. ..@ STRUCTURAL        :List of 6
##   .. .. ..$ POP_CL    :List of 2
##   .. .. .. ..$ value: chr "0.1"
##   .. .. .. ..$ lo   : chr "0.001"
##   .. .. ..$ POP_V     :List of 2
##   .. .. .. ..$ value: chr "8"
##   .. .. .. ..$ lo   : chr "0.001"
##   .. .. ..$ POP_KA    :List of 2
##   .. .. .. ..$ value: chr "0.362"
##   .. .. .. ..$ lo   : chr "0.001"
##   .. .. ..$ POP_TLAG  :List of 2
##   .. .. .. ..$ value: chr "1"
##   .. .. .. ..$ lo   : chr "0.001"
##   .. .. ..$ BETA_CL_WT:List of 2
##   .. .. .. ..$ value: chr "0.75"
##   .. .. .. ..$ fix  : chr "true"
##   .. .. ..$ BETA_V_WT :List of 2
##   .. .. .. ..$ value: chr "1"
##   .. .. .. ..$ fix  : chr "true"
##   .. ..@ VARIABILITY       :List of 7
##   .. .. ..$ PPV_CL   :List of 1
##   .. .. .. ..$ value: chr "0.1"
##   .. .. ..$ PPV_V    :List of 1
##   .. .. .. ..$ value: chr "0.1"
##   .. .. ..$ PPV_KA   :List of 1
##   .. .. .. ..$ value: chr "0.1"
##   .. .. ..$ PPV_TLAG :List of 2
##   .. .. .. ..$ value: chr "0.1"
##   .. .. .. ..$ fix  : chr "true"
##   .. .. ..$ CORR_CL_V:List of 1
##   .. .. .. ..$ value: chr "0.01"
##   .. .. ..$ RUV_PROP :List of 2
##   .. .. .. ..$ value: chr "0.1"
##   .. .. .. ..$ lo   : chr "0"
##   .. .. ..$ RUV_ADD  :List of 2
##   .. .. .. ..$ value: chr "0.1"
##   .. .. .. ..$ lo   : chr "1.0E-4"
##   .. ..@ name              : chr "warfarin_PK_ODE_par"
##  $ warfarin_PK_ODE_mdl :Formal class 'mdlObj' [package "ddmore"] with 11 slots
##   .. ..@ IDV                       : chr "T"
##   .. ..@ COVARIATES                :List of 1
##   .. .. ..$ :List of 2
##   .. .. .. ..$ .subtype: chr "EquationDef"
##   .. .. .. ..$ name    : chr "logtWT"
##   .. ..@ VARIABILITY_LEVELS        :List of 2
##   .. .. ..$ :List of 1
##   .. .. .. ..$ ID:List of 2
##   .. .. .. .. ..$ level: chr "2"
##   .. .. .. .. ..$ type : chr "parameter"
##   .. .. ..$ :List of 1
##   .. .. .. ..$ DV:List of 2
##   .. .. .. .. ..$ level: chr "1"
##   .. .. .. .. ..$ type : chr "observation"
##   .. ..@ STRUCTURAL_PARAMETERS     : chr [1:6] "POP_CL" "POP_V" "POP_KA" "POP_TLAG" ...
##   .. ..@ VARIABILITY_PARAMETERS    : chr [1:7] "PPV_CL" "PPV_V" "CORR_CL_V" "PPV_KA" ...
##   .. ..@ RANDOM_VARIABLE_DEFINITION:List of 6
##   .. .. ..$ :List of 4
##   .. .. .. ..$ .subtype: chr "RandVarDefn"
##   .. .. .. ..$ blkAttrs:List of 1
##   .. .. .. .. ..$ level: chr "DV"
##   .. .. .. ..$ name    : chr "EPS_Y"
##   .. .. .. ..$ distn   : chr "Normal(mean=0, var=1)"
##   .. .. ..$ :List of 4
##   .. .. .. ..$ .subtype: chr "RandVarDefn"
##   .. .. .. ..$ blkAttrs:List of 1
##   .. .. .. .. ..$ level: chr "ID"
##   .. .. .. ..$ name    : chr "ETA_CL"
##   .. .. .. ..$ distn   : chr "Normal(mean=0, sd=PPV_CL)"
##   .. .. ..$ :List of 4
##   .. .. .. ..$ .subtype: chr "RandVarDefn"
##   .. .. .. ..$ blkAttrs:List of 1
##   .. .. .. .. ..$ level: chr "ID"
##   .. .. .. ..$ name    : chr "ETA_V"
##   .. .. .. ..$ distn   : chr "Normal(mean=0, sd=PPV_V)"
##   .. .. ..$ :List of 4
##   .. .. .. ..$ .subtype: chr "RandVarDefn"
##   .. .. .. ..$ blkAttrs:List of 1
##   .. .. .. .. ..$ level: chr "ID"
##   .. .. .. ..$ name    : chr "ETA_KA"
##   .. .. .. ..$ distn   : chr "Normal(mean=0, sd=PPV_KA)"
##   .. .. ..$ :List of 2
##   .. .. .. ..$         :List of 4
##   .. .. .. .. ..$ type : chr "correlation"
##   .. .. .. .. ..$ rv1  : chr "ETA_CL"
##   .. .. .. .. ..$ rv2  : chr "ETA_V"
##   .. .. .. .. ..$ value: chr "CORR_CL_V"
##   .. .. .. ..$ blkAttrs:List of 1
##   .. .. .. .. ..$ level: chr "ID"
##   .. .. ..$ :List of 4
##   .. .. .. ..$ .subtype: chr "RandVarDefn"
##   .. .. .. ..$ blkAttrs:List of 1
##   .. .. .. .. ..$ level: chr "ID"
##   .. .. .. ..$ name    : chr "ETA_TLAG"
##   .. .. .. ..$ distn   : chr "Normal(mean=0, sd=PPV_TLAG)"
##   .. ..@ INDIVIDUAL_VARIABLES      :List of 4
##   .. .. ..$ :List of 1
##   .. .. .. ..$ CL:List of 5
##   .. .. .. .. ..$ type  : chr "linear"
##   .. .. .. .. ..$ trans : chr "ln"
##   .. .. .. .. ..$ pop   : chr "POP_CL"
##   .. .. .. .. ..$ fixEff:List of 1
##   .. .. .. .. .. ..$ :List of 2
##   .. .. .. .. .. .. ..$ coeff: chr "BETA_CL_WT"
##   .. .. .. .. .. .. ..$ cov  : chr "logtWT"
##   .. .. .. .. ..$ ranEff: chr "ETA_CL"
##   .. .. ..$ :List of 1
##   .. .. .. ..$ V:List of 5
##   .. .. .. .. ..$ type  : chr "linear"
##   .. .. .. .. ..$ trans : chr "ln"
##   .. .. .. .. ..$ pop   : chr "POP_V"
##   .. .. .. .. ..$ fixEff:List of 1
##   .. .. .. .. .. ..$ :List of 2
##   .. .. .. .. .. .. ..$ coeff: chr "BETA_V_WT"
##   .. .. .. .. .. .. ..$ cov  : chr "logtWT"
##   .. .. .. .. ..$ ranEff: chr "ETA_V"
##   .. .. ..$ :List of 1
##   .. .. .. ..$ KA:List of 4
##   .. .. .. .. ..$ type  : chr "linear"
##   .. .. .. .. ..$ trans : chr "ln"
##   .. .. .. .. ..$ pop   : chr "POP_KA"
##   .. .. .. .. ..$ ranEff: chr "ETA_KA"
##   .. .. ..$ :List of 1
##   .. .. .. ..$ TLAG:List of 4
##   .. .. .. .. ..$ type  : chr "linear"
##   .. .. .. .. ..$ trans : chr "ln"
##   .. .. .. .. ..$ pop   : chr "POP_TLAG"
##   .. .. .. .. ..$ ranEff: chr "ETA_TLAG"
##   .. ..@ MODEL_PREDICTION          :List of 2
##   .. .. ..$ :List of 2
##   .. .. .. ..$ DEQ     :List of 3
##   .. .. .. .. ..$ :List of 3
##   .. .. .. .. .. ..$ .subtype: chr "EquationDef"
##   .. .. .. .. .. ..$ name    : chr "RATEIN"
##   .. .. .. .. .. ..$ expr    : chr "if (T>=TLAG) then GUT*KA else 0"
##   .. .. .. .. ..$ :List of 1
##   .. .. .. .. .. ..$ GUT:List of 3
##   .. .. .. .. .. .. ..$ deriv: chr "(-RATEIN)"
##   .. .. .. .. .. .. ..$ init : chr "0"
##   .. .. .. .. .. .. ..$ x0   : chr "0"
##   .. .. .. .. ..$ :List of 1
##   .. .. .. .. .. ..$ CENTRAL:List of 3
##   .. .. .. .. .. .. ..$ deriv: chr "(RATEIN-CL*CENTRAL/V)"
##   .. .. .. .. .. .. ..$ init : chr "0"
##   .. .. .. .. .. .. ..$ x0   : chr "0"
##   .. .. .. ..$ .subtype: chr "BlockStmt"
##   .. .. ..$ :List of 3
##   .. .. .. ..$ .subtype: chr "EquationDef"
##   .. .. .. ..$ name    : chr "CC"
##   .. .. .. ..$ expr    : chr "CENTRAL/V"
##   .. ..@ OBSERVATION               :List of 1
##   .. .. ..$ :List of 1
##   .. .. .. ..$ Y:List of 5
##   .. .. .. .. ..$ type        : chr "combinedError1"
##   .. .. .. .. ..$ additive    : chr "RUV_ADD"
##   .. .. .. .. ..$ proportional: chr "RUV_PROP"
##   .. .. .. .. ..$ eps         : chr "EPS_Y"
##   .. .. .. .. ..$ prediction  : chr "CC"
##   .. ..@ GROUP_VARIABLES           : list()
##   .. ..@ name                      : chr "warfarin_PK_ODE_mdl"
##  $ warfarin_PK_ODE_task:Formal class 'taskObj' [package "ddmore"] with 5 slots
##   .. ..@ ESTIMATE:List of 2
##   .. .. ..$ algo  : chr "saem"
##   .. .. ..$ blocks: list()
##   .. ..@ SIMULATE: NULL
##   .. ..@ EVALUATE: NULL
##   .. ..@ OPTIMISE: NULL
##   .. ..@ name    : chr "warfarin_PK_ODE_task"

Use 'ddmore' function getDataObjects() to retrieve only data object(s) from an existing .mdl file. This function returns a list of Parameter Object(s) from which we select the first element Hover over the variable name to see its structure

myDataObj <- getDataObjects(mdlfile)[[1]]

Use 'ddmore' function getParameterObjects() to retrieve only parameter object(s) from an existing .mdl file

myParObj <- getParameterObjects(mdlfile)[[1]]

Use 'ddmore' function getModelObjects() to retrieve only model object(s) from an existing .mdl file.

myModObj <- getModelObjects(mdlfile)[[1]]

Use 'ddmore' function getTaskPropertiesObjects() to retrieve only task properties object(s) from an existing .mdl file

myTaskObj <- getTaskPropertiesObjects(mdlfile)[[1]]

Exploratory Data Analysis

Recall that getDataObjects only reads the MDL code from the .mdl file. Use 'ddmore' function readDataObj() to create an R object from the MDL data object.

myData <- readDataObj(myDataObj)

Let's look at the first 6 lines of the data set

head(myData)
##   ID TIME   WT AMT DVID  DV MDV      logtWT
## 1  1  0.0 66.7 100    0  NA   1 -0.04829029
## 2  1  0.5 66.7  NA    1 0.0   0 -0.04829029
## 3  1  1.0 66.7  NA    1 1.9   0 -0.04829029
## 4  1  2.0 66.7  NA    1 3.3   0 -0.04829029
## 5  1  3.0 66.7  NA    1 6.6   0 -0.04829029
## 6  1  6.0 66.7  NA    1 9.1   0 -0.04829029

Extract only observation records

myEDAData<-myData[myData$MDV==0,]

Open an R window to record and access all your plots

windows(record=TRUE) 

Plot the data using xyplot from the lattice library

plot1 <- xyplot(DV~TIME,groups=ID,data=myEDAData,type="b",ylab="Conc. (mg/L)",xlab="Time (h)")
print(plot1)

plot of chunk unnamed-chunk-14

plot2 <- xyplot(DV~TIME|ID,data=myEDAData,type="b",layout=c(3,4),ylab="Conc. (mg/L)",xlab="Time (h)",scales=list(relation="free"))
print(plot2)

plot of chunk unnamed-chunk-14 plot of chunk unnamed-chunk-14 plot of chunk unnamed-chunk-14

Export the results in a pdf file

pdf(paste0(uc,"_EGA.pdf"))
 print(plot1)
 print(plot2)
dev.off()
## png 
##   2

Model Development

ESTIMATE model parameters using Monolix

The ddmore “estimate” function translates the contents of the .mdl file to a target language and then estimates parameters using the target software. After estimation, the output is converted to a Standardised Output object which is saved in a .SO.xml file.

Translated files and Monolix output will be returned in the ./Monolix subfolder. The Standardised Output object (.SO.xml) is read and parsed into an R object called “mlx” of (S4) class “StandardOutputObject”.

mlx <- estimate(mdlfile, target="MONOLIX", subfolder="Monolix")
## -- Mon Aug 15 14:26:38 2016
## New
## Submitted
## Job 6c624368-0348-4904-ad5c-c4f1e6a90bf8 progress:
## Running [ ...... ]
## Importing Results
## Copying the result data back to the local machine for job ID 6c624368-0348-4904-ad5c-c4f1e6a90bf8...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job35642e07595c to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase1/Monolix
## Done.
## 
## 
## The following main elements were parsed successfully:
##   ToolSettings
##   RawResults
##   Estimation::PopulationEstimates::MLE
##   Estimation::PrecisionPopulationEstimates::MLE
##   Estimation::IndividualEstimates::Estimates
##   Estimation::IndividualEstimates::RandomEffects
##   Estimation::Residuals::ResidualTable
##   Estimation::Predictions
##   Estimation::OFMeasures::IndividualContribToLL
##   Estimation::OFMeasures::InformationCriteria
##   Estimation::OFMeasures::LogLikelihood
## 
## Completed
## -- Mon Aug 15 14:28:40 2016
slotNames(mlx)
## [1] "ToolSettings"     "RawResults"       "TaskInformation" 
## [4] "Estimation"       "ModelDiagnostic"  "Simulation"      
## [7] "OptimalDesign"    ".pathToSourceXML"

The ddmore “LoadSOObj” reads and parsed existing Standardise Output Objects mlx <- LoadSOObject(“Monolix/UseCase1.SO.xml”) The ddmore function “getPopulationParameters” extracts the Population Parameter values from an R object of (S4) class “StandardOutputObject” and returns the estimates. See documentation for getPopulationParameters to see other arguments and settings for this function.

parameters_mlx <- getPopulationParameters(mlx, what="estimates")$MLE
parameters_mlx
##      POP_V  BETA_V_WT     POP_KA     POP_CL BETA_CL_WT   POP_TLAG 
##    8.08213    1.00000    1.91540    0.13404    0.75000    0.97620 
##      PPV_V     PPV_KA     PPV_CL   PPV_TLAG  CORR_CL_V    RUV_ADD 
##    0.13068    1.17050    0.26539    0.10000    0.20615    0.22301 
##   RUV_PROP 
##    0.06579
getPopulationParameters(mlx, what="precisions")
## $MLE
##     Parameter     MLE      SE    RSE
## 1  BETA_CL_WT 0.75000 0.00000   0.00
## 2   BETA_V_WT 1.00000 0.00000   0.00
## 3   CORR_CL_V 0.20615 0.20897 101.37
## 4      POP_CL 0.13404 0.00652   4.86
## 5      POP_KA 1.91540 0.64466  33.66
## 6    POP_TLAG 0.97620 0.02119   2.17
## 7       POP_V 8.08213 0.22096   2.73
## 8      PPV_CL 0.26539 0.03555  13.39
## 9      PPV_KA 1.17050 0.25543  21.82
## 10   PPV_TLAG 0.10000 0.00000   0.00
## 11      PPV_V 0.13068 0.02251  17.23
## 12    RUV_ADD 0.22301 0.04731  21.21
## 13   RUV_PROP 0.06579 0.00947  14.40
getEstimationInfo(mlx)
## $OFMeasures
## $OFMeasures$LogLikelihood
## $OFMeasures$LogLikelihood[[1]]
## [1] -369.305
## 
## 
## $OFMeasures$IndividualContribToLL
##    Subject ICtoLL
## 1        1 -24.74
## 2        2  -5.28
## 3        3 -15.02
## 4        4 -14.06
## 5        5 -14.62
## 6        6  -7.56
## 7        7 -19.51
## 8        8 -22.20
## 9        9 -31.28
## 10      10  -5.77
## 11      12 -19.88
## 12      13 -19.23
## 13      14 -19.13
## 14      15 -11.29
## 15      16 -16.60
## 16      17  -7.29
## 17      18  -5.43
## 18      19  -8.10
## 19      20  -6.69
## 20      21  -7.69
## 21      22  -7.93
## 22      23  -8.07
## 23      24  -6.98
## 24      25  -7.27
## 25      26  -9.76
## 26      27  -5.44
## 27      28  -9.14
## 28      29  -5.68
## 29      30  -8.34
## 30      31  -7.73
## 31      32  -4.76
## 32      33  -6.82
## 
## $OFMeasures$InformationCriteria
## $OFMeasures$InformationCriteria$AIC
## [1] 758.61
## 
## $OFMeasures$InformationCriteria$BIC
## [1] 773.27
## 
## 
## 
## $Messages
## list()

Perform model diagnostics for the base model using Xpose functions

Use 'ddmore' function as.xpdb() to create an Xpose database object from the Standardised Output object, regardless of target software used for estimation.

mlx.xpdb<-as.xpdb(mlx,datafile)
## 
## Removed dose rows in rawData slot of SO to enable merge with Predictions data.

We can then call Xpose functions referencing this mlx.xpdb object as the input. Perform some basic goodness of fit

print(basic.gof(mlx.xpdb))

plot of chunk unnamed-chunk-17

print(ind.plots(mlx.xpdb))

plot of chunk unnamed-chunk-17 plot of chunk unnamed-chunk-17

Export graphs to PDF file

pdf("GOF_MLX.pdf")
 print(basic.gof(mlx.xpdb))
 print(ind.plots(mlx.xpdb))
dev.off()
## png 
##   2

SAEM Estimation with NONMEM

By default, a covariance step is not run when estimating in NONMEM. To see how it can be requested, see UseCase1_1.mdl

NM <- estimate(mdlfile, target="NONMEM", subfolder="NONMEM")
## -- Mon Aug 15 15:51:33 2016
## New
## Submitted
## Job a2d41579-6e9f-4160-b1a2-d96f5d4e12e2 progress:
## Running [ ............................. ]
## Importing Results
## Copying the result data back to the local machine for job ID a2d41579-6e9f-4160-b1a2-d96f5d4e12e2...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job35646d455ffe to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase1/NONMEM
## Done.
## 
## 
## The following main elements were parsed successfully:
##   RawResults
##   Estimation::PopulationEstimates::MLE
##   Estimation::IndividualEstimates::Estimates
##   Estimation::IndividualEstimates::RandomEffects
##   Estimation::Residuals::ResidualTable
##   Estimation::Predictions
##   Estimation::OFMeasures::Deviance
## 
## The following MESSAGEs were raised during the job execution:
##  estimation_successful: 1
##  covariance_step_run: 0
##  rounding_errors: 0
##  estimate_near_boundary: 0
##  s_matrix_singular: 0
##  nmoutput2so_version: This SOBlock was created with nmoutput2so version 4.5.27
## 
## Completed
## -- Mon Aug 15 16:01:18 2016

Load previous results NM <- LoadSOObject(“NONMEM/UseCase1.SO.xml”)

Results from NONMEM should be comparable with results from MONOLIX

parameters_nm <- getPopulationParameters(NM, what="estimates")
print(parameters_nm)
## $MLE
##      POP_CL       POP_V      POP_KA    POP_TLAG    RUV_PROP     RUV_ADD 
## 0.132862000 8.163970000 1.755850000 0.950650000 0.107732000 0.000100001 
##  BETA_CL_WT   BETA_V_WT      PPV_CL   CORR_CL_V       PPV_V      PPV_KA 
## 0.750000000 1.000000000 0.262838000 0.275564000 0.138113000 0.943956000 
##    PPV_TLAG 
## 0.100000000
print(parameters_mlx)
##      POP_V  BETA_V_WT     POP_KA     POP_CL BETA_CL_WT   POP_TLAG 
##    8.08213    1.00000    1.91540    0.13404    0.75000    0.97620 
##      PPV_V     PPV_KA     PPV_CL   PPV_TLAG  CORR_CL_V    RUV_ADD 
##    0.13068    1.17050    0.26539    0.10000    0.20615    0.22301 
##   RUV_PROP 
##    0.06579
print(getEstimationInfo(NM))
## $OFMeasures
## $OFMeasures$Deviance
## $OFMeasures$Deviance[[1]]
## [1] -310.8532
## 
## 
## 
## $Messages
## $Messages$Info
## $Messages$Info$estimation_successful
## [1] "1"
## 
## $Messages$Info$covariance_step_run
## [1] "0"
## 
## $Messages$Info$rounding_errors
## [1] "0"
## 
## $Messages$Info$estimate_near_boundary
## [1] "0"
## 
## $Messages$Info$s_matrix_singular
## [1] "0"
## 
## $Messages$Info$nmoutput2so_version
## [1] "This SOBlock was created with nmoutput2so version 4.5.27"

Xpose diagnostics using NONMEM output

nm.xpdb<-as.xpdb(NM,datafile)
## 
## Removed dose rows in rawData+Predictions slot of SO to enable merge with Residuals data.
## 
## Residuals data does not currently contain dose rows in output from Nonmem executions.

Perform some basic goofness of fit

print(basic.gof(nm.xpdb))

plot of chunk unnamed-chunk-20

print(ind.plots(nm.xpdb))

plot of chunk unnamed-chunk-20 plot of chunk unnamed-chunk-20

print(parm.hist(nm.xpdb))

plot of chunk unnamed-chunk-20

Export plots to a pdf file

pdf("GOF_NM.pdf")
 print(basic.gof(nm.xpdb))
 print(ind.plots(nm.xpdb))
 print(parm.hist(nm.xpdb))
dev.off()
## png 
##   2

Change estimation method to FOCEI (for speed)

MDL Objects can be manipulated from R to change for example the estimation algorithm

myTaskProperties <- getTaskPropertiesObjects(mdlfile)[[1]]
myNewTaskProperties <- myTaskProperties
myNewTaskProperties@ESTIMATE$algo <- "focei"

Assembling the new Modelling Object Group (MOG). Note that we reuse the data, parameters and model from the MOG.

myNewerMOG <- createMogObj(dataObj = getDataObjects(mdlfile)[[1]], 
        parObj = getParameterObjects(mdlfile)[[1]], 
        mdlObj = getModelObjects(mdlfile)[[1]], 
        taskObj = myNewTaskProperties)

We can then write the MOG back out to an .mdl file.

mdlfile.FOCEI <- paste0(uc,"_FOCEI.mdl")
writeMogObj(myNewerMOG,mdlfile.FOCEI)

Test estimation using this new MOG in NONMEM via PsN

By default, a covariance step is not run when estimating in PsN. To see how it can be requested, see UseCase1_1.mdl

NM.FOCEI <- estimate(mdlfile.FOCEI, target="PsN", subfolder="NONMEM_FOCEI")
## -- Mon Aug 15 16:01:33 2016
## New
## Submitted
## Job 1b928cc4-f793-4b5b-820c-3abf378e6f94 progress:
## Running [ ...... ]
## Importing Results
## Copying the result data back to the local machine for job ID 1b928cc4-f793-4b5b-820c-3abf378e6f94...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job356437e2464b to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase1/NONMEM_FOCEI
## Done.
## 
## 
## The following main elements were parsed successfully:
##   RawResults
##   Estimation::PopulationEstimates::MLE
##   Estimation::IndividualEstimates::Estimates
##   Estimation::IndividualEstimates::RandomEffects
##   Estimation::Residuals::ResidualTable
##   Estimation::Predictions
##   Estimation::OFMeasures::Deviance
## 
## The following MESSAGEs were raised during the job execution:
##  estimation_successful: 1
##  covariance_step_run: 0
##  rounding_errors: 0
##  hessian_reset: 0
##  zero_gradients: 0
##  final_zero_gradients: 0
##  estimate_near_boundary: 0
##  s_matrix_singular: 0
##  significant_digits: 3.2
##  nmoutput2so_version: This SOBlock was created with nmoutput2so version 4.5.27
## 
## Completed
## -- Mon Aug 15 16:03:36 2016

Load previous results NM.FOCEI <- LoadSOObject(“NONMEM_FOCEI/UseCase1_FOCEI.SO.xml”) Results from NONMEM should be comparable to previous results

getPopulationParameters(NM.FOCEI,  what="estimates")
## $MLE
##     POP_CL      POP_V     POP_KA   POP_TLAG   RUV_PROP    RUV_ADD 
##  0.1346600  8.0468500  1.2944300  0.8629310  0.0628024  0.3147830 
## BETA_CL_WT  BETA_V_WT     PPV_CL  CORR_CL_V      PPV_V     PPV_KA 
##  0.7500000  1.0000000  0.2635890  0.2128860  0.1366690  0.9175980 
##   PPV_TLAG 
##  0.1000000
parameters_nm
## $MLE
##      POP_CL       POP_V      POP_KA    POP_TLAG    RUV_PROP     RUV_ADD 
## 0.132862000 8.163970000 1.755850000 0.950650000 0.107732000 0.000100001 
##  BETA_CL_WT   BETA_V_WT      PPV_CL   CORR_CL_V       PPV_V      PPV_KA 
## 0.750000000 1.000000000 0.262838000 0.275564000 0.138113000 0.943956000 
##    PPV_TLAG 
## 0.100000000

Xpose diagnostics using NONMEM output

nmfocei.xpdb<-as.xpdb(NM.FOCEI,datafile)
## 
## Removed dose rows in rawData+Predictions slot of SO to enable merge with Residuals data.
## 
## Residuals data does not currently contain dose rows in output from Nonmem executions.

Basic diagnostics for NONMEM fit.

print(basic.gof(nmfocei.xpdb))

plot of chunk unnamed-chunk-25

Export graph to a pdf

pdf("GOF_NM_FOCEI.pdf")
 print(basic.gof(nmfocei.xpdb))
dev.off()
## png 
##   2

Run the Bootstrap using PsN

The ddmore “bootstrap.PsN” function is a wrap up function that calls Bootstrap PsN functionality using as input an MDL file that will be translated to NMTRAN as first step. Additional PsN arguments can be specified under the “bootstrapOptions” attribute. After task execution, the output from PsN is converted to a Standardised Output object which is saved in a .SO.xml file. Translated files and PsN output will be returned in the ./Bootstrap subfolder

bootstrapResults <- bootstrap.PsN(mdlfile.FOCEI, samples=20, seed=123456,
        bootstrapOptions=" -no-skip_minimization_terminated -threads=2",
        subfolder="Bootstrap", plot=TRUE)
## -- Mon Aug 15 16:03:39 2016
## New
## Submitted
## Job 6b50dc81-b512-4384-9614-3b678761952e progress:
## Running [ ......................................................................... ]
## Importing Results
## Copying the result data back to the local machine for job ID 6b50dc81-b512-4384-9614-3b678761952e...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job3564439f3a39 to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase1/Bootstrap
## Done.
## 
## 
## The following main elements were parsed successfully:
##   RawResults
##   Estimation::PopulationEstimates::MLE
##   Estimation::PopulationEstimates::OtherMethodBootstrap
##   Estimation::PrecisionPopulationEstimates::OtherMethodBootstrap
##   Estimation::IndividualEstimates::Estimates
##   Estimation::IndividualEstimates::RandomEffects
##   Estimation::Residuals::ResidualTable
##   Estimation::Predictions
##   Estimation::OFMeasures::Deviance
## 
## The following WARNINGs were raised during the job execution:
##  bootstrap_parameter_scale: The parameters PPV_CL, CORR_CL_V, PPV_V, PPV_KA and PPV_TLAG were requested on the sd/corr scale but are given on the var/cov scale in all bootstrap results.
## 
## The following MESSAGEs were raised during the job execution:
##  estimation_successful: 1
##  covariance_step_run: 0
##  rounding_errors: 0
##  hessian_reset: 0
##  zero_gradients: 0
##  final_zero_gradients: 0
##  estimate_near_boundary: 0
##  s_matrix_singular: 0
##  significant_digits: 3.2
##  nmoutput2so_version: This SOBlock was created with nmoutput2so version 4.5.27
## 
## Completed
## -- Mon Aug 15 16:28:04 2016
## Warning: NAs introduced by coercion
## [[1]]

plot of chunk Bootstrap

## 
## [[2]]
## NULL
## 
## [[3]]

plot of chunk Bootstrap

## 
## [[4]]
## NULL
## 
## [[5]]
## NULL

Load results from a bootstrap previously performed bootstrapResults <- LoadSOObject(“Bootstrap/UseCase1_FOCEI.SO.xml”) Export bootstrap histograms to a pdf

pdf(paste0(uc,"_Bootstrap.pdf"))
 print(boot.hist(results.file = file.path("Bootstrap",paste0("raw_results_",uc,"_FOCEI.csv")),
                incl.ids.file = file.path("Bootstrap","included_individuals1.csv")))
## [[1]]
## 
## [[2]]
## NULL
## 
## [[3]]
## 
## [[4]]
## NULL
## 
## [[5]]
## NULL
dev.off()
## png 
##   2

Extract parameter estimates and precision from bootstrap results.

print(getPopulationParameters(bootstrapResults, what="estimates"))
## $MLE
##     POP_CL      POP_V     POP_KA   POP_TLAG   RUV_PROP    RUV_ADD 
##  0.1346600  8.0468500  1.2944300  0.8629310  0.0628024  0.3147830 
## BETA_CL_WT  BETA_V_WT     PPV_CL  CORR_CL_V      PPV_V     PPV_KA 
##  0.7500000  1.0000000  0.2635890  0.2128860  0.1366690  0.9175980 
##   PPV_TLAG 
##  0.1000000 
## 
## $Bootstrap
##             Parameter        Mean      Median
## BETA_CL_WT BETA_CL_WT 0.750000000 0.750000000
## BETA_V_WT   BETA_V_WT 1.000000000 1.000000000
## CORR_CL_V   CORR_CL_V 0.006601379 0.005672145
## POP_CL         POP_CL 0.135766800 0.136824000
## POP_KA         POP_KA 1.357139000 1.242695000
## POP_TLAG     POP_TLAG 0.849418600 0.869470500
## POP_V           POP_V 8.034198000 7.973425000
## PPV_CL         PPV_CL 0.071233230 0.070891400
## PPV_KA         PPV_KA 0.715497300 0.716611500
## PPV_TLAG     PPV_TLAG 0.010000000 0.010000000
## PPV_V           PPV_V 0.015006800 0.015667400
## RUV_ADD       RUV_ADD 0.208932400 0.249919500
## RUV_PROP     RUV_PROP 0.088466900 0.079072550

Extract the information regarding the precision intervals

print(getPopulationParameters(bootstrapResults, what="intervals")$Bootstrap)
##     Parameter        Mean      Median       Perc_5    Perc_95
## 1  BETA_CL_WT 0.750000000 0.750000000  0.750000000 0.75000000
## 2   BETA_V_WT 1.000000000 1.000000000  1.000000000 1.00000000
## 3   CORR_CL_V 0.006601379 0.005672145 -0.004049103 0.01591210
## 4      POP_CL 0.135766800 0.136824000  0.123975500 0.14836720
## 5      POP_KA 1.357139000 1.242695000  0.706269800 2.38384400
## 6    POP_TLAG 0.849418600 0.869470500  0.551942100 0.97224690
## 7       POP_V 8.034198000 7.973425000  7.618959000 8.55206800
## 8      PPV_CL 0.071233230 0.070891400  0.033271180 0.12462570
## 9      PPV_KA 0.715497300 0.716611500  0.185977000 1.35855100
## 10   PPV_TLAG 0.010000000 0.010000000  0.010000000 0.01000000
## 11      PPV_V 0.015006800 0.015667400  0.005174977 0.02571412
## 12    RUV_ADD 0.208932400 0.249919500  0.001099000 0.42160650
## 13   RUV_PROP 0.088466900 0.079072550  0.037310650 0.16461970

VPC of model

Before running the VPC with PsN we must update the (initial) values in the MDL Parameter Object MLE estimates from previous step can be used

structuralPar <- getPopulationParameters(NM.FOCEI, what="estimates",block='structural')$MLE
variabilityPar <- getPopulationParameters(NM.FOCEI, what="estimates",block='variability')$MLE

Update the parameter object using the ddmore “updateParObj” function. This function updates an R object of (S4) class “parObj”. The user chooses which block to update, what items within that block, and what to replace those items with. NOTE: that updateParObj can only update attributes which ALREADY EXIST in the MDL Parameter Object for that item. This ensures that valid MDL is preserved.

myParObj <- getParameterObjects(mdlfile)[[1]]
myParObjUpdated <- updateParObj(myParObj,block="STRUCTURAL",
        item=names(structuralPar),
        with=list(value=structuralPar))
myParObjUpdated <- updateParObj(myParObjUpdated,block="VARIABILITY",
        item=names(variabilityPar),
        with=list(value=variabilityPar))

Check that the appropriate initial values have been updated to the MLE values from the previous fit.

print(myParObjUpdated@STRUCTURAL)
## $POP_CL
## $POP_CL$value
## [1] "0.13466"
## 
## $POP_CL$lo
## [1] "0.001"
## 
## 
## $POP_V
## $POP_V$value
## [1] "8.04685"
## 
## $POP_V$lo
## [1] "0.001"
## 
## 
## $POP_KA
## $POP_KA$value
## [1] "1.29443"
## 
## $POP_KA$lo
## [1] "0.001"
## 
## 
## $POP_TLAG
## $POP_TLAG$value
## [1] "0.862931"
## 
## $POP_TLAG$lo
## [1] "0.001"
## 
## 
## $BETA_CL_WT
## $BETA_CL_WT$value
## [1] "0.75"
## 
## $BETA_CL_WT$fix
## [1] "true"
## 
## 
## $BETA_V_WT
## $BETA_V_WT$value
## [1] "1"
## 
## $BETA_V_WT$fix
## [1] "true"
print(myParObjUpdated@VARIABILITY)
## $PPV_CL
## $PPV_CL$value
## [1] "0.263589"
## 
## 
## $PPV_V
## $PPV_V$value
## [1] "0.136669"
## 
## 
## $PPV_KA
## $PPV_KA$value
## [1] "0.917598"
## 
## 
## $PPV_TLAG
## $PPV_TLAG$value
## [1] "0.1"
## 
## $PPV_TLAG$fix
## [1] "true"
## 
## 
## $CORR_CL_V
## $CORR_CL_V$value
## [1] "0.212886"
## 
## 
## $RUV_PROP
## $RUV_PROP$value
## [1] "0.0628024"
## 
## $RUV_PROP$lo
## [1] "0"
## 
## 
## $RUV_ADD
## $RUV_ADD$value
## [1] "0.314783"
## 
## $RUV_ADD$lo
## [1] "1.0E-4"

Assembling the new MOG. Note that we reuse the data, model and tasks from the previous run.

myVPCMOG <- createMogObj(dataObj = getDataObjects(mdlfile)[[1]], 
        parObj = myParObjUpdated, 
        mdlObj = getModelObjects(mdlfile)[[1]], 
        taskObj = getTaskPropertiesObjects(mdlfile)[[1]])

We can then write the MOG back out to an .mdl file.

mdlfile.VPC <- paste0(uc,"_VPC.mdl")
writeMogObj(myVPCMOG,mdlfile.VPC)

Similarly as above, ddmore “VPC.PsN” function can be used to run a VPC using PsN as target tool

vpcFiles <- VPC.PsN(mdlfile.VPC,samples=20, seed=12345,
        vpcOptions ="-n_simulation=10 -auto_bin=10",
        subfolder="VPC", plot=TRUE) 
## -- Mon Aug 15 16:28:16 2016
## New
## Submitted
## Job 634d58ef-0b3b-43d7-a591-814e9a1d138e progress:
## Running [ ... ]
## Importing Results
## Copying the result data back to the local machine for job ID 634d58ef-0b3b-43d7-a591-814e9a1d138e...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job356467e95dfc to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase1/VPC
## Done.
## 
## 
## The following main elements were parsed successfully:
##   RawResults
##   SimulationSimulationBlock
##   SimulationSimulationBlock
## 
## The following MESSAGEs were raised during the job execution:
##  nmoutput2so_version: This SOBlock was created with nmoutput2so version 4.5.27
## 
## Completed
## -- Mon Aug 15 16:29:18 2016

plot of chunk VPC

To replay the visualisation using information from the VPC SO file

pdf(paste0(uc,"_VPC.pdf"))
 plot(xpose.VPC(vpc.info= file.path("./VPC",vpcFiles@RawResults@DataFiles$PsN_VPC_results$path),
        vpctab= file.path("./VPC",vpcFiles@RawResults@DataFiles$PsN_VPC_vpctab$path),
        main="VPC warfarin"))
dev.off()
## png 
##   2

Simulation using simulx

The mlxR package has been developed to visualize and explore models that are encoded in MLXTRAN or PharmML The ddmore function as.PharmML translates an MDL file (extension .mdl) to its PharmML representation. The output file (extension .xml) is saved in the working directory.

myPharmML <- as.PharmML(mdlfile)

Use parameter values from the FOCEI estimation

parValues <- getPopulationParameters(NM.FOCEI, what="estimates")$MLE

Simulate for the typical weight of 70. Recall that logtWT = log(WT/70).

p <- c(parValues,logtWT=0)

Parameter values used in simulation

print(p)
##     POP_CL      POP_V     POP_KA   POP_TLAG   RUV_PROP    RUV_ADD 
##  0.1346600  8.0468500  1.2944300  0.8629310  0.0628024  0.3147830 
## BETA_CL_WT  BETA_V_WT     PPV_CL  CORR_CL_V      PPV_V     PPV_KA 
##  0.7500000  1.0000000  0.2635890  0.2128860  0.1366690  0.9175980 
##   PPV_TLAG     logtWT 
##  0.1000000  0.0000000

Simulate PK parameters for individuals

ind <- list(name = c('TLAG','KA','CL','V'))

Simulate predicted (CC) and observed concentration values (Y)

f   <- list( name = c('CC'), time = seq(0,to=50,by=1))
y   <- list( name = c('Y'), time = c(0, 0.5, 1, 2, 3, 4, 6, 8, 12, 24, 36, 48))

Simulate for a dose of 100mg given at time 0 into the GUT (oral administration)

adm <- list(target='GUT', time = 0, amount = 100)

Simulate 12 subjects

g <- list( size = 12, level = 'individual', treatment=adm)

Call simulx

res  <- simulx(model =myPharmML,
               parameter = p,
               group = g,
               output = list(ind,y,f))

Simulated parameter values for each individual

print(res$parameter)
##    id      TLAG         KA         CL         V
## 1   1 0.7913309  1.5383068 0.14977929  7.694128
## 2   2 1.0028087  0.8058306 0.12444606  6.488144
## 3   3 0.8986409 24.7808753 0.11482284  8.042080
## 4   4 0.8513698  0.4824796 0.11496917  8.546734
## 5   5 0.9004174  0.2229009 0.10748701  9.209824
## 6   6 0.8876357  1.3460423 0.12275335 10.165668
## 7   7 0.8109790  0.1928436 0.07716102 10.188859
## 8   8 0.9601427  1.9267014 0.11136785 10.973770
## 9   9 0.7487865  0.9263937 0.14117459  6.909233
## 10 10 0.8856971  2.2336167 0.13579359  7.471812
## 11 11 0.8826824  0.8462787 0.19315549  8.113318
## 12 12 0.8500256  0.4582425 0.15521015  8.560211

Plot simulated results

plot(ggplot() + 
                geom_line(data=res$CC, aes(x=time, y=CC, colour=id)) +
                geom_point(data=res$Y, aes(x=time, y=Y, colour=id)) +
                xlab("time (h)") + ylab("concentration") )

plot of chunk unnamed-chunk-44

Simulate 1000 subjects - with simulx this is a QUICK process!

g <- list( size = 1000, level = 'individual',  treatment = adm)

Call simulx

res.1000  <- simulx(model =myPharmML,
                    parameter = p,
                    group = g,
                    output = list(ind,y))

Plot prediction intervals with prctilemlx. band defines the percentile bands displayed:

plot(prctilemlx(res.1000$Y,band=list(number=9, level=90)))

plot of chunk unnamed-chunk-46

Table of the same information

print(prctilemlx(res.1000$Y,band=list(number=10, level=100), plot=FALSE)$y)
##    time         0%        10%        20%         30%          40%
## 1   0.0 -0.6650112 -0.3878409 -0.2759908 -0.20125958 -0.084938410
## 2   0.5 -0.7144047 -0.3428163 -0.1961387 -0.09181983 -0.001034849
## 3   1.0 -0.4826743  0.3915174  0.7587353  1.23592441  1.713589255
## 4   2.0  3.2602299  7.5555810 11.4614198 13.69685676 15.503492007
## 5   3.0  6.1786625 12.3965200 16.2984887 18.27327380 19.846901466
## 6   4.0  7.7257979 15.5581680 18.5855787 19.99237765 21.515872370
## 7   6.0 11.0799901 17.5110975 19.2374108 20.23897449 20.991582528
## 8   8.0 11.7838145 16.9717234 18.7109402 19.68703190 20.653312683
## 9  12.0 15.2066912 17.3789136 18.5041136 19.04081139 19.715532772
## 10 24.0 10.8601329 14.0142020 15.0071817 15.54981228 16.057784898
## 11 36.0  7.7504395 10.7736954 12.2161534 12.72192003 13.391264032
## 12 48.0  4.8144638  8.6474204  9.7166439 10.48169193 10.925676476
##            50%         50%         60%         70%        80%        90%
## 1  -0.04139662 -0.04139662  0.02499712  0.09674689  0.1827609  0.3925643
## 2   0.04825664  0.04825664  0.09176159  0.17588343  0.2874130  0.3937877
## 3   2.40821987  2.40821987  3.44754941  4.98239016  6.3339425 10.6149923
## 4  17.48697864 17.48697864 18.66748103 20.22748358 22.5611373 25.8287706
## 5  21.04985374 21.04985374 22.44715043 23.93668815 24.9920746 26.2614674
## 6  22.47737119 22.47737119 23.45932426 23.90304699 25.2002925 27.4722474
## 7  21.82855279 21.82855279 23.13181901 24.37069119 25.7633099 27.8790810
## 8  21.58836014 21.58836014 22.72372931 23.55748618 24.8136814 25.8782012
## 9  20.31357567 20.31357567 21.93642073 22.76119255 23.3512499 24.4950710
## 10 16.92530863 16.92530863 17.56496214 18.40505437 19.0603044 20.1363024
## 11 13.78817353 13.78817353 14.65923716 15.48769747 16.5055051 17.9387153
## 12 11.50488542 11.50488542 12.31591407 12.97902404 13.5337158 14.3218896
##          100%
## 1   0.8552684
## 2   0.7803884
## 3  25.1728326
## 4  34.2603146
## 5  39.1122770
## 6  35.7701219
## 7  31.1222474
## 8  31.1155623
## 9  30.5263834
## 10 24.4986102
## 11 20.1233098
## 12 16.0807855