Book contents

STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 2

Data Collection and Sampling

Reliable data are the foundation of statistical inference. How you collect data affects the conclusions you can draw. This chapter covers basic principles of study design, sampling and the common pitfalls that can undermine analyses.

Why Sampling?

In most situations it is impractical or impossible to measure an entire population. Instead, we take a sample and use it to make inferences about the population. A sampling frame is a list or procedure that defines the population of interest. Ideally, each member of the population has a known, non-zero probability of being selected.

Sampling Methods

Bias and Non-Response

Example: Estimating Average Study Hours

Suppose you wish to estimate the average number of hours per week that university students spend studying. The population is all students at your university. To obtain a simple random sample, you could:

Worked example

  1. Obtain a list of all registered students (the sampling frame).

  2. Use software to randomly select 200 student IDs.

  3. Send a survey asking respondents to report their study hours.

R code to draw a simple random sample of student IDs:

# Suppose students is a vector of all student IDs
set.seed(123)
sample_size <- 200
sample_ids <- sample(students, sample_size)

Python code using NumPy:

import numpy as np

np.random.seed(123)
sample_size = 200
sample_ids = np.random.choice(students, size=sample_size, replace=False)

Julia code using Random:

using Random
Random.seed!(123)
sample_size = 200
sample_ids = rand(RandomSampler(students), sample_size)

Once the sample is selected, collect the study hours data. The sample mean provides an unbiased estimate of the population mean. The standard error quantifies uncertainty due to sampling.

Exercises

Practice

  1. Describe how you would design a stratified sample to estimate average commuting times in a city with distinct neighbourhoods. Which strata would you choose and why?

  2. Simulate a simple random sample of 100 numbers from 1 to 10 000 in R, Python or Julia. Compute the sample mean and compare it to the population mean.

  3. Explain why cluster sampling might be preferred over simple random sampling in a survey of high-school students’ dietary habits.

  4. Discuss the potential sources of bias in an online poll about voting intentions. How could you attempt to correct for them?

References