Guided-demo: Exploring 'ddmore' R package functionality of count data models

UseCase11 : Poisson distribution

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)

Create a working directory under 'models' folder where results are stored

uc<-"UseCase11"
datafile <- "count.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)) 

List files available in working directory

list.files()
## [1] "count.csv"     "UseCase11.mdl"

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] "Poisson_DIST_dat"  "Poisson_DIST_par"  "Poisson_DIST_mdl" 
## [4] "Poisson_DIST_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     CP  DV MDV
## 1  1  0.00 0.0000   0   1
## 2  1  0.00 0.0000   7   0
## 3  1  0.25 1.0089   9   0
## 4  1  2.00 5.5795 131   0
## 5  1  6.00 7.5844 332   0
## 6  1 24.00 1.7112  17   0

Extract only observation records

myEDAData<-myData[myData$MDV==0,]
head(myEDAData)
##   ID  TIME     CP  DV MDV
## 2  1  0.00 0.0000   7   0
## 3  1  0.25 1.0089   9   0
## 4  1  2.00 5.5795 131   0
## 5  1  6.00 7.5844 332   0
## 6  1 24.00 1.7112  17   0
## 8  2  0.00 0.0000   9   0

Open an R window to record and access all your plots

windows(record=TRUE) 

Now plot the data using xyplot from the lattice library

plot1 <- xyplot(DV~TIME,groups=ID,data=myEDAData,type="b",ylab="Counts",xlab="Time (h)")
print(plot1)

plot of chunk unnamed-chunk-15

plot2 <- xyplot(DV~CP,groups=ID,data=myEDAData,type="p",ylab="Counts",xlab="Conc")
print(plot2)

plot of chunk unnamed-chunk-15

plot3 <- histogram(~DV, data=myEDAData, xlab="", type="count")
print(plot3)

plot of chunk unnamed-chunk-15

Export results to a PDF file

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

Compute mean and variances for each id

stats <- ddply(myEDAData, .(ID), summarise, mean=mean(DV), var=var(DV))

plot(xyplot(log(var)~log(mean),data=stats,
        main="Mean-Variance Plot",ylab="log(Individual variance)",xlab="log(Individual mean)"))

plot of chunk unnamed-chunk-17

Compute the frequency of counts up to 10 for the Poissonness plot (Plan et al, CPT:PsP,2014)

library(plyr)
poiss <- count(myEDAData,"DV")
poiss$metameter<- log(poiss$freq) + lfactorial(poiss$DV) -log(sum(poiss$freq))

plot(xyplot(metameter~DV,data=poiss[poiss$DV<1000,],
        main="Poissonness plot",ylab="Count metameter",xlab="Count"))

plot of chunk unnamed-chunk-18

How many observations per ID?

library(tables)
## Loading required package: Hmisc
## Loading required package: survival
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## 
## The following objects are masked from 'package:plyr':
## 
##     is.discrete, summarize
## 
## The following objects are masked from 'package:base':
## 
##     format.pval, round.POSIXt, trunc.POSIXt, units
tabular(~(ID=as.factor(ID)), data=myEDAData)
##                                                                         
##    ID                                                                   
##    1  2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
##    5  5 5 5 5 5 5 5 5 5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5 
##                                                                         
##                                                                         
##  27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
##  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5 
##                                                                         
##                                                                         
##  51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
##  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5 
##                                                                         
##                                                                         
##  75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
##  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5 
##        
##        
##  99 100
##  5  5
tabular(~(DV=as.factor(DV)), data=myEDAData)
##                                                                          
##    DV                                                                    
##    3  4 5 6 7  8 9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
##    2  5 8 8 20 8 10 21 17 15 22 16 14 14 13 10 10 12 5  4  8  6  2  6  8 
##                                                                         
##                                                                         
##  28 29 30 31 32 33 34 35 37 38 41 45 46 47 49 50 52 54 56 58 59 61 63 69
##  2  6  3  3  6  4  2  3  1  1  1  1  1  2  1  1  1  1  1  1  2  1  1  1 
##                                                                          
##                                                                          
##  71 73 78 82 83 86 89 90 91 92 93 95 96 97 98 101 103 104 105 109 110 113
##  2  2  2  1  1  1  2  1  3  3  1  1  1  1  2  3   1   1   1   1   2   1  
##                                                                         
##                                                                         
##  116 117 120 121 123 127 128 131 132 133 135 138 143 147 150 152 154 156
##  1   2   1   1   1   1   1   1   1   2   3   2   1   2   3   2   3   1  
##                                                                         
##                                                                         
##  157 160 164 165 170 174 176 180 183 184 187 188 192 193 194 195 196 202
##  1   1   1   1   1   1   1   1   1   2   2   1   1   2   1   1   1   1  
##                                                                         
##                                                                         
##  203 207 215 216 217 218 219 222 223 224 226 228 230 232 238 239 242 243
##  1   1   1   1   2   2   1   1   3   1   1   1   4   2   1   1   1   1  
##                                                                         
##                                                                         
##  244 245 247 251 252 254 259 268 275 277 278 284 289 290 293 305 319 320
##  2   1   1   2   3   2   1   2   2   1   2   1   1   1   1   1   1   1  
##                                                                         
##                                                                         
##  321 323 326 331 332 339 344 346 350 354 364 365 373 379 384 390 401 404
##  2   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1  
##                                                                         
##                                                                         
##  409 435 441 459 465 474 508 517 590 593 597 647 657 665 684 714 716 746
##  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1  
##                                                                       
##                                                                       
##  793 820 933 951 981 1008 1018 1037 1142 1168 1885 1999 2881 3020 4360
##  1   1   1   1   1   1    1    1    1    1    1    1    1    1    1   
##      
##      
##  4494
##  1

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")
## -- Tue Aug 16 16:19:14 2016
## New
## Submitted
## Job dd29de8e-15ad-4129-9301-abca8c042503 progress:
## Running [ ......... ]
## Importing Results
## Copying the result data back to the local machine for job ID dd29de8e-15ad-4129-9301-abca8c042503...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job35645707375 to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase11/Monolix
## Done.
## 
## 
## The following main elements were parsed successfully:
##   ToolSettings
##   RawResults
##   Estimation::PopulationEstimates::MLE
##   Estimation::PrecisionPopulationEstimates::MLE
##   Estimation::IndividualEstimates::Estimates
##   Estimation::IndividualEstimates::RandomEffects
##   Estimation::Predictions
##   Estimation::OFMeasures::IndividualContribToLL
##   Estimation::OFMeasures::InformationCriteria
##   Estimation::OFMeasures::LogLikelihood
## 
## Completed
## -- Tue Aug 16 16:22:17 2016

The ddmore “LoadSOObj” reads and parsed existing Standardise Output Objects

mlx <- LoadSOObject(“Monolix/UseCase11.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_BETA POP_BASECOUNT     PPV_EVENT 
##       0.50175      10.00776       0.03938
print(getPopulationParameters(mlx, what="precisions"))
## $MLE
##       Parameter      MLE      SE   RSE
## 1 POP_BASECOUNT 10.00776 0.35054  3.50
## 2      POP_BETA  0.50175 0.00472  0.94
## 3     PPV_EVENT  0.03938 0.00588 14.93
print(getEstimationInfo(mlx))
## $OFMeasures
## $OFMeasures$LogLikelihood
## $OFMeasures$LogLikelihood[[1]]
## [1] -1779.985
## 
## 
## $OFMeasures$IndividualContribToLL
##     Subject ICtoLL
## 1         1 -16.83
## 2         2 -16.94
## 3         3 -16.93
## 4         4 -20.70
## 5         5 -15.68
## 6         6 -17.34
## 7         7 -15.72
## 8         8 -16.61
## 9         9 -15.36
## 10       10 -17.84
## 11       11 -15.78
## 12       12 -21.27
## 13       13 -19.68
## 14       14 -17.31
## 15       15 -16.39
## 16       16 -17.02
## 17       17 -21.79
## 18       18 -17.12
## 19       19 -21.71
## 20       20 -19.39
## 21       21 -20.84
## 22       22 -17.02
## 23       23 -17.77
## 24       24 -16.71
## 25       25 -19.58
## 26       26 -24.51
## 27       27 -18.04
## 28       28 -16.92
## 29       29 -19.57
## 30       30 -20.34
## 31       31 -17.14
## 32       32 -16.18
## 33       33 -16.16
## 34       34 -20.26
## 35       35 -14.94
## 36       36 -16.79
## 37       37 -17.20
## 38       38 -15.39
## 39       39 -18.57
## 40       40 -21.13
## 41       41 -14.96
## 42       42 -18.40
## 43       43 -19.92
## 44       44 -18.81
## 45       45 -17.01
## 46       46 -16.71
## 47       47 -18.49
## 48       48 -18.37
## 49       49 -14.97
## 50       50 -14.44
## 51       51 -16.99
## 52       52 -15.74
## 53       53 -16.36
## 54       54 -18.64
## 55       55 -17.32
## 56       56 -20.14
## 57       57 -17.99
## 58       58 -18.44
## 59       59 -22.93
## 60       60 -16.39
## 61       61 -17.53
## 62       62 -20.12
## 63       63 -15.15
## 64       64 -17.94
## 65       65 -18.80
## 66       66 -16.09
## 67       67 -17.22
## 68       68 -23.28
## 69       69 -17.04
## 70       70 -14.53
## 71       71 -22.39
## 72       72 -14.47
## 73       73 -18.86
## 74       74 -16.08
## 75       75 -19.02
## 76       76 -16.79
## 77       77 -16.57
## 78       78 -17.46
## 79       79 -19.02
## 80       80 -15.41
## 81       81 -18.67
## 82       82 -18.45
## 83       83 -18.88
## 84       84 -20.06
## 85       85 -16.32
## 86       86 -15.13
## 87       87 -15.67
## 88       88 -16.84
## 89       89 -16.28
## 90       90 -16.81
## 91       91 -18.92
## 92       92 -17.18
## 93       93 -15.84
## 94       94 -23.61
## 95       95 -18.10
## 96       96 -19.03
## 97       97 -16.30
## 98       98 -16.86
## 99       99 -16.25
## 100     100 -15.53
## 
## $OFMeasures$InformationCriteria
## $OFMeasures$InformationCriteria$AIC
## [1] 3565.97
## 
## $OFMeasures$InformationCriteria$BIC
## [1] 3573.79
## 
## 
## 
## $Messages
## list()

Perform model diagnostics for the base model using Xpose functions (graphs are exported to PDF)

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)
## Source data frame: rawData 500
## Number of Rows in source data frame:  500
## Target data frame: Predictions 1
## Number of Rows in source data frame:  1
## Error in checkDoseRows(df1, df2, label1 = "rawData", label2 = "Predictions"): Number of non-dose rows differs when attempting to merge in Predictions

We can then call Xpose functions referencing this mlx.xpdb object as the input. Goodness of fit are not defined for discrete data models. Therefore, only a parameter histogram is shown.

print(parm.hist(mlx.xpdb, inclZeroWRES=TRUE))
## Error in print(parm.hist(mlx.xpdb, inclZeroWRES = TRUE)): error in evaluating the argument 'x' in selecting a method for function 'print': Error in xvardef("parms", object) : object 'mlx.xpdb' not found

Export graphs to a PDF file

pdf("GOF_MLX.pdf")
print(parm.hist(mlx.xpdb, inclZeroWRES=TRUE))
## Error in print(parm.hist(mlx.xpdb, inclZeroWRES = TRUE)): error in evaluating the argument 'x' in selecting a method for function 'print': Error in xvardef("parms", object) : object 'mlx.xpdb' not found
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")
## -- Tue Aug 16 16:22:17 2016
## New
## Submitted
## Job 8d7ea1f2-fb4e-4914-97a3-33d8df60d905 progress:
## Running [ ... ]
## Importing Results
## Copying the result data back to the local machine for job ID 8d7ea1f2-fb4e-4914-97a3-33d8df60d905...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job3564126d4ace to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase11/NONMEM
## Done.
## 
## 
## The following main elements were parsed successfully:
##   RawResults
##   Estimation::PopulationEstimates::MLE
##   Estimation::IndividualEstimates::Estimates
##   Estimation::IndividualEstimates::RandomEffects
##   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
## -- Tue Aug 16 16:23:19 2016

Load previous results

