This example script is intended to to illustrate Bayesian analysis with BUGS based on example335.mdl following prior specification in the BayesianModels_v1.1.doc section 3.3.5.
The following steps are implemented in this workflow:
Prepared by Mike K Smith, Pfizer Ltd, Sandwich, UK 04 August 2016
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 <- "example335.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" "example335.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]]
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)
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 16:01:24 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 8dac64a3-1df8-4a76-9c48-d9ca840ffa3c progress:
## Running [ ... ]
## Importing Results
## Copying the result data back to the local machine for job ID 8dac64a3-1df8-4a76-9c48-d9ca840ffa3c...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpWk83qr\DDMORE.job87b42009611b to C:/SEE/MDL_IDE/workspace/UseCasesDemo/models/Priors/example335/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 16:02:28 2016
# BUGS <- LoadSOObject(file.path("WinBUGS","InstallationTest_DEQ_BUGS.SO.xml"))
getPopulationParameters(BUGS)$Bayesian
## Parameter Mean Median SDP Perc_2.5 Perc_5
## 1 lPOP_P[1] -2.29371160 -2.295000 0.037705914 -2.365000000 -2.35400000
## 2 lPOP_P[2] 2.09071320 2.091000 0.027837750 2.035000000 2.04400000
## 3 OMEGA_K 0.01173349 0.010120 0.007056080 0.003357225 0.00410120
## 4 OMEGA_T 0.06684953 0.017500 0.156743573 0.002931200 0.00361205
## 5 OMEGA_V 0.01317952 0.011655 0.006482668 0.005434100 0.00611500
## 6 POP_K 0.10096289 0.100800 0.003829281 0.093960250 0.09497000
## 7 POP_T 6.22916140 6.107000 1.388913179 3.907025000 4.22400000
## 8 POP_V 8.09373260 8.090500 0.225691470 7.651000000 7.72500000
## 9 TAU_K 115.55006400 98.810000 68.179076040 33.561500000 40.77250000
## 10 TAU_T 87.02060542 57.135000 93.435166153 2.072000000 3.39935000
## 11 TAU_V 91.71062800 85.795000 39.396607110 33.221250000 38.87250000
## Perc_25 Perc_50 Perc_75 Perc_95 Perc_97.5
## 1 -2.31800000 -2.295000 -2.2720000 -2.2280000 -2.21305000
## 2 2.07300000 2.091000 2.1060000 2.1409500 2.14900000
## 3 0.00688675 0.010120 0.0145275 0.0245285 0.02979850
## 4 0.00817625 0.017500 0.0519400 0.2941700 0.48269750
## 5 0.00884750 0.011655 0.0157775 0.0257190 0.03010875
## 6 0.09849000 0.100800 0.1031000 0.1077000 0.10939500
## 7 5.24400000 6.107000 7.0617500 8.6278500 9.28262500
## 8 7.94825000 8.090500 8.2190000 8.5049000 8.57700000
## 9 68.83750000 98.810000 145.2000000 243.7900000 297.78250000
## 10 19.25000000 57.135000 122.3000000 276.7950000 341.17750000
## 11 63.39750000 85.795000 113.0000000 163.5000000 183.99750000
getPopulationParameters(BUGS, what="estimates")$Bayesian # o other options, what= precisions, intervals, all
## Parameter Mean Median
## lPOP_P[1] lPOP_P[1] -2.29371160 -2.295000
## lPOP_P[2] lPOP_P[2] 2.09071320 2.091000
## OMEGA_K OMEGA_K 0.01173349 0.010120
## OMEGA_T OMEGA_T 0.06684953 0.017500
## OMEGA_V OMEGA_V 0.01317952 0.011655
## POP_K POP_K 0.10096289 0.100800
## POP_T POP_T 6.22916140 6.107000
## POP_V POP_V 8.09373260 8.090500
## TAU_K TAU_K 115.55006400 98.810000
## TAU_T TAU_T 87.02060542 57.135000
## TAU_V TAU_V 91.71062800 85.795000
# 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.10096 0.003829 5.415e-05 0.0003905
## POP_V 8.09373 0.225691 3.192e-03 0.0383985
## POP_T 6.22916 1.388913 1.964e-02 0.0561877
## OMEGA_K 0.01173 0.007056 9.979e-05 0.0003101
## OMEGA_V 0.01318 0.006483 9.168e-05 0.0001718
## OMEGA_T 0.06685 0.156744 2.217e-03 0.0242311
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## POP_K 0.093970 0.098490 0.10080 0.10310 0.10920
## POP_V 7.651000 7.948750 8.09050 8.21900 8.57700
## POP_T 3.907975 5.244000 6.10700 7.06125 9.26837
## OMEGA_K 0.003366 0.006888 0.01012 0.01452 0.02974
## OMEGA_V 0.005438 0.008848 0.01166 0.01577 0.03006
## OMEGA_T 0.002939 0.008179 0.01750 0.05194 0.48260
## trace and density plots
plot(coda_pars, ask=F)
## gelman-rubin diagnostics (if at least 2 chains)
#gelman.diag(coda_out)
#gelman.plot(coda_out)