Chapter 3 Exercises
HIV test
Solutions

1. Referring to Example 3.1.1. write BUGS code to compute the probability that someone testing positive actually has HIV. (Don't just write out the formula in Example 3.1.1, but use BUGS to perform Bayesian inference on the required parameter, given the observed data, as in Section 3.3)

One way to implement this problem in BUGS is as follows...

model {
B <- 1 # observed data: person of unknown status has tested positive
######
A ~ dbern(0.001) # 0/1 indicator for whether the person has HIV - the parameter of interest.
B ~ dbern(p)
p <- 0.95*A + 0.02*(1 - A) # probability that B = 1
}

   node   mean   sd   MC error   2.5%   median   97.5%   start   sample
   A   0.04607   0.2096   6.373E-4   0.0   0.0   1.0   1   100000
   
The posterior mean of A gives the required probability.

2. Suppose the individual in question has another two tests, one positive and one negative. By extending the code from question 1, compute the probability that they have HIV, assuming conditional independence (given the disease state) between test results.

model {
A ~ dbern(0.001)
for (i in 1:3) {
B[i] ~ dbern(p)
}
p <- 0.95*A + 0.02*(1 - A)
}

list(B = c(1,1,0))

   node   mean   sd   MC error   2.5%   median   97.5%   start   sample
   A   0.1042   0.3055   9.435E-4   0.0   0.0   1.0   1   100000

3. What if all three tests had been positive? How does this compare with what you might have expected the result to be before running the analysis, and why?

model {
A ~ dbern(0.001)
for (i in 1:3) {
B[i] ~ dbern(p)
}
p <- 0.95*A + 0.02*(1 - A)
}

list(B = c(1,1,1))

   node   mean   sd   MC error   2.5%   median   97.5%   start   sample
   A   0.9904   0.09736   3.105E-4   1.0   1.0   1.0   1   100000

One might naturally have expected a lower probability, given the result for Q2, but the negative test result in Q2 had a strong influence (two positive tests alone would have given a probability of roughly 0.7).