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 wiht the coursor any code lines you wish to execute and press CTRL+R+R in your keyboard.
Clear workspace and set working directory under 'UsesCasesDemo' project
rm(list=ls(all=F))
mydir <- file.path(Sys.getenv("MDLIDE_WORKSPACE_HOME"),"UseCasesDemo")
setwd(mydir)
Set name of .mdl file and dataset for future tasks
uc <- "UseCase2_2"
datafile <- "warfarin_conc_analytic.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)
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))
cache.path <- file.path(mydir,uc, 'cache')
dir.create(cache.path)
opts_chunk$set(cache.path=cache.path)
figure.path <- file.path(mydir,uc, 'figure')
dir.create(figure.path)
opts_chunk$set(figure.path=figure.path)
List files available in working directory
list.files()
## [1] "cache" "figure"
## [3] "UseCase2_2.mdl" "warfarin_conc_analytic.csv"
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 Standard Output object which is saved in a .SO.xml file.
Translated files and Monolix output will be returned in the ./Monolix subfolder. The Standard 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")
## -- Wed Aug 17 08:51:41 2016
## New
## Submitted
## Job 53761d0f-c2b0-49b4-92bb-eb633968a02f progress:
## Running [ .......... ]
## Importing Results
## Copying the result data back to the local machine for job ID 53761d0f-c2b0-49b4-92bb-eb633968a02f...
## From C:\Users\zparra\AppData\Local\Temp\Rtmpk5tG0t\DDMORE.job1c8457084b to D:/SEE-Prod5_RC4/MDL_IDE/workspace/UseCasesDemo/UseCase2_2/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
## -- Wed Aug 17 08:55:08 2016
slotNames(mlx)
## [1] "ToolSettings" "RawResults" "TaskInformation"
## [4] "Estimation" "ModelDiagnostic" "Simulation"
## [7] "OptimalDesign" ".pathToSourceXML"
The ddmore “LoadSOObj” function reads and parses existing Standard Output Objects
mlx <- LoadSOObject(“Monolix/UseCase2_2.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
print(parameters_mlx)
## POP_V BETA_V_WT POP_KA POP_CL BETA_CL_WT POP_TLAG
## 8.08758 1.00000 1.67019 0.13438 0.75000 0.97399
## PPV_V PPV_KA PPV_CL PPV_TLAG CORR_CL_V RUV_ADD
## 0.13357 1.12170 0.26583 0.10000 0.19380 0.22733
## RUV_PROP
## 0.06484
print(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.19380 0.20050 103.46
## 4 POP_CL 0.13438 0.00644 4.80
## 5 POP_KA 1.67019 0.67645 40.50
## 6 POP_TLAG 0.97399 0.05459 5.60
## 7 POP_V 8.08758 0.22016 2.72
## 8 PPV_CL 0.26583 0.03450 12.98
## 9 PPV_KA 1.12170 0.29706 26.48
## 10 PPV_TLAG 0.10000 0.00000 0.00
## 11 PPV_V 0.13357 0.02198 16.46
## 12 RUV_ADD 0.22733 0.04455 19.60
## 13 RUV_PROP 0.06484 0.00919 14.18
print(getEstimationInfo(mlx))
## $OFMeasures
## $OFMeasures$LogLikelihood
## $OFMeasures$LogLikelihood[[1]]
## [1] -332.895
##
##
## $OFMeasures$IndividualContribToLL
## Subject ICtoLL
## 1 1 -24.46
## 2 2 -5.22
## 3 3 -13.08
## 4 4 -12.17
## 5 5 -11.05
## 6 6 -7.51
## 7 7 -18.58
## 8 8 -20.94
## 9 9 -30.99
## 10 10 -5.77
## 11 12 -19.83
## 12 13 -19.28
## 13 14 -19.29
## 14 15 -11.46
## 15 16 -14.38
## 16 17 -5.69
## 17 18 -5.13
## 18 19 -6.62
## 19 20 -5.18
## 20 21 -5.95
## 21 22 -5.87
## 22 23 -8.12
## 23 24 -4.86
## 24 25 -7.16
## 25 26 -7.25
## 26 27 -5.29
## 27 28 -6.82
## 28 29 -5.58
## 29 30 -4.57
## 30 31 -5.03
## 31 32 -4.71
## 32 33 -5.07
##
## $OFMeasures$InformationCriteria
## $OFMeasures$InformationCriteria$AIC
## [1] 685.79
##
## $OFMeasures$InformationCriteria$BIC
## [1] 700.44
##
##
##
## $Messages
## list()
Use 'ddmore' function as.xpdb() to create an Xpose database object from the Standard 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 (graphs are exported to PDF file)
print(basic.gof(mlx.xpdb))
print(ind.plots(mlx.xpdb))
Export graphs to a PDF file
pdf("GOF_MLX.pdf")
print(basic.gof(mlx.xpdb))
print(ind.plots(mlx.xpdb))
dev.off()
## png
## 2
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
NM.FOCEI <- estimate(mdlfile.FOCEI, target="PsN", subfolder="NONMEM_FOCEI")
## -- Wed Aug 17 08:55:48 2016
## New
## Submitted
## Job 865adb69-f78b-460e-995b-b58bccccd3e1 progress:
## Running [ .... ]
## Importing Results
## Copying the result data back to the local machine for job ID 865adb69-f78b-460e-995b-b58bccccd3e1...
## From C:\Users\zparra\AppData\Local\Temp\Rtmpk5tG0t\DDMORE.job1c84ad61bef to D:/SEE-Prod5_RC4/MDL_IDE/workspace/UseCasesDemo/UseCase2_2/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.9
## nmoutput2so_version: This SOBlock was created with nmoutput2so version 4.5.27
##
## Completed
## -- Wed Aug 17 08:57:14 2016
Load previous results
NM.FOCEI <- LoadSOObject(“NONMEM_FOCEI/UseCase2_2_FOCEI.SO.xml”)
Results from NONMEM should be comparable with results from MONOLIX
print(getPopulationParameters(NM.FOCEI, what="estimates"))
## $MLE
## POP_CL POP_V POP_KA POP_TLAG RUV_PROP RUV_ADD
## 0.1341370 8.1009300 1.5617300 0.9677410 0.0716013 0.1932260
## BETA_CL_WT BETA_V_WT PPV_CL CORR_CL_V PPV_V PPV_KA
## 0.7500000 1.0000000 0.2637870 0.2425230 0.1348370 0.9360510
## PPV_TLAG
## 0.1000000
print(parameters_mlx)
## POP_V BETA_V_WT POP_KA POP_CL BETA_CL_WT POP_TLAG
## 8.08758 1.00000 1.67019 0.13438 0.75000 0.97399
## PPV_V PPV_KA PPV_CL PPV_TLAG CORR_CL_V RUV_ADD
## 0.13357 1.12170 0.26583 0.10000 0.19380 0.22733
## RUV_PROP
## 0.06484
When basing VPC on estimation from a target software other than NONMEM we must update the parameter values.
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.134137"
##
## $POP_CL$lo
## [1] "0.001"
##
##
## $POP_V
## $POP_V$value
## [1] "8.10093"
##
## $POP_V$lo
## [1] "0.001"
##
##
## $POP_KA
## $POP_KA$value
## [1] "1.56173"
##
## $POP_KA$lo
## [1] "0.001"
##
##
## $POP_TLAG
## $POP_TLAG$value
## [1] "0.967741"
##
## $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.263787"
##
##
## $PPV_V
## $PPV_V$value
## [1] "0.134837"
##
##
## $PPV_KA
## $PPV_KA$value
## [1] "0.936051"
##
##
## $PPV_TLAG
## $PPV_TLAG$value
## [1] "0.1"
##
## $PPV_TLAG$fix
## [1] "true"
##
##
## $CORR_CL_V
## $CORR_CL_V$value
## [1] "0.242523"
##
##
## $RUV_PROP
## $RUV_PROP$value
## [1] "0.0716013"
##
## $RUV_PROP$lo
## [1] "0"
##
##
## $RUV_ADD
## $RUV_ADD$value
## [1] "0.193226"
##
## $RUV_ADD$lo
## [1] "1.0E-4"
Assembling the new MOG. Note that we reuse the data and model 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)
## -- Wed Aug 17 08:57:30 2016
## New
## Submitted
## Job 0c230152-eb0c-460f-85c3-62330d7b9603 progress:
## Running [ ..... ]
## Importing Results
## Copying the result data back to the local machine for job ID 0c230152-eb0c-460f-85c3-62330d7b9603...
## From C:\Users\zparra\AppData\Local\Temp\Rtmpk5tG0t\DDMORE.job1c842ba4635f to D:/SEE-Prod5_RC4/MDL_IDE/workspace/UseCasesDemo/UseCase2_2/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
## -- Wed Aug 17 08:59:16 2016
To replay the visualisation using information from the VPC SO file
pdf(paste0(uc,"_VPC.pdf"))
print(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
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). Add values for D (dose) and DT (dosing time) defined as variables (regressors) in the model
p <- c(parValues,WT=70, D=100, DT=0)
Parameter values used in simulation
print(p)
## POP_CL POP_V POP_KA POP_TLAG RUV_PROP RUV_ADD
## 0.1341370 8.1009300 1.5617300 0.9677410 0.0716013 0.1932260
## BETA_CL_WT BETA_V_WT PPV_CL CORR_CL_V PPV_V PPV_KA
## 0.7500000 1.0000000 0.2637870 0.2425230 0.1348370 0.9360510
## PPV_TLAG WT D DT
## 0.1000000 70.0000000 100.0000000 0.0000000
Simulate PK parameters for individuals
ind <- list(name = c('TLAG','KA','CL','V'))
Simulate predicted (CC) and observed concentration values (Y)
y <- list( name = c('Y'), time = c(0, 0.5, 1, 2, 3, 4, 6, 8, 12, 24, 36, 48))
Simulate 12 subjects
g <- list( size = 12, level = 'individual')
Call simulx
res <- simulx(model =myPharmML,
parameter = p,
group = g,
output = list(ind,y))
Simulated parameter values for each individual
print(res$parameter)
## id TLAG KA CL V
## 1 1 0.9356894 0.6451967 0.18197151 8.799391
## 2 2 0.9700949 1.4414063 0.11884120 7.472901
## 3 3 0.9040197 0.5246312 0.15667877 8.135486
## 4 4 0.9903474 4.8557984 0.10518821 9.309286
## 5 5 1.0400231 1.8951482 0.08414053 8.149116
## 6 6 0.9774454 2.2099002 0.10300078 7.718685
## 7 7 0.9947353 1.6160878 0.19504679 9.360933
## 8 8 0.9704389 3.1210803 0.18149569 10.150381
## 9 9 1.0801591 10.7717333 0.11256055 8.772858
## 10 10 1.0333295 3.1836816 0.16202058 9.016320
## 11 11 0.9817942 0.9392428 0.11919247 6.899412
## 12 12 1.0836311 1.2956363 0.11878333 6.617062
Plot simulated results
plot(ggplot() +
geom_line(data=res$Y, aes(x=time, y=Y, colour=id)) +
xlab("time (h)") + ylab("concentration"))
Simulate 1000 subjects - with simulx this is a QUICK process!
g <- list( size = 1000, level = 'individual')
Call simulx
res.1000 <- simulx(model =myPharmML,
parameter = p,
group = g,
output = list(ind,y))
Plot of observed concentrations (with residual error) band defines the percentile bands displayed level = range of values to examine (in %) 100 = full range of values number = number of bins within the level range.
print(prctilemlx(res.1000$Y,band=list(number=9, level=90)))
Table of the same information
print(prctilemlx(res.1000$Y,band=list(number=10, level=100), plot=F)$y)
## time 0% 10% 20% 30% 40%
## 1 0.0 -0.3757483 -0.26110984 -0.19233740 -0.10858583 -0.06109141
## 2 0.5 -0.3706682 -0.25536970 -0.17246049 -0.09981883 -0.02971514
## 3 1.0 -0.2917140 -0.07357764 0.01106998 0.12804411 0.24055799
## 4 2.0 2.0817819 5.31436899 6.44906101 7.50284076 8.67047746
## 5 3.0 2.8381545 7.47347847 8.91653881 9.58494618 10.23390313
## 6 4.0 4.4298149 8.82258216 9.15797669 10.03222450 10.59972112
## 7 6.0 4.9099734 9.26098043 9.86172412 10.21070049 10.60435442
## 8 8.0 5.3950661 9.03924181 9.46239977 10.18147355 10.49975628
## 9 12.0 7.3722019 8.61507710 8.98635458 9.27221152 9.77122902
## 10 24.0 5.6026466 6.76009368 7.24389700 7.64215556 7.82740952
## 11 36.0 4.1297099 5.44223555 5.89749193 6.22009564 6.65229541
## 12 48.0 2.5820845 4.23110815 4.69204295 4.98872253 5.38557343
## 50% 50% 60% 70% 80% 90%
## 1 -0.01177881 -0.01177881 0.04037895 0.09073635 0.1367715 0.2038863
## 2 0.02593226 0.02593226 0.05136149 0.08733063 0.1304080 0.2201305
## 3 0.38845627 0.38845627 0.82275871 1.28855461 2.0985882 3.5840481
## 4 9.46993725 9.46993725 10.16840081 10.77093527 11.8484318 13.1585944
## 5 10.93249502 10.93249502 11.53349637 12.11837697 12.9633060 13.4534256
## 6 11.15041340 11.15041340 11.65063755 12.14244278 12.9010263 13.8902896
## 7 11.08353209 11.08353209 11.65485980 12.27149963 12.6509724 13.3816359
## 8 10.73803839 10.73803839 11.42921944 11.90932606 12.6439714 13.3513918
## 9 10.38023436 10.38023436 10.84878651 11.12702666 11.7228660 12.1837622
## 10 8.23918064 8.23918064 8.80852999 9.17723903 9.8259105 10.1931192
## 11 7.10509016 7.10509016 7.40504258 7.81432558 8.0483513 8.6788733
## 12 5.65042185 5.65042185 5.92257117 6.48641007 6.9632076 7.3880965
## 100%
## 1 0.3797270
## 2 0.4194215
## 3 12.8920853
## 4 16.0372964
## 5 18.6038388
## 6 16.5212834
## 7 17.5137888
## 8 16.1526235
## 9 14.8241989
## 10 11.9060324
## 11 10.7187334
## 12 9.2190855