NM <- LoadSOObject(“NONMEM/UseCase11.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.
Results from NONMEM should be comparable with results from MONOLIX

parameters_nm <- getPopulationParameters(NM, what="estimates")
print(parameters_nm)
## $MLE
## POP_BASECOUNT      POP_BETA     PPV_EVENT 
##     10.354800      0.495869      0.039625
print(parameters_mlx)
##      POP_BETA POP_BASECOUNT     PPV_EVENT 
##       0.50175      10.00776       0.03938
print(getEstimationInfo(NM))
## $OFMeasures
## $OFMeasures$Deviance
## $OFMeasures$Deviance[[1]]
## [1] 3003.271
## 
## 
## 
## $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


There is a bug in the generation of xpose database

#nm.xpdb<-as.xpdb(NM,datafile)
#

Perform some basic goodness of fit

#print(parm.hist(nm.xpdb, inclZeroWRES=TRUE))
#

Export graphs to a PDF file

#pdf("GOF_NM.pdf")
# print(parm.hist(nm.xpdb, inclZeroWRES=TRUE))
#dev.off()

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 <- "foce"

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.FOCE <- paste0(uc,"_FOCE.mdl")
writeMogObj(myNewerMOG,mdlfile.FOCE)

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.FOCE <- estimate(mdlfile.FOCE, target="PsN", subfolder="NONMEM_FOCE")
## -- Tue Aug 16 16:23:26 2016
## New
## Submitted
## Job 631bf8ed-da33-44e3-b6fc-ddfdb9752b1d progress:
## Running [ .. ]
## Importing Results
## Copying the result data back to the local machine for job ID 631bf8ed-da33-44e3-b6fc-ddfdb9752b1d...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job35645f850e1 to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase11/NONMEM_FOCE
## Done.
## 
## 
## The following main elements were parsed successfully:
##   RawResults
##   Estimation::PopulationEstimates::MLE
##   Estimation::IndividualEstimates::Estimates
##   Estimation::IndividualEstimates::RandomEffects
##   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: 4.1
##  nmoutput2so_version: This SOBlock was created with nmoutput2so version 4.5.27
## 
## Completed
## -- Tue Aug 16 16:24:08 2016

The ddmore “LoadSOObj” reads and parsed existing Standardise Output Objects

NM.FOCE <- LoadSOObject(“NONMEM_FOCE/UseCase11_FOCE.SO.xml”)

Results from NONMEM should be comparable to previous results

getPopulationParameters(NM.FOCE,  what="estimates")$MLE
## POP_BASECOUNT      POP_BETA     PPV_EVENT 
##    10.0443000     0.5011100     0.0395245
parameters_nm
## $MLE
## POP_BASECOUNT      POP_BETA     PPV_EVENT 
##     10.354800      0.495869      0.039625
parameters_mlx
##      POP_BETA POP_BASECOUNT     PPV_EVENT 
##       0.50175      10.00776       0.03938

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.FOCE, samples=20, seed=12345,
        bootstrapOptions=" -no-skip_minimization_terminated -threads=2",
        subfolder="Bootstrap", plot=TRUE)
## -- Tue Aug 16 16:24:08 2016
## New
## Submitted
## Job 1ea3ec33-3de4-455e-ab08-96f603b56110 progress:
## Running [ .... ]
## Importing Results
## Copying the result data back to the local machine for job ID 1ea3ec33-3de4-455e-ab08-96f603b56110...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job356445203c04 to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase11/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::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: 4.1
##  nmoutput2so_version: This SOBlock was created with nmoutput2so version 4.5.27
## 
## Completed
## -- Tue Aug 16 16:25:30 2016
## [[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/UseCase11.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,"_FOCE.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.

getPopulationParameters(bootstrapResults, what="estimates")
## $MLE
## POP_BASECOUNT      POP_BETA     PPV_EVENT 
##    10.0443000     0.5011100     0.0395245 
## 
## $Bootstrap
##                   Parameter        Mean     Median
## POP_BASECOUNT POP_BASECOUNT 10.05172000 10.0617500
## POP_BETA           POP_BETA  0.50087290  0.5004300
## PPV_EVENT         PPV_EVENT  0.03998502  0.0385686

Extract the information regarding the precision intervals

getPopulationParameters(bootstrapResults,what="intervals")$Bootstrap
## Warning in getMLEPopulationParameters(SOObject, what = what): Tried to fetch the parameter interval values, however section Estimation::PrecisionPopulationEstimates::MLE::AsymptoticCI was not found in the SO Object.
##  Omitting interval values for MLE section in returned output.
##       Parameter        Mean     Median     Perc_5     Perc_95
## 1 POP_BASECOUNT 10.05172000 10.0617500 9.37975200 10.70909000
## 2      POP_BETA  0.50087290  0.5004300 0.49675090  0.50550740
## 3     PPV_EVENT  0.03998502  0.0385686 0.03162865  0.05334042

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.FOCE, what="estimates",block='structural')$MLE
variabilityPar <- getPopulationParameters(NM.FOCE, 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.

myParObjUpdated@STRUCTURAL
## $POP_BASECOUNT
## $POP_BASECOUNT$value
## [1] "10.0443"
## 
## $POP_BASECOUNT$lo
## [1] "0"
## 
## 
## $POP_BETA
## $POP_BETA$value
## [1] "0.50111"
## 
## $POP_BETA$lo
## [1] "0"
## 
## $POP_BETA$hi
## [1] "10"
myParObjUpdated@VARIABILITY
## $PPV_EVENT
## $PPV_EVENT$value
## [1] "0.0395245"
## 
## $PPV_EVENT$type
## [1] "var"

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. CP is used as idv to perform a VPC

vpcFiles_CP <- VPC.PsN(mdlfile.VPC,samples=20, seed=12345,
        vpcOptions ="-n_simulation=10 -flip_comments -idv=CP",
        subfolder="VPC_CP", plot=TRUE) 
## -- Tue Aug 16 16:25:42 2016
## New
## Submitted
## Job 8b1ceb51-9682-4dca-b0aa-eb35e77f5e0f progress:
## Running [ ..... ]
## Importing Results
## Copying the result data back to the local machine for job ID 8b1ceb51-9682-4dca-b0aa-eb35e77f5e0f...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpYF4SFO\DDMORE.job356439317e3a to C:/SEE/MDL_IDE/workspace/UseCasesDemo/UseCase11/VPC_CP
## Done.
## 
## 
## The following main elements were parsed successfully:
##   RawResults
## 
## The following MESSAGEs were raised during the job execution:
##  nmoutput2so_version: This SOBlock was created with nmoutput2so version 4.5.27
## 
## Completed
## -- Tue Aug 16 16:27:24 2016

plot of chunk unnamed-chunk-38

To replay the visualisation using information from the VPC SO file

pdf(paste0(uc,"_VPC.pdf"))
  xpose.VPC(vpc.info=file.path("./VPC_CP", vpcFiles_CP@RawResults@DataFiles$PsN_VPC_results$path),
        vpctab=file.path("./VPC_CP", vpcFiles_CP@RawResults@DataFiles$PsN_VPC_vpctab$path),
        main="",
        logy=TRUE)
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

p <- getPopulationParameters(NM.FOCE, what="estimates")$MLE

Parameter values used in simulation

print(p)
## POP_BASECOUNT      POP_BETA     PPV_EVENT 
##    10.0443000     0.5011100     0.0395245

Simulate PK parameters for individuals

ind <- list(name = c('BASECOUNT','BETA'))

Simulate Poisson intensity (LAMBDA) and observed counts (Y)

f   <- list( name = c('LAMBDA'), time = 0:2)
y   <- list( name = c('Y'), time = 0:2)

Simulate 20 subjects

g <- list( size = 20, level = 'longitudinal')

Call simulx

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

Simulated parameter values for each individual

print(res$parameter)
##    id BASECOUNT    BETA
## 1   1  10.42705 0.50111
## 2   2  10.42705 0.50111
## 3   3  10.42705 0.50111
## 4   4  10.42705 0.50111
## 5   5  10.42705 0.50111
## 6   6  10.42705 0.50111
## 7   7  10.42705 0.50111
## 8   8  10.42705 0.50111
## 9   9  10.42705 0.50111
## 10 10  10.42705 0.50111
## 11 11  10.42705 0.50111
## 12 12  10.42705 0.50111
## 13 13  10.42705 0.50111
## 14 14  10.42705 0.50111
## 15 15  10.42705 0.50111
## 16 16  10.42705 0.50111
## 17 17  10.42705 0.50111
## 18 18  10.42705 0.50111
## 19 19  10.42705 0.50111
## 20 20  10.42705 0.50111

Plot simulated results

print(ggplotmlx(aes(x=time, y=LAMBDA), data=res$LAMBDA) + geom_line(size=1) +
                geom_point(aes(x=time, y=Y, color = id), data=res$Y, color="red") + ylab("") )

plot of chunk unnamed-chunk-48