Comparing two populations through samples

Setting up example

Let's define two populations as following Normal (Gaussian) distributions and imagine taking samples from them. These represent the lengths of two whitefish populations in the Baltic Sea.

A plot of their probability distribution functions, true means, and values of n=15 random sampls from each and their sample means is given in the lecture notes:
!lecture_notes1_week1, p.4
We end up with sample means of y¯=25.1 and z¯=31.4

Question

If we didn't know the true means, would the data given by our n=15 random samples be enough to prove that the true means are different?

The sample means are called Estimators and they are a mathematical tool to estimate the true population parameters. Let's take a look at these values:

Distribution of sample means

If we repeat this random sampling 100 times, then take the average of all of these, the value will be closer to the true population mean. The lecture notes get into some other techniques in R for estimating the true mean from samples.

What's important from this is that estimators have their own variances.

Difference between populations

By analyzing the distribution (variance) of our samples, we can determine how confident we are that there is a difference between the true means of the populations (not just the sample means).

Back to our example. Let's consider yi and yi′, two random independent samples from population 1. Since we know that the trait values in y are Gaussian (normally) distributed, we can solve analytically that:

yi−yi′∼N(0,2σ2)
Why does it have this distribution?

  • Since yi and yi′ both come from the same population, they have the same mean, so subtracting one from the other should give a mean of zero.
    • This is why the mean is 0 in the distribution
  • Variance is additive and has no positive or negative sign. So when you subtract one sample from the other, the variances will actually add on to each other as long as they are independent (which they are in this case).

Now let's consider the difference between a random sample from population 1 and population 2:

yi−zi∼N(μ1−μ2,2σ2)

Difference between sample means

The distribution of differences between the sample means (y¯ and z¯) in our samples from populations 1 and 2 is:

y¯−z¯∼N(μ1−μ2,2σ2n)
Why is the variance divided by n?

The Variance of the sample average is V(ξ¯)=1nV(ξ). If both our samples use the same n and are independent, then we have:

V(y¯−z¯)=V(y¯)+V(z¯)V(y¯−z¯)=1nV(yi)+1nV(zi)V(y¯−z¯)=1n(σ2+σ2)V(y¯−z¯)=2σ2n

Since σ=10 and n=15, the variance is 13.33 and standard deviation is 3.7

Cumulative Distribution Function

This kind of function tells us what the probability that a random variable is less than or equal to a certain value. See Continuous Distributions for more info.

Let's define d=y¯−z¯ and call the cumulative function Fd(x) as the cumulative probability that d is less than the value x. This is defined as:

Fd(0)=Probability(d≤0)=∫−∞0p(d)dd

Where p(d) is the probability density function (distribution) of d - in other words:

p(d)=N(μ1−μ2,2σ2n)

So, Fd(0) is the integral (area under the curve) taken from minus infinity to zero of this function. Drawing:

This cumulative distribution function is useful for:

Frequentist inference

It lets us quantify how likely it is to observe a specific value in an estimator (in our case the difference between sample means)

Bayesian inference

It lets us calculate the probability that some parameter has a certain value. Not sure what this means yet, we'll look at this more later.

Frequentist hypothesis testing

Now we can figure out how likely it is that the two population trait means are different based only on our two samples.

Hypotheses:

y¯−z¯∼N(0,2σ2n)
Assumption of distributions

Note that both hypotheses assume we have a normal (Gaussian) distribution of both populations and samples. We will return to this note later when we look at the Bayesian Approach to Statistics.

To evaluate how likely that the difference in means arises under the null hypothesis lets introduce two random vectors Y and Z that collect n independent samples from the null distribution.

Then, we define another random variable T as T=Y¯−Z¯ - equal to the difference between sample means in the null distribution.

Note

The difference between y,z and Y,Z is that Y and Z are known to come from the null distribution, but y and z may or may not have come from this, that's what we're trying to figure out.

(in reality, they came from different distributions, but we are pretending we don't know that for this exercise).

Now we can get the probability that the difference in population means (t=y¯−z¯) is at least as extreme as the observed difference between our sample means under the null hypothesis:

Probability(|T|≥|t|)=Pr(|Y¯−Z¯|≥|y¯−z¯|)

This will give us a p-value, or probability that the test statistic T is as least as extreme as test statistic t. This is a two-tailed t-test. The lecture notes show how to evaluate this in R:
!lecture_notes1_week1, p.12

We can compare our p-value to a threshold value called α or a significance level. Traditional statistical analysis holds that we reject our null hypothesis if the p value is smaller than the significance level, and accepted otherwise.

Remember:

Student's t-test

Since variances are usually unknown, we need to estimate it using the sample variance. If the two populations are assumed to have different population variances, we need to use the Student's t-test (sample variance of population 1 = sy2):

y¯−z¯sy2/n+sz2/n∼Student-tv=n−2(0,1)

Confidence intervals

The output in R of the t-test mentions 95 percent confidence intervals. These are a measurement of uncertainty in an estimate.

Confidence level γ is a value between zero and one that tells how confident we want to be about the statement of the true value of the parameter of interest (for example the difference in population means). It is often defined to be a number close to one – most typically 95%.

Confidence intervals will be denoted by [l(D),u(D)]:

Confidence interval for a parameter θ, with confidence level γ is an interval [l(D),u(D)], such that

Pr(l(D)≤θ≤u(D))=γ

under the statistical model used to describe data.

(The probability of θ being between the two boundaries is equal to our Confidence level γ, usually 95%.)

Implementing confidence intervals

Based on our probability density function p(d) from earlier, we can calculate many iterations of getting the upper and lower bounds from Y and Z, random samples generated from our null model. We are skipping the details of how to actually solve the l(D) and u(D) functions for now, but this is how to do it in R:
!lecture_notes1_week1, p.15
Or:

l(D)=(y¯−z¯)−(quantile at 0.975)(sy−sz)nl(U)=(y¯−z¯)+(quantile at 0.975)(sy−sz)n

Not exactly sure how the quantile function works.

Conclusion

This is one method of frequentist hypothesis testing to see if two populations have different means.

The qualitative part of the test comes from the t-test, since it doesn't tell us how big the difference is, just how likely it is to be different.

The Confidence interval gives us both a qualitative comparison of whether a difference exists, and a quantitative estimate of the size of the difference.

Next, we will look at using linear models to compare population means.

Linear models for comparing population means

Definitions

Linear models are another way to compare population means through regression.

First, let's define y as a vector of all of the observations for populations 1 and 2:

y=[y1,y2...yn,z1,z2,...,zn]

Then we define a covariate (differenct from covariance) vector x which is just 0 for all the yi values and 1 for all the zi values:

x=[0,0,...0,1,1,...,1]

(The first n values of the vector x are 0, the last n elements are 1).

The linear model for comparing our groups is:

Yi=β0+β1xi+ϵi

The intercept β0 is μ1 and the coefficient on x β1=μ2−μ1 (also known as d).

Implementation in R

# Create vector of observed outcome variables (=fish lenghts) 
Y = c(y,z) 
# create a vector of covariates (=0 or 1 to indcate sampling location) 
X = c(0*y,z/z)


# And then visualize the data in "regression manner" 
plot(X,Y) 
# overlay the data with sample means 
points(0,mean(Y[X==0]), pch=17, col="black", cex=2) points(1,mean(Y[X==1]), pch=17, col="blue", cex=2)

!lecture_notes1_week1, p.17
The plot created from this shows the distribution of the two vectors on the y-axis, and assigns them to zero and one based on their category on the x axis. You can start to see how a regression would work on this with the R function lm(). We just need to add 1 to the X-values since I guess this will make the math work better (x-values are 1 and 2 instead of 0 and 1).

model = lm(Y ~ 1+X)
summary(model)

!lecture_notes1_week1, p.18
Based on the summary of our model generated in R, we can see that they gave an estimate of the difference between the two groups as 6.307 (under the coefficients X1 section). Standard error is 3.429. You can also use confint(model) to get the confidence intervals for the model.

Now let's go back to the main lecture note and look at data transformations.
Statistics - Introduction#Data Transformations