The new sample size calculator for R (already)

aka “Maybe I shouldn’t post so quickly

Just hours ago, I posted my first set of functions for R to determine the sample size for a known population. Then, I had to update that post to reflect my newfound knowledge, and now, I thought I would update again, so that the best functions I came up with would all be in one place. There are two functions, sample.size.table() and sample.size(). Here’s some more information about each.

sample.size.table()

The new code looks like this:

sample.size.table = 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 = ss/(1 + ((ss-1)/population))
  c.level = c(paste(c.lev, "%", sep = ""))
  results = data.frame(c.level, round(p.ss, digits = 0))
  names(results) = c("Confidence Level", "Sample Size")
  METHOD = c("Suggested sample sizes at different confidence levels")
  moe = paste((c.interval*100), "%", sep="")
  resp.dist = paste((margin*100),"%", sep="")
  pre = structure(list(Population=population,
                       "Margin of error" = moe,
                       "Response distribution" = resp.dist,
                       method = METHOD),
                  class = "power.htest")
  print(pre)
  print(results)
}

What’s good about it or when do you use it? Now, you can specify the confidence levels you want to experiment with. See the following example:

sample.size.table(c.lev = c(90, 91, 92, 93, 94,
                            95, 96, 97, 98, 99,
                            99.9, 99.99, 99.999, 99.9999),
                  population = 100)
##
##      Suggested sample sizes at different confidence levels
##
##            Population = 100
##       Margin of error = 5%
## Response distribution = 50%
##
##    Confidence Level Sample Size
## 1               90%          73
## 2               91%          74
## 3               92%          76
## 4               93%          77
## 5               94%          78
## 6               95%          80
## 7               96%          81
## 8               97%          83
## 9               98%          85
## 10              99%          87
## 11            99.9%          92
## 12           99.99%          94
## 13          99.999%          95
## 14         99.9999%          96

Unlike the original function I had written, this one allows you to enter any values your heart desires (logical values would be from 1 to 99.99-something) and even lets you enter an array of numbers like we did in the example above. Pretty spiffy, right?

sample.size()

If you don’t care for a whole array of results, you might be more interested in simply using the sample.size() function, which looks like this:

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 = "")
  structure(list(Population = population,
                 "Confidence level" = c.lev,
                 "Margin of error" = c.interval,
                 "Response distribution" = margin,
                 "Recommended sample size" = p.ss,
                 method = METHOD),
            class = "power.htest")
}

It’s pretty easy to use. Here’s an example:

sample.size(c.lev = 97, population = 300)
##
##      Recommended sample size for a population of 300 at a 97% confidence level
##
##              Population = 300
##        Confidence level = 97
##         Margin of error = 0.05
##   Response distribution = 0.5
## Recommended sample size = 183
##

Now, I guess we need to see if this post was made prematurely too!

comments powered by Disqus