Skip to content

Instantly share code, notes, and snippets.

@mrdwab
Created May 21, 2011 17:35
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdwab/984707 to your computer and use it in GitHub Desktop.
Save mrdwab/984707 to your computer and use it in GitHub Desktop.
R sample size and confidence interval calculation
###############################################################################
# Sample Size and Confidence Interval Calculation #
# v 1.3 by "Ananda Mahto"/mrdwab/ananda@mahto.info #
# 2011 May 17 #
# --------------------------------------------------------------------------- #
# #
# Example usage: #
# * sample.size.table(c.lev = c(90, 95, 98, 99), population = 378) #
# * sample.size(c.lev = 98, population = 200) #
# * confidence.interval(c.lev = 95, p.ss = 80, population = 100) #
# #
# Advanced usage: #
# * sample.size.table (c.lev = rep(c(95:99), each = 5), #
# c.int = c(1:5), pop = 100) #
# * sample.size.table (c.lev = rep(c(95:99)), c.int = 5, #
# pop = rep(c(101:105), each=5)) #
# --------------------------------------------------------------------------- #
# Notes: #
# c.lev = confidence level, usually 90 or 95 #
# margin = usually set at 50%, which gives the largest sample size #
# c.interval = confidence interval, usually +/- 5% #
# population = the entire population you want to draw a sample from #
# p.ss = your population sample size #
###############################################################################
sample.size.table = function(c.lev, margin=50, c.interval=5, population) {
z = qnorm(.5+c.lev/200)
m = margin/100
int = c.interval/100
ss = (z^2 * m * (1-m))/(int^2)
p.ss = ss/(1 + ((ss-1)/population))
CL = c(paste(c.lev, "%", sep = ""))
CI = c(paste(c.interval, "%", sep = ""))
M = c(paste(margin, "%", sep = ""))
METHOD = c("Sample size calculations")
RES = data.frame(population, CL, CI, M, round(p.ss, digits = 0))
pre = structure(list(POP = "Population", CL = "Confidence level",
CI = "Confidence interval (+/-)", MOE = "Margin of error",
SS = "Sample size", method = METHOD), class = "power.htest")
names(RES) = c("POP","CL", "CI", "MOE", "SS")
print(pre)
print(RES)
}
sample.size = function(c.lev, margin=.5, c.interval=.05, population) {
z.val = qnorm(.5+c.lev/200)
ss = (z.val^2 * margin * (1-margin))/c.interval^2
p.ss = round((ss/(1 + ((ss-1)/population))), digits=0)
METHOD = paste("Recommended sample size for a population of ", population,
" at a ", c.lev, "% confidence level", sep = "")
moe = paste((c.interval*100), "%", sep="")
resp.dist = paste((margin*100),"%", sep="")
structure(list(Population = population, "Confidence level" = c.lev,
"Margin of error" = moe, "Response distribution" = resp.dist,
"Recommended sample size" = p.ss, method = METHOD), class = "power.htest")
}
confidence.interval = function(c.lev, margin=.5, p.ss, population) {
z.val = qnorm(.5+c.lev/200)
ss = ((population*p.ss-p.ss)/(population-p.ss))
c.interval = sqrt((z.val^2 * margin * (1-margin))/ss)
r.cint = round(c.interval*100, digits = 2)
METHOD = paste("The confidence interval is ", r.cint,
"% with the following assumptions:", sep = "")
resp.dist = paste((margin*100),"%", sep="")
structure(list(Population = population, "Sample size" = p.ss,
"Confidence level" = c.lev, "Response distribution" = resp.dist,
method = METHOD), class = "power.htest")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment