# Sample size to achieve target confidence of freedom

Calculate the sample size required to achieve a desired level of confidence of population freedom. Can be applied at the cluster (herd/flock) level or population level. If applied at the cluster level, sensitivity and design prevalence are unit (animal) level values, whereas if applied at the population level, sensitivity and design prevalence are cluster level values.

Inputs are:

• the unit or cluster level sensitivity for the sampling;
• prior confidence of freedom (from previous analyses or opinion);
• probability of introduction of disease during the surveillance period;
• the required level of confidence of freedom after the additional surveillance;
• population size (optional); and
• the design prevalence as either a proportion or an integer number of units/clusters.

Enter test sensitivity if the target is a cluster level freedom or cluster-sensitivity if the target is a system or population level freedom. If population size is entered the hypergeometric approximation is used, otherwise the calculation is based on the binomial distribution.

The main outputs are:

• the adjusted prior confidence of freedom (taking into account the probability of introduction while the surveillance is being undertaken);
• the target cluster or system sensitivity required to achieve the required confidence of freedom; and
• the sample size required to achieve the target cluster or system sensitivity and resulting confidence of freedom.

No example available
No references available
```				################################################################
# program to calculate sample size required to achieve
# desired PFree
################################################################
# uses RSurveillance package

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

source(paste(fpath, "R/HTMLStream.R", sep = ""))
source(paste(fpath, "R/epitools_functions.r", sep = ""))

# extract command arguments
# Sens, Prior, PIntro, Target posterior (PFree.target), population (N)
a1<- type.convert(a0[8:13])

# cat(a1)

# initialise variables
Sens<- a1[1]
prior<- a1[2]
PIntro<- a1[3]
PFree.target<- a1[4]        # upper confidence limit
N<- a1[5]
prev<- a1[6]
prev.int<- ifelse(prev < 1, F, T)
# x<- ceiling(population * prevalence)        # x = number infected
digits<- 3

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("Test/cluster sensitivity", "Prior confidence of freedom", "Probability of introduction",
"Target confidence of freedom", "Population size", "Design Prevalence")
if (N <= 0) inputs[5, 1]<- "Unknown"
if(N <= 0) N<- NA
if (prev.int) inputs[nrow(inputs)]<- paste(prev, "units") else inputs[nrow(inputs)]<- paste(prev*100,"%", sep = "")

n<- n.pfree(PFree.target, prior, PIntro, prev, Sens, N)
results<- rbind(inputs, "Sample size required"= paste(n, "units"))
sink()

# write to html and file
heading<- "Sample size required to achieve a target probability of freedom"