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
!lecture_notes1_week1, p.4
We end up with sample means of
If we didn't know the true means, would the data given by our
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
- Since
and 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).
- This is why the variance is
. - See Probability - Variance#Sum of variances
- This is why the variance is
Now let's consider the difference between a random sample from population 1 and population 2:
- We are also assuming here that both populations are independent and have the same variance, which is why the variance is doubled when subtracting them.
Difference between sample means
The distribution of differences between the sample means (
The Variance of the sample average is
Since
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
Where
So,
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:
- H0: The two samples come from the same population distribution
- e.g. they have the same mean and standard deviation
- We can call this common distribution
and assume the variance is known but the man is unknown. - If this null hypothesis is true, then the difference between our two sample means would be normally distributed around zero, specifically according to the following equation:
- H1: The two samples come from different population distributions
- e.g. they might have different means or standard deviations
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
Then, we define another random variable
The difference between
(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 (
This will give us a p-value, or probability that the test statistic
!lecture_notes1_week1, p.12
We can compare our p-value to a threshold value called
Remember:
- p-value does not tell us the probability there is a difference between the two populations
- p-value does not tell us how big the difference between populations is
- The significance level
is an arbitrary choice and should be considered carefully before just relying on . - It's not recommended to test hypotheses and reject null hypotheses solely based on significance levels, but p-values are still useful for assessing statistical support for a phenomenon.
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 =
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 intervals will be denoted by
means the data (in our case, , e.g. all the population 1 and 2 data) and are functions that determine the lower and upper ends of the confidence interval. Since they are functions of our data, they are also random variables (like the test statistics).
Implementing confidence intervals
Based on our probability density function
!lecture_notes1_week1, p.15
Or:
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
Then we define a covariate (differenct from covariance) vector
(The first
The linear model for comparing our groups is:
The intercept
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