This example script is intended to to illustrate Bayesian analysis with BUGS based on example334.mdl following prior specification in the BayesianModels_v1.1.doc section 3.3.4.
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 <- "example334.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" "example334.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:59:56 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 bdd042bf-5b05-463c-8fc5-17c74f5f19ba progress:
## Running [ ... ]
## Importing Results
## Copying the result data back to the local machine for job ID bdd042bf-5b05-463c-8fc5-17c74f5f19ba...
## From C:\Users\smith_mk\AppData\Local\Temp\4\RtmpWk83qr\DDMORE.job87b438ef6c46 to C:/SEE/MDL_IDE/workspace/UseCasesDemo/models/Priors/example334/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:01:01 2016
# BUGS <- LoadSOObject(file.path("WinBUGS","InstallationTest_DEQ_BUGS.SO.xml"))
getPopulationParameters(BUGS)$Bayesian
## Parameter Mean Median SDP Perc_2.5
## 1 invOMEGA_P[1,1] 13.45413320 12.800000 4.820904e+00 5.8886000
## 2 invOMEGA_P[1,2] 1.93154131 1.789000 3.427965e+00 -4.4058750
## 3 invOMEGA_P[2,1] 1.93154131 1.789000 3.427965e+00 -4.4058750
## 4 invOMEGA_P[2,2] 14.00666140 13.410000 4.948636e+00 6.0191000
## 5 lPOP_K -2.29782480 -2.299000 8.955263e-02 -2.4780000
## 6 lPOP_V 2.09350920 2.096000 8.455673e-02 1.9250000
## 7 OMEGA_P[1,1] 0.09315982 0.084740 3.896494e-02 0.0431800
## 8 OMEGA_P[1,2] -0.01313871 -0.011235 2.621735e-02 -0.0711330
## 9 OMEGA_P[2,1] -0.01313871 -0.011235 2.621735e-02 -0.0711330
## 10 OMEGA_P[2,2] 0.08943334 0.080980 3.788604e-02 0.0426745
## 11 POP_K 0.10088147 0.100350 9.061002e-03 0.0839000
## 12 POP_T 6.36920960 6.219500 1.427347e+00 4.0530000
## 13 POP_V 8.14236500 8.136000 6.886007e-01 6.8550500
## 14 TAU_T 92.75927152 59.150000 1.020662e+02 2.5952500
## Perc_5 Perc_25 Perc_50 Perc_75 Perc_95 Perc_97.5
## 1 6.7781000 10.0225000 12.800000 16.24750000 22.469500 24.7692500
## 2 -3.3989500 -0.3531250 1.789000 4.07300000 7.714950 8.9418750
## 3 -3.3989500 -0.3531250 1.789000 4.07300000 7.714950 8.9418750
## 4 7.0030000 10.3825000 13.410000 16.97000000 23.210000 25.2000000
## 5 -2.4480000 -2.3570000 -2.299000 -2.24000000 -2.148000 -2.1130750
## 6 1.9500000 2.0380000 2.096000 2.14900000 2.226000 2.2589750
## 7 0.0475800 0.0665925 0.084740 0.11020000 0.164400 0.1905975
## 8 -0.0569295 -0.0263900 -0.011235 0.00223775 0.024119 0.0347045
## 9 -0.0569295 -0.0263900 -0.011235 0.00223775 0.024119 0.0347045
## 10 0.0468910 0.0637250 0.080980 0.10560000 0.161585 0.1837000
## 11 0.0864300 0.0947100 0.100350 0.10640000 0.116800 0.1208900
## 12 4.3281000 5.3660000 6.219500 7.23100000 8.943300 9.5630000
## 13 7.0280000 7.6772500 8.136000 8.57275000 9.265950 9.5746250
## 14 3.9220000 20.9050000 59.150000 127.17500000 306.895000 386.6950000
getPopulationParameters(BUGS, what="estimates")$Bayesian # o other options, what= precisions, intervals, all
## Parameter Mean Median
## invOMEGA_P[1,1] invOMEGA_P[1,1] 13.45413320 12.800000
## invOMEGA_P[1,2] invOMEGA_P[1,2] 1.93154131 1.789000
## invOMEGA_P[2,1] invOMEGA_P[2,1] 1.93154131 1.789000
## invOMEGA_P[2,2] invOMEGA_P[2,2] 14.00666140 13.410000
## lPOP_K lPOP_K -2.29782480 -2.299000
## lPOP_V lPOP_V 2.09350920 2.096000
## OMEGA_P[1,1] OMEGA_P[1,1] 0.09315982 0.084740
## OMEGA_P[1,2] OMEGA_P[1,2] -0.01313871 -0.011235
## OMEGA_P[2,1] OMEGA_P[2,1] -0.01313871 -0.011235
## OMEGA_P[2,2] OMEGA_P[2,2] 0.08943334 0.080980
## POP_K POP_K 0.10088147 0.100350
## POP_T POP_T 6.36920960 6.219500
## POP_V POP_V 8.14236500 8.136000
## TAU_T TAU_T 92.75927152 59.150000
# 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","OMEGA_P[1,1]","OMEGA_P[2,2]","OMEGA_P[1,2]")
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.10088 0.009061 0.0001281 0.0003175
## POP_V 8.14236 0.688601 0.0097383 0.0229230
## OMEGA_P[1,1] 0.09316 0.038965 0.0005510 0.0006483
## OMEGA_P[2,2] 0.08943 0.037886 0.0005358 0.0006515
## OMEGA_P[1,2] -0.01314 0.026217 0.0003708 0.0004655
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## POP_K 0.08390 0.09471 0.10035 0.106400 0.1205
## POP_V 6.85695 7.67775 8.13600 8.572250 9.5604
## OMEGA_P[1,1] 0.04318 0.06660 0.08474 0.110200 0.1905
## OMEGA_P[2,2] 0.04285 0.06373 0.08098 0.105600 0.1837
## OMEGA_P[1,2] -0.07087 -0.02639 -0.01124 0.002235 0.0345
## 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)