trace tracker logo

Sample size for freedom with fixed unit sensitivity

Calculate the sample size required to achieve a target population or cluster level sensitivity for a survey.

For these calculations unit specificity is assumed to be 100%. For cluster (herd, flock, etc) level calculations, enter test sensitivity, unit-level design prevalence and required cluster-level sensitivity. Alternatively, for population level calculations enter cluster-level (herd) sensitivity, cluster-level design prevalence and required population-level sensitivity.

Calculations use the hypergeometric approximation if population size is provided, or binomial method if population size is not specified.

Inputs are:

  • Design prevalence as either a proportion or an integer number of units (animals for cluster level sensitivity and clusters for population-level sensitivity);
  • Unit sensitivity (test sensitivity to calculate cluster (herd) sensitivity or cluster (herd) sensitivity tocalculate population sensitivit;
  • Required population-level sensitivity; and
  • Population size (optional if design prevalence is specified as a proportion, required if design prevalence is a number of units). Leave population size blank if not known.

Outputs are:

  • Required sample size for the given target population sensitivity, design prevalence and unit sensitivity; and
  • A table and graph of sample sizess for varying population and design prevalence values and the given target population sensitivity and unit sensitivity.

No results

No example available
No references available
				################################################################
# program to calculate sample size for population freedom
################################################################
# uses RSurveillance package

rm(list = ls())
test<- ifelse(length(commandArgs()) < 3, TRUE, FALSE)
fpath<- ifelse(test, "webRootUrl", "rtoolsPath")
# cat(test)

# 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
# pstar, pstar.int, sens, seh, N (if known),
    a1<- type.convert(a0[8:11])

# initialise variables
pstar<- a1[1]
pstar.int<- ifelse(pstar < 1, F, T)
Sens<- a1[2]
seh<- a1[3]
N<- a1[4]
digits<- 4
filename<- digest(Sys.time)
graphfile<- paste(fpath, "tmp/", filename, ".png", sep="")
sinkfile<- paste(fpath, "tmp/", filename, ".txt", sep="")
sink(sinkfile)

# table of inputs
inputs<- array("", dim = c(length(a1), 1))
inputs[, 1]<- a1
rownames(inputs)<- c("Design prevalence (Pstar)", "Unit (test or cluster) sensitivity", "Required population sensitivity", "Population size (N)")
if (N <= 0) inputs[4, 1]<- "Unknown"
if (pstar.int) inputs[1,1]<- paste(pstar, "units") else inputs[1,1]<- paste(pstar*100,"%", sep = "")
if (N <= 0) N<- NA

n<- ceiling(n.freedom(N, seh, pstar, Sens))
if (is.na(n)) n<- "n > N"
results1<- rbind(inputs, "Sample size (n)" = n)


pstar.levels<- c(0.005, 0.010, 0.020, 0.030, 0.050, 0.1, 0.2, 0.3, 0.5)
N.levels<- c(50, 100, 200, 300, 500, 1000, 5000, 10000, 100000, 1000000)
results2<- array(NA, dim = c(length(N.levels), length(pstar.levels)))
for (j in 1:length(N.levels)) {
  for (i in 1:length(pstar.levels)) {
    results2[j, i]<- n.freedom(N.levels[j], seh, pstar.levels[i], Sens)
  }
}
colnames(results2)<- c(paste("Pstar = ", pstar.levels, sep = ""))
rownames(results2)<- c(paste("N = ", format(N.levels, scientific = F), sep = ""))
n.lines<- length(N.levels)
# cat(test)

# graph results
#    Title<-"Sample size for varying Pstar and population size"
#    OpenGraphOutput(graphfile, pointsize = 12, ht = 6, wd = 8)
#    line.colours<- c("darkblue", "red", "darkgreen", "magenta", "brown", "purple", "black", "blue", "darkred", "tan")
#    plot(x = pstar.levels, y = results2[3,], type="n", ylab = "Sample size", 
#                xlab="Design prevalence (Pstar)", main=Title, col = line.colours[1])
#    for (i in 1:n.lines) {
#        lines(x = pstar.levels, y = results2[i,], type = "l", col = line.colours[i])
#    }
#    legend.txt<- rownames(results2)[1:n.lines]
#    legend("topright", legend.txt, 
#        col = line.colours, lty = c(1, 1), plot = TRUE, cex = 0.7)
#    CloseGraphOutput("R")
sink()
# cat(test)

# add infinite population if appropriate
if (!pstar.int) {
  n.inf<- n.binom(seh, pstar.levels, Sens)
  results2<- rbind(results2, "N = inf" = n.inf)
}
results2[is.na(results2)]<- "n > N"
# write to html and file
heading<- "Sample size for freedom surveys"
subheadings<- c("", paste("Sample size for varying design prevalence and population size (Unit Se = ", Sens, ", Population Se = ", seh, ")", sep = ""))
tmp.file<- paste(fpath, "tmp/", filename, sep = "")
result.txt<- ""
output<- html.output(heading, subheadings, "", results = list(results1, results2), graphs = "", graph.headings = "", show.inputs = F, show.graphs = F, tmp.file, result.txt = result.txt) 
write.html(output, tmp.file)
cat(output)