## intgerpretation of diagnostic test result
##...given
## DFR frequency of the disease in the population
## FP false positive rate of the test
## FN false negative rate of the test
##...what is the interpretation of a positive test result?
## NS sample size of an epidemiological study
DFR <-0.0001
FP <- 0.02
FN <- 0.04
NS <- 1000000000
########### pick NS individuals; how many have the disease?
set.seed(555)
ND <- rbinom(1,NS,DFR)
NH <- NS-ND
########### run the entire sample through the diagnostic test
# notation:
# NFN number of false negative test results
# NTP number of true positive test results
# NFP number of false positive test results
# NTN number of true negative test results
#### run the ND diseased individuals
NFN <- rbinom(1,ND,FN)
NTP <- ND-NFN
### run the NH healthy individuals
NFP <- rbinom(1,NH,FP)
NTN <- NH-NFP
################## of the individuals who tested poisitive,
################## how many have the disease? NTP
################# What fraction is this of all those who tested positive?
R <- NTP/(NTP+NFP)
print("Fraction of the positive tests who have the disease=")
print(R)
############### what fraction of all the people who took the test
############### have the disease
RT <- (NTP+NFN)/(NTP+NFP+NTN+NFN)
print("Fraction of the entire sample who have the disease=")
print(RT)
################### BAYES FORMULA ###############
## ( X suffix indicates "expected"
NFNX <- NS*DFR*FN
NTPX <- NS*DFR*(1.0-FN)
NFPX <- NS*(1.0-DFR)*FP
NTNX <- NS*(1.0-DFR)*(1.0-FP)
RB <- NTPX/(NTPX+NFPX)
print("Bayes posterior probability of positive tests having the disease=")
print(RB)