How many samples do I need?

A sample size calculator with shiny bling

mrdwab

Why do I need this?

Conducting surveys is an expensive proposition, particularly when the populations you are working with are large.

Wouldn't it be nice if you could just survey a sample of your target population and still be able to say with certainty that your results are reliable?

Well, you can! You just need to know what the right sample size should be.

We've got just the app to help you with that!

But first...

How do I calculate the sample size I need?

Magic!

Actually, no. That's not true. There's a pair of formulae that you need that looks like this:

\[ss = \frac{-Z^2\times p\times(1-p)}{c^2}\] \[pss = \frac{ss}{1+\frac{ss-1}{pop}}\]

Basically, you're going to have to provide:

  1. The population.
  2. The confidence interval (or margin of error) (for example, 5%).
  3. The confidence level you need (for example, 95%).
  4. The response distribution (usually untouched at 50%).

(See this link for details on the formula.)

Can I do this in R?

A basic function would look like this:

sample.size <- function(population, c.lev = 95, c.int = 5, distribution = 50) {
  z = qnorm(0.5 + c.lev/200)
  ss = (z^2 * (distribution/100) * (1 - (distribution/100)))/((c.int/100)^2)
  ss/(1 + ((ss - 1)/population))
}

You could then use it like this:

sample.size(300)
## [1] 168.6957

Not too bad, but not too pretty either....

Introducing the Shiny Sample Size Calculator

Get it now!

Use "devtools" and run it locally:

library(devtools)
library(shiny)
runGitHub("mrdwab/ddp_shiny_sample")

Two easy steps!

  1. Enter the population.
  2. Click on "Calculate my sample size!"

But wait! Do you want to see how the sample size changes when fiddling with the other input variables?

Use the sliders to get a live update of the resulting sample size!

Run it on the cloud!

Run it online!