trace tracker logo

Probability of false positives

This utility calculates the expected probability of numbers of false positive results to a test for a given sample size and test specificity, assuming the sample is from an uninfected population. It answers the question: 'For a given sample size and test specificity, and assuming that all animals are uninfected, what is the probability that there will be 1, 2, 3 etc false positives ?'. It also answers the related questions: 'What is the probability of x (1, 2, 3, etc) or more false positive results? and What is the probability of x (1, 2, 3, etc) or less false positive results?'

Inputs are the sample size tested and the test specificity.

Outputs are a table and graph of probabilities associated with each of the likely number of false positive results.

No results

No example available
No references available
				######################################
# Program to calculate numbers of false positives
######################################
 rm(list = ls())


# check version and load header script
test<- ifelse(length(commandArgs()) < 3, TRUE, FALSE)
fpath<- ifelse(test, "webRootUrl", "rtoolsPath")

# load header scripts
  source(paste(fpath, "R/epi_head.R", sep = ""))
  source(paste(fpath, "R/HTMLStream.R", sep = ""))
  source(paste(fpath, "R/epitools_functions.r", sep = ""))


# extract command arguments
    a1<- type.convert(a0[8:9])


# cat(a0)
# cat(a1)
digits<- 4
p<- a1[1]
n<- a1[2]
names(a1)<- c("Probability of true result (specificity)", "Sample size")
filename <- digest(Sys.time())
tmp.path<- paste(fpath, "tmp/", sep = "")
tmp.file<- paste(fpath, "tmp/", filename, sep = "")
sinkfile<- paste(fpath, "tmp/", filename, ".txt", sep="")   # fpath,
graphfile<- paste(fpath, "tmp/", filename, ".png", sep="")

# table of inputs
inputs<- array("", dim = c(length(a1), 1))
inputs[1:2, 1]<- a1
rownames(inputs)<- names(a1)

results<- array(0, dim = c(n+1, 4))
colnames(results)<- c("Number of false positives (x)", "P(pos=x)", "P(pos<= x)", "P(pos>=x)")
results1<- results
results[,1]<- 0:n
results[,2]<- dbinom(0:n, n, (1-p))
results[1,3]<- results[1,2]
results[1,4]<- 1
for(i in 1:n+1) {
  results[i,3]<- results[i-1,3] + results[i,2]
  results[i,4]<- results[i-1,4] - results[i-1,2]
}
results<- round(results, digits)
results1[,2:4]<- format(results[,2:4], scientific = F)
results1[,1]<- results[,1]
max<- match(0, results[,4])+1

# graph results
    Title<-"Probability of varying numbers of false +ve"
    OpenGraphOutput(graphfile, pointsize = 12, ht = 6, wd = 8)
    cols<- c("darkblue", "darkgreen", "red", "magenta", "brown", "purple")
    plot(x = results[1:max,1], y = results[1:max,2], ylim = c(0,1), xlab = colnames(results)[1],
                ylab= "Probability", main=Title, col = cols[1], type = "o", pch=3)
    lines(x = results[1:max,1], y = results[1:max,3], col = cols[2], type = "o", pch=8)
    lines(x = results[1:max,1], y = results[1:max,4], col = cols[3], type = "o", pch=4)
    legend("right", colnames(results)[2:4], col = cols[1:3], pch = c(3, 8, 4), cex = 0.8)
sink(sinkfile)
    CloseGraphOutput("R")
sink()

# write to html and file
heading<- "Probability of false positives"
subheadings<- ""
tmp.file<- paste(fpath, "tmp/", filename, sep = "")
output<- html.output(heading, subheadings = "", inputs, results = list(results1[1:max,]), graphs = graphfile, graph.headings = "Distribution of false positives", show.inputs = T, show.graphs = T, tmp.file, result.txt = "") 
write.html(output, tmp.file)
cat(output)