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() which you can easily load in R by typing source("http://news.mrdwab.com/samplesize") at the command prompt.

Here’s some more information about each.

sample.size.table()

The new code looks like this:

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> 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:

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
> 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:

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
> 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:

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
> 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!


Related posts (possibly):

  1. A sample size calculator function for R IMPORTANT: This is here mostly to remind me of how...
  2. Stratified Random Sampling in R–A Function in Progress IMPORTANT: This is here mostly to remind me of how...
  3. Stratified random sampling in R from a data frame After a little bit more work, there’s a new stratified...
  4. R is like a giant calculator for grownups One of the things that is interesting about R is...
  5. Sampling with replacement in R In my last post about sampling (Simple sampling with R)...
This entry was posted in (all categories), Geekiness, Useless Knowledge and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • http://news.mrdwab.com mrdwab

    If you’d like to view the text version of the most up-to-date version of this R script, visit http://news.mrdwab.com/samplesize.R.txt

    There, you’ll find any updates or any functions I’ve added–like a new function to determine the confidence interval if you know the sample size, the population, and the confidence level.

    Enjoy!

  • http://news.mrdwab.com mrdwab

    If you’d like to view the text version of the most up-to-date version of this R script, visit http://news.mrdwab.com/samplesize.R.txt

    There, you’ll find any updates or any functions I’ve added–like a new function to determine the confidence interval if you know the sample size, the population, and the confidence level.

    Enjoy!

  • Shanti Gupta

    Ananda,
    This is really useful in learning how to calculate sample size with confidence interval.

    • http://news.mrdwab.com mrdwab

      Shanti,

      The calculation part is useful, but what I like most is being able to see multiple scenarios in a single place. For instance, what would our samples be at different confidence levels? What about different margins of error? Using this function–or slightly modified versions of this function, it is easy to see how changing these values would affect your sample size.

  • Prashanth NS

    Thanks! Very useful. 

    • http://ananda.mahto.info Ananda

      You’re welcome.

      Just out of curiosity, do you use R at the Institute of Public Health, Bangalore?

    • http://news.mrdwab.com mrdwab

      You’re welcome.

      Just out of curiosity, do you use R at your work? I’m curious about R’s uptake in India’s professional sector.

      —- On Wed, 22 Jun 2011 11:45:49 +0530 Disqus <> wrote —-

      ======