Part One
Suppose we want to estimate the average weight of men and women in the U.S.
And we want to quantify the uncertainty of the estimate.
One approach is to simulate many experiments and see how much the results vary from one experiment to the next.
I'll start with the unrealistic assumption that we know the actual distribution of weights in the population. Then I'll show how to solve the problem without that assumption.
Based on data from the BRFSS, I found that the distribution of weight in kg for women in the U.S. is well modeled by a lognormal distribution with the following parameters:
Here's what that distribution looks like:
make_sample
draws a random sample from this distribution. The result is a NumPy array.
Here's an example with n=100
. The mean and std of the sample are close to the mean and std of the population, but not exact.
We want to estimate the average weight in the population, so the "sample statistic" we'll use is the mean:
One iteration of "the experiment" is to collect a sample of 100 women and compute their average weight.
We can simulate running this experiment many times, and collect a list of sample statistics. The result is a NumPy array.
The next line runs the simulation 1000 times and puts the results in sample_means
:
Let's look at the distribution of the sample means. This distribution shows how much the results vary from one experiment to the next.
Remember that this distribution is not the same as the distribution of weight in the population. This is the distribution of results across repeated imaginary experiments.
The mean of the sample means is close to the actual population mean, which is nice, but not actually the important part.
The standard deviation of the sample means quantifies the variability from one experiment to the next, and reflects the precision of the estimate.
This quantity is called the "standard error".
We can also use the distribution of sample means to compute a "90% confidence interval", which contains 90% of the experimental results:
Now we'd like to see what happens as we vary the sample size, n
. The following function takes n
, runs 1000 simulated experiments, and summarizes the results.
Here's a test run with n=100
:
Now we can use interact
to run plot_sampling_distribution
with different values of n
. Note: xlim
sets the limits of the x-axis so the figure doesn't get rescaled as we vary n
.
Other sample statistics
This framework works with any other quantity we want to estimate. By changing sample_stat
, you can compute the SE and CI for any sample statistic.
Exercise 1: Fill in sample_stat
below with any of these statistics:
Standard deviation of the sample.
Coefficient of variation, which is the sample standard deviation divided by the sample standard mean.
Min or Max
Median (which is the 50th percentile)
10th or 90th percentile.
Interquartile range (IQR), which is the difference between the 75th and 25th percentiles.
NumPy array methods you might find useful include std
, min
, max
, and percentile
. Depending on the results, you might want to adjust xlim
.
STOP HERE
We will regroup and discuss before going on.
Part Two
So far we have shown that if we know the actual distribution of the population, we can compute the sampling distribution for any sample statistic, and from that we can compute SE and CI.
But in real life we don't know the actual distribution of the population. If we did, we wouldn't be doing statistical inference in the first place!
In real life, we use the sample to build a model of the population distribution, then use the model to generate the sampling distribution. A simple and popular way to do that is "resampling," which means we use the sample itself as a model of the population distribution and draw samples from it.
Before we go on, I want to collect some of the code from Part One and organize it as a class. This class represents a framework for computing sampling distributions.
The following function instantiates a Resampler
and runs it.
Here's a test run with n=100
Now we can use interact_func
in an interaction:
Exercise 2: write a new class called StdResampler
that inherits from Resampler
and overrides sample_stat
so it computes the standard deviation of the resampled data.
Test your code using the cell below:
When your StdResampler
is working, you should be able to interact with it:
STOP HERE
We will regroup and discuss before going on.
Part Three
We can extend this framework to compute SE and CI for a difference in means.
For example, men are heavier than women on average. Here's the women's distribution again (from BRFSS data):
And here's the men's distribution:
I'll simulate a sample of 100 men and 100 women:
The difference in means should be about 17 kg, but will vary from one random sample to the next:
Here's the function that computes Cohen's effect size again:
The difference in weight between men and women is about 1 standard deviation:
Now we can write a version of the Resampler
that computes the sampling distribution of .
Now we can instantiate a CohenResampler
and plot the sampling distribution.
This example demonstrates an advantage of the computational framework over mathematical analysis. Statistics like Cohen's , which is the ratio of other statistics, are relatively difficult to analyze. But with a computational approach, all sample statistics are equally "easy".
One note on vocabulary: what I am calling "resampling" here is a specific kind of resampling called "bootstrapping". Other techniques that are also considering resampling include permutation tests, which we'll see in the next section, and "jackknife" resampling. You can read more at http://en.wikipedia.org/wiki/Resampling_(statistics).