Comparing two means – Student’s t-test

Student’s t-test (also known as Welch two sample test) requires that samples are independent, of equal variance and normally distributed. The Shapiro-Wilk test may thus be employed to check for normality prior to performing the comparison; Fisher’s F test will help checking for equal variances.

In Student’s t-test, the null hypothesis H0 states that the means of the two samples are equal.

In the following example, we’ll compare the samples W1.may15 and W1.may16. These 2 groups contains the daily temperatures recorded during the first week of  May 2015 and the first week of May 2016 at a location close to Bergen.


Let’s check these samples in a boxplot :

boxplot(W1.may15,W1.may16, ylab="Temperature (degrees Celsius)", names=c("W1.may15","W1.may16"))

Skjermbilde 2016-06-13 23.07.45

As you may see, the samples and their medians appear to slightly differ, but the respective spreads of the samples seem similar. Let’s proceed with verifying the assumptions of normality and equal variance:

var.test(W1.may15, W1.may16)

Skjermbilde 2016-06-13 23.11.17

According to the Shapiro-Wilk test, both samples are likely to come from normal distributions. Looking at Fisher’s F test, the variances of the groups are equal. All assumptions for running Student’s t-test are respected.

Satisfied? Let’s keep going then. Now it is (finally) time to perform Student’s t test:


Skjermbilde 2016-06-13 23.16.04

The p-value that results from the test is greater than 0.05 (the typical value of α); therefore, the null hypothesis H(the means of temperature observed during the first week of May 2015 and during the first week of May 2016 are equal) cannot be rejected.