Skip to content

Instantly share code, notes, and snippets.

@jpcarrascal
Created November 18, 2019 16:26
Show Gist options
  • Save jpcarrascal/d509e701ed416bb4261cac325b699b80 to your computer and use it in GitHub Desktop.
Save jpcarrascal/d509e701ed416bb4261cac325b699b80 to your computer and use it in GitHub Desktop.
Estimate optimal sample size for a survey given the size of the population, the desired confidence level and margin of error.
# Simple function to determine optimum sample size for a survey.
# A lot of websites provide online calculators, but here is the code so you can do it yourself in R.
# Source:
# Krejcie, Robert V., and Daryle W. Morgan. "Determining sample size for research activities."
# Educational and psychological measurement 30.3 (1970): 607-610.
# https://journals.sagepub.com/doi/abs/10.1177/001316447003000308?journalCode=epma
# Downloadable PDF: https://home.kku.ac.th/sompong/guest_speaker/KrejcieandMorgan_article.pdf
#
# The paper provides a table with chi-squared distribution values for 1 degree of freedom.
# R function qchisq() generates the right value from a given confidence level, so the table is not needed.
# Function input parameters:
#
# N: population size
# err: desired margin of error. E.g. for 5%, err = 0.05
# cl: confidence level. E.g. for 95%, cl = 0.95
# P = response distribution (population proportion in the source). Often 50% is used as it gives larger sample size.
#
# Written by JP Carrascal: www.jpcarrascal.com, www.github.com/jpcarrascal
#
sample_size <- function(N, err, cl, P=0.5)
{
chisqV <- qchisq(cl, df=1)
ssize <- ( chisqV*N*P*(1-P) ) / ( err^2*(N-1) + chisqV*P*(1-P) )
return ( ssize )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment