Skip to content

Instantly share code, notes, and snippets.

View jpcarrascal's full-sized avatar
💭
www.spacebarman.com

JP Carrascal jpcarrascal

💭
www.spacebarman.com
View GitHub Profile
@jpcarrascal
jpcarrascal / survey_sample_size.R
Created November 18, 2019 16:26
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.