This example script is intended to to illustrate Bayesian analysis with BUGS based on example331.mdl following prior specification in the BayesianModels_v1.1.doc section 3.3.1.
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 <- "example331.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" "example331.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 15:53:36 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 a8bb8f0b-c69f-4de7-ad09-69ed96a3e38e progress:
## Running [ ... ]
## Importing Results
## Copying the result data back to the local machine for job ID a8bb8f0b-c69f-4de7-ad09-69ed96a3e38e...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpWk83qr\DDMORE.job87b41d9b275b to C:/SEE/MDL_IDE/workspace/UseCasesDemo/models/Priors/example331/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:54:44 2016
# BUGS <- LoadSOObject(file.path("WinBUGS","InstallationTest_DEQ_BUGS.SO.xml"))
getPopulationParameters(BUGS)$Bayesian
## Parameter Mean Median SDP Perc_2.5
## 1 lPOP_K -2.28957760 -2.2900000 0.036303261 -2.359000000
## 2 lPOP_V 2.08899500 2.0880000 0.028958569 2.027000000
## 3 OMEGA_K 0.01149391 0.0098935 0.006777842 0.003207000
## 4 OMEGA_T 0.07352484 0.0222950 0.136353515 0.002905075
## 5 OMEGA_V 0.01308878 0.0117500 0.006441644 0.005236100
## 6 POP_K 0.10137522 0.1013000 0.003675864 0.094540000
## 7 POP_T 6.26579520 6.0925000 1.442113152 3.862025000
## 8 POP_V 8.08009620 8.0730000 0.234024199 7.592000000
## 9 TAU_K 117.37080800 101.1000000 70.891532857 34.324500000
## 10 TAU_T 80.65445990 44.8500000 94.996548971 2.051600000
## Perc_5 Perc_25 Perc_50 Perc_75 Perc_95
## 1 -2.34900000 -2.31475000 -2.2900000 -2.2650000 -2.2290000
## 2 2.04600000 2.07200000 2.0880000 2.1050000 2.1370000
## 3 0.00393530 0.00693825 0.0098935 0.0143100 0.0240195
## 4 0.00359715 0.00891500 0.0222950 0.0684925 0.3490400
## 5 0.00598100 0.00872750 0.0117500 0.0155675 0.0254630
## 6 0.09547000 0.09881250 0.1013000 0.1038000 0.1076000
## 7 4.19710000 5.25875000 6.0925000 7.1157500 8.8228000
## 8 7.73600000 7.93800000 8.0730000 8.2040000 8.4740000
## 9 41.63100000 69.87000000 101.1000000 144.1000000 254.0850000
## 10 2.86550000 14.60000000 44.8500000 112.1750000 277.9900000
## Perc_97.5
## 1 -2.22200000
## 2 2.15200000
## 3 0.02913625
## 4 0.48736000
## 5 0.02979650
## 6 0.10830000
## 7 9.42400000
## 8 8.60300000
## 9 311.89750000
## 10 344.29000000
getPopulationParameters(BUGS, what="estimates")$Bayesian # o other options, what= precisions, intervals, all
## Parameter Mean Median
## lPOP_K lPOP_K -2.28957760 -2.2900000
## lPOP_V lPOP_V 2.08899500 2.0880000
## OMEGA_K OMEGA_K 0.01149391 0.0098935
## OMEGA_T OMEGA_T 0.07352484 0.0222950
## OMEGA_V OMEGA_V 0.01308878 0.0117500
## POP_K POP_K 0.10137522 0.1013000
## POP_T POP_T 6.26579520 6.0925000
## POP_V POP_V 8.08009620 8.0730000
## TAU_K TAU_K 117.37080800 101.1000000
## TAU_T TAU_T 80.65445990 44.8500000
# 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.10138 0.003676 5.198e-05 0.0003401
## POP_V 8.08010 0.234024 3.310e-03 0.0447764
## POP_T 6.26580 1.442113 2.039e-02 0.0631159
## OMEGA_K 0.01149 0.006778 9.585e-05 0.0003085
## OMEGA_V 0.01309 0.006442 9.110e-05 0.0002034
## OMEGA_T 0.07352 0.136354 1.928e-03 0.0181281
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## POP_K 0.094540 0.098818 0.101300 0.10380 0.10830
## POP_V 7.592000 7.938000 8.073000 8.20400 8.60300
## POP_T 3.862975 5.260250 6.092500 7.11525 9.42400
## OMEGA_K 0.003207 0.006939 0.009893 0.01431 0.02899
## OMEGA_V 0.005240 0.008729 0.011750 0.01556 0.02966
## OMEGA_T 0.002908 0.008917 0.022295 0.06848 0.48204
## 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)