### Conditional Probability Simulation ### Bayesian Probability # lie detector prev <- 0.50 # prevalence = P(L) sens <- 0.80 # sensitivity = P(+|L) spec <- .90 # specificity = 1 - P(+|L^c) reps <- 10000 true.pos.test.pos <- 0 # counter for lie detector catching a truly lying person test.pos <- 0 # counter for lie detector says person is lying for(i in 1:reps){ # simulate a person as lying or not lie <- 0 if(runif(1) < prev){ lie <- 1 } # if lying simulate if test positive or not if(lie == 1 ){ if(runif(1) <= sens){ test.pos <- test.pos + 1 true.pos.test.pos <- true.pos.test.pos + 1 } } # if not lying simulate if test positive or not else{ if(runif(1) <= (1-spec)) test.pos <- test.pos + 1 } } # simulated probability true.pos.test.pos/test.pos