Prior example3311 test

This example script is intended to to illustrate Bayesian analysis with BUGS based on example3311.mdl following prior specification in the BayesianModels_v1.1.doc section 3.3.1.1.

The following steps are implemented in this workflow:

Prepared by Mike K Smith, Pfizer Ltd, Sandwich, UK 04 August 2016

Initialisation

Clear workspace and set working directory

rm(list=ls(all=F))
getwd()
## [1] "C:/SEE/MDL_IDE/workspace/UseCasesDemo/scripts/Priors"

Some useful functions for working with the SEE and workspaces in the MDL-IDE A system variable has been set which retrieves the directory of the workspaces

Sys.getenv("MDLIDE_WORKSPACE_HOME")
## [1] "C:\\SEE\\MDL_IDE\\workspace"

By typing “~” you can get to this directory

mydir <- file.path("~/UseCasesDemo/models/Priors")
setwd(mydir)

Another function from the ddmore R package retrieves the directory that the SEE is installed in. You can then navigate relative to these directories.

ddmore:::DDMORE.checkConfiguration()
## [1] "C:/SEE"

Set name of .mdl file

mdlfile <- "example3311.mdl"
model <- tools::file_path_sans_ext(mdlfile)
datafile <- "Testdata.csv"

wd <- file.path(mydir,model)
dir.create(wd)

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

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

Set the working directory.

setwd(wd)

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 = wd) 
cache.path <- file.path(wd, 'cache')
dir.create(cache.path)
opts_chunk$set(cache.path=cache.path)
figure.path <- file.path(wd, 'figure')
dir.create(figure.path)
opts_chunk$set(figure.path=figure.path)

List files available in working directory

list.files()
## [1] "cache"           "example3311.mdl" "figure"          "Testdata.csv"

Read the different MDL objects needed for upcoming tasks. An MDL file may contain more than one object of any type (though typically only one model!) so these functions return a list of all objects of that type found in the target MDL file. To pick out the first, use the double square bracket notation in R to retrieve the first item of the list.

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

Exploratory Data Analysis

getDataObjects reads and parses the MDL. It doesn't actually read the data file. To do so, use the ddmore function readDataObj() to create an R object (data frame) based on the MDL data object. This uses data column headers defined in the Data Object.

myData <- readDataObj(myDataObj)

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

head(myData)
##   ID AMT TIME    DV MDV
## 1  1 100    0    NA   1
## 2  1  NA    1 9.436   0
## 3  1  NA    2 8.214   0
## 4  1  NA    4 7.945   0
## 5  1  NA    8 4.978   0
## 6  1  NA   12 3.853   0
myEDAData <- myData[!is.na(myData$DV),]

library(ggplot2)

Plot the data using ggplot2 library

plot1 <- ggplot(aes(y=DV,x=TIME, group=ID), data=myEDAData) +
                geom_line()+
                geom_point() + 
                labs(y="Concentration" ,x="Time (hours)")
print(plot1)

plot of chunk ExploratoryAnalysis

Model Development

ESTIMATE model parameters in WINBUGS

Assembling the new MOG for Winbugs. Note that we reuse the data and model. Two main changes are made: selecting the appropriate Task Properties Object for BUGS and exchanging the Prior Object instead of the Parameter Object.

# BUGS <- estimate(mdlfile, target="winbugs", subfolder="WinBUGS", preprocessSteps=list(prepareWinbugsDs))

Alternatively the runWinBUGS function is provided with the ddmore R package to allow the user some additional control over BUGS execution.

 BUGS<-runWinBUGS(mdlfile,subfolder="WinBUGS")
## -- Fri Aug 19 15:55:46 2016
## New
## Warning in NMTRAN2BUGSdataconverter(model = model.pharmml): rate column is
## missing. rate will be set to 0 by default.
## Warning in NMTRAN2BUGSdataconverter(model = model.pharmml): ii column is
## missing. Default 0 values will be set.
## Warning in NMTRAN2BUGSdataconverter(model = model.pharmml): cmt column is
## missing. cmt will be set to 1 by default.
## Warning in NMTRAN2BUGSdataconverter(model = model.pharmml): cmt column is
## missing. cmt will be set to 1 by default.
## Warning in NMTRAN2BUGSdataconverter(model = model.pharmml): addl column is
## missing. addl will be set to 0 by default.
## Warning in NMTRAN2BUGSdataconverter(model = model.pharmml): ss column is
## missing. ss will be set to 0 by default.
## Submitted
## Job 9a84765e-ebbf-4a1f-b3aa-179e6d75a249 progress:
## Running [ ... ]
## Importing Results
## Copying the result data back to the local machine for job ID 9a84765e-ebbf-4a1f-b3aa-179e6d75a249...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpWk83qr\DDMORE.job87b4313632cf to C:/SEE/MDL_IDE/workspace/UseCasesDemo/models/Priors/example3311/WinBUGS
## Done.
## 
## 
## The following main elements were parsed successfully:
##   RawResults
##   Estimation::PopulationEstimates::Bayesian
##   Estimation::PrecisionPopulationEstimates::Bayesian
##   Estimation::IndividualEstimates::Estimates
##   Estimation::PrecisionIndividualEstimates::StandardDeviation
##   Estimation::PrecisionIndividualEstimates::EstimatesDistribution
##   Estimation::PrecisionIndividualEstimates::PercentilesCI
## 
## The following MESSAGEs were raised during the job execution:
##  winbugs_message: success
## 
## Completed
## -- Fri Aug 19 15:56:51 2016
# BUGS <- LoadSOObject(file.path("WinBUGS","InstallationTest_DEQ_BUGS.SO.xml"))

getPopulationParameters(BUGS)$Bayesian
##   Parameter       Mean    Median         SDP    Perc_2.5    Perc_5
## 1   OMEGA_K 0.01117005 0.0095555 0.006764536 0.003184100 0.0038261
## 2   OMEGA_T 0.06639854 0.0155000 0.166355988 0.002734075 0.0033261
## 3   OMEGA_V 0.01273579 0.0114400 0.006034850 0.005387100 0.0060372
## 4     POP_K 0.10100261 0.1009000 0.003614639 0.094020250 0.0950500
## 5     POP_T 6.15537840 6.0055000 1.485954876 3.790050000 4.1470000
## 6     POP_V 8.09041220 8.0870000 0.210791269 7.674000000 7.7430500
##      Perc_25   Perc_50  Perc_75  Perc_95  Perc_97.5
## 1 0.00656000 0.0095555 0.013930 0.023788 0.02878925
## 2 0.00700275 0.0155000 0.049295 0.310250 0.49738250
## 3 0.00861425 0.0114400 0.015220 0.023870 0.02799950
## 4 0.09862000 0.1009000 0.103200 0.107100 0.10860000
## 5 5.15700000 6.0055000 6.928750 8.680750 9.44555000
## 6 7.95800000 8.0870000 8.222000 8.441950 8.51295000
getPopulationParameters(BUGS, what="estimates")$Bayesian # o other options,  what= precisions, intervals, all
##         Parameter       Mean    Median
## OMEGA_K   OMEGA_K 0.01117005 0.0095555
## OMEGA_T   OMEGA_T 0.06639854 0.0155000
## OMEGA_V   OMEGA_V 0.01273579 0.0114400
## POP_K       POP_K 0.10100261 0.1009000
## POP_T       POP_T 6.15537840 6.0055000
## POP_V       POP_V 8.09041220 8.0870000
# it is possible also to omit $Bayesian

Here we use the coda package to examine and assess MCMC convergence.

library(coda)

By default the parameters monitored by BUGS are all parameters specified with random variable distributions in the PRIOR_VARIABLE_DEFINITION. In this example these are specified on the log scale. The following code transforms them back to the natural scale. Note that in the public release, MCMC nodes for monitoring will be specified via the Task Properties Object.

parameters_BUGS<- getPopulationParameters(BUGS, what="estimates")$Bayesian

How to retrieve parameter names from the Task Properties Object

#estPars <- getTaskPropertiesObjects(mdlfile)[[1]]@ESTIMATE$blocks[[1]]$TARGET_SETTINGS$parameters
#estPars <- gsub("\\\"","", estPars)
#estPars <- gsub(" ", "", estPars)
#estPars <- unlist(strsplit(estPars, split=","))

Since there are very few parameters and they are consistent across the Priors example models:

estPars <- c("POP_K","POP_V","POP_T","OMEGA_K","OMEGA_V","OMEGA_T")

coda MCMC trace and density plots

#to read only one chain
BUGSoutputPath <- file.path(getwd(),"WinBUGS")
coda_out <- read.coda(output.file=file.path(BUGSoutputPath,"output1.txt"), index.file=file.path(BUGSoutputPath,"outputIndex.txt"), quiet=T)
coda_pars <- coda_out[,estPars]
#to read more chains (3 in this case) use read.coda.interactive()
#coda_out <- read.coda.interactive()
# Enter in the order:
  #outputIndex.txt
  #output1.txt  
  #output2.txt  
  #output2.txt
## summary statistics
summary(coda_pars)
## 
## Iterations = 1001:6000
## Thinning interval = 1 
## Number of chains = 1 
## Sample size per chain = 5000 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##            Mean       SD  Naive SE Time-series SE
## POP_K   0.10100 0.003615 5.112e-05      0.0002535
## POP_V   8.09041 0.210791 2.981e-03      0.0299881
## POP_T   6.15538 1.485955 2.101e-02      0.0729932
## OMEGA_K 0.01117 0.006765 9.566e-05      0.0002866
## OMEGA_V 0.01274 0.006035 8.535e-05      0.0001719
## OMEGA_T 0.06640 0.166356 2.353e-03      0.0180479
## 
## 2. Quantiles for each variable:
## 
##             2.5%      25%      50%     75%   97.5%
## POP_K   0.094030 0.098620 0.100900 0.10320 0.10860
## POP_V   7.674000 7.958000 8.087000 8.22200 8.51105
## POP_T   3.791950 5.157000 6.005500 6.92825 9.42845
## OMEGA_K 0.003188 0.006560 0.009555 0.01393 0.02876
## OMEGA_V 0.005391 0.008615 0.011440 0.01522 0.02798
## OMEGA_T 0.002737 0.007010 0.015500 0.04928 0.49672
## trace and density plots
plot(coda_pars, ask=F)

plot of chunk MCMC_Diagnostics plot of chunk MCMC_Diagnostics

## gelman-rubin diagnostics (if at least 2 chains)
#gelman.diag(coda_out)
#gelman.plot(coda_out)