How to Sample Data (With Code)
Sampling Our Data

We now need to take a sample of these data for our testing or development process.

How do we know how big our sample should be to represent our population accurately?

This is where we can use Cochran’s formulae:

Using the formula, we can calculate the sample size required to fulfill our required margin of error and confidence level. If we are looking at a specific attribute within a dataset, we can also specify the estimated proportion of our population to contain this attribute — although this is not a requirement.

So, we have a dataset containing 100,000 records. How many of these should we include in our sample?

Let’s say we would like to be 95% confident in our sample representing the population correctly. We can also accept a margin of error of ±5%.

population size: 100K - N = 100000

confidence level: 95% - cl = 0.95

margin of error: 5% - e = 0.05

target proportion: 50% - p = 0.5

First, we have all of the parameters needed to calculate n₀ except Z (the z-score). To find the z-score, we half our confidence level from 0.95 to 0.475 , then look-up this value in a z-score table, which gives us 1.96 .

We find our halved confidence level 0.475 in the table. The z-score is given by the values on the axes, 1.9 + 0.06, giving us 1.96. Find the table here.

Alternatively, we can calculate this using Python, R, or Excel.

Python

-----

import scipy.stats as st

st.norm.ppf(1-(1-0.95)/2)

[Out]: 1.959964

R

-----

qnorm(1-(1-0.95)/2)

[1]: 1.959964

Excel

-----

=NORMSINV(1-(1-0.95)/2)

1.959964

Note that in every code, we perform the same 1-(1-0.95)/2 calculation. This is because every implementation calculates the z-score for a left-tailed single distribution, which looks like this:

Instead, what we want is a center-aligned area, which looks like this:

Now that we have all of the required values, we calculate n₀:

To satisfy our sampling needs, we should round this up to 385 records. We round-up as this is our threshold — anything below 384.15 will have a less than 95% confidence level, or greater than a 5% margin of error.

To perform this calculation in code, we do the following:

Python

-----

z**2 * p * (1 - p) / e**2

[Out]: 384.15

R

-----

z^2 * p * (1 - p) / e^2

[1]: 384.15

Excel (replace each letter with the respective cell)

-----

=z^2*p*(1-p)/e^2

384.15

The second formula is what we use for small datasets; it adjusts our required sample size based on the population size N.

Smaller datasets will reduce the number of required samples, whereas larger datasets do not.

Using our population size N = 100,000 causes a slight reduction in our required sample size:

Giving us a final required sample size of 383 records. Performing this same calculation again in code, we have:

Python

-----

n_0 / (1 + (n_0 - 1) / N)

[Out]: 382.68

R

-----

n_0 / (1 + (n_0 - 1) / N)

[1]: 382.68

Excel (replace each letter with the respective cell)

-----

=n_0/(1+(n_0-1)/N)

382.68

With that, we have an easy, replicable method for calculating sample sizes. If we take it a little further, we can implement the full process in code to make our future sampling much easier.

In Python, we can build a simple sample size calculator like so:

Using our same parameters from earlier examples, we calculate the sample size with ease:

sample(100000, 0.95, 0.05, 0.5)

[Out]: 383

We can see how the population size affects our smaller samples calculation too:

sample(500, 0.95, 0.05, 0.5)

[Out]: 218

sample(20, 0.95, 0.05, 0.5)

[Out]: 20

sample(1e8, 0.95, 0.05, 0.5)

[Out]: 385

Of course, we do the same in both R and Excel by threading together the respective codes we previously discussed.
['James Briggs']
['Technology', 'Python', 'Data Science', 'Programming', 'Towards Data Science']