Tuesday, July 19, 2011

Appendix to Advanced Bayesian Methods for Medical Test Accuracy

The code listed below can be copied and pasted to the WinBUGS worksheet and executed according to the instructions below.

The following will introduce the fundamentals of executing a Bayesian analysis with WinBUGS.
Consider the following example of an exercises stress test given by the 2 by 2 table below.

 Exercise Stress Test and Heart Disease

                                                                             CAD
EST
D = 0
D = 1
X = 0
327
208
X = 1
115
818



This information is contained in the first list statement of the following WinBUGS code, where a 1 is added to each cell frequency. This is in effect assumes a uniform prior is appropiate for the analysis.The Document below appears in the WinBUGS worksheeet.




# Measures of accuracy
# Binary Scores
Model;
{
# Dirichlet distribution for cell probabilities
g00~dgamma(a00,2)
g01~dgamma(a01,2)
g10~dgamma(a10,2)
g11~dgamma(a11,2)

h<-g00+g01+g10+g11
# the theta have a Dirichlet distribution
theta00<-g00/h
theta01<-g01/h
theta10<-g10/h
theta11<-g11/h
# the basic test accuracies are below
tpf<-theta11/(theta11+theta01)
se<-tpf
sp<-1-fpf
fpf<-theta10/(theta10+theta00)
tnf<-theta00/(theta00+theta10)
fnf<-theta01/(theta01+theta11)
ppv<-theta11/(theta10+theta11)
npv<-theta00/(theta00+theta01)
pdlr<-tpf/fpf
ndlr<-fnf/tnf

}

# Exercise Stress Test Pepe
# Uniform Prior (add one to each cell of the table frequencies !)
list(a00=328,a01=209,a10=116,a11=819)
# initial values
list(g00=1,g01=1,g10=1,g11=1)




In order to execute the analysis, the following sequence should be performed. The analysis will consisit of estimating the test accuracy with the true and false positive fractions and the postive and negative predictive values. 65,000 observations will be generated from the posterior distributions.
1.left click 'model' of the tool bar then left click on specification. This activates the specification tool.
2.active the word 'model' in the third line of the  code and click on the check model button of the specifcation tool.
3. activate the word 'list' from the first list statement of the code and click on the load data button of the specification tool.
4. click on the compile button of the specification tool.
5.click on the word 'list' of the second list statement of the code. This contains the inital values for the MCMC simulation.
6. click on the inference menu of the tool bar and click on sample monitor tool
7.in the node box type the word tpf, then click on set button of the sample monitor tool
8. in the node box, type fpf then click on the set button
9. type the word ppv in the node box and click on the set button
10. type he word npv in the node box then click on the set button
11.type * in the node box
12.type 5000 in the beg box of the sample monitor tool. The simulation is initiated with 5000 observations that are ignored for the posterior analysis.
13. click on the word model of the tool bar then click on the word update. This activates the update tool bar
14.type 70000 in the updates box and type 100 in the refresh box

You are now ready to execute the analysis!

15. click on the update box of the update tool bar. This initiates the simulation of 65000 observations from the posterior distribution of the four parameters.

You are now ready to veiw the output ( the posterior analysis of the simulation)

16. In the sample monitor tool click on the word stats and you will see the output below. Note that .7967 is the mean of the posterior distribution of the true positive fraction tpf. The error column is the simulation error for estimating the tpf with 65000 observations. Note also the posterior distribution of the fpf has a mean and standard deviation of .2612 and .0208 respectively and the 95% credible interval is (.2215,.3033).For additional information refer to the appendix of the book.


Parameter
Mean
SD
Error
Lower 2 1/2
Median
Upper 2 1/2
TPF
.7967
.0125
<.00001
.7716
.7968
.8208
FPF
.2612
.0208
<.00001
.2215
.2608
.3033






1 comment: