Single Samlpe Tests

Suppose if we wish to test whether a sample, that has been drawn from a population having true mean (\(\mu\))is equal to a particular value \(\mu\)0 or not . Then the most common way of testing the hypothesis is follows as below.

Steps in hypothesis testing

  1. Setting Null(H0)and Alternative hypothesis(H1)

    H0: \(\large\mu = \large\mu\)0 (The sample has been drawn from the population having mean significantly equal to \(\mu\)0)

    Against

    H1: \(\large\mu\neq \mu\)0 (The sample may not be regarded as come from the population having mean \(\mu\)0)

  2. Selection of level of significance(\(\alpha\))

    In common practice we select \(\alpha = 0.05\)

  3. The test statistic.

    \({\displaystyle t = \frac{\overline{x} - \mu_0}{S/\sqrt{n}}}\)

  4. Decision Rule: Reject the \(H_0\), if \(p-value \leq 0.05\). Accept \(H_0\) , otherwise.

  5. Conclusion: Conclusion can be drawn on the basis of decision. If the H0 is rejected, then we can conclude that the sample has been drawn from the population having mean significantly equal to \(\mu_0\).

We shall discuss all those steps with the help of an example. For this let us retrieve a data set called iris by using data function.

data("iris")
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Check for underlying assumptions

Now suppose, if we are willing to find out whether the average Sepal length is 5 inches or not. This hypothesis in R can be tested as below. As it is necessary to check whether the data under study satisfies the underlying assumptions to perform any statistical analysis, we have to discuss some important underlying assumptions for the student’s t -test1, the test which we shall be using to test the hypothesis.

The following underlying assumptions shouldn’t be violated for the student’s t-test.

  • The data are interval or ratio scaled
  • The data should be fairly Normally distributed
  • The data are independent
  • There must not be any outliers in the data set.

We shall check whether our test variable (Sepal length) does satisfy the above mentioned assumptions or not. Ist let us test whether the test variable is fairly Normally distributed or not. For this we are using Shapiro Wilk test for Normality. This is the robust technique to test whether a given data is fairly normally distributed or not. The Null hypothesis is “The data are fairly Normally Distributed” against “The data are not significantly Normally Distributed” This can be tested by using the function shapiro.test in R. If the p- value is higher than 0.05 we can conclude that the data are fairly Normally distributed. Otherwise we conclude that data are not significantly Normally distributed.

Now let’s see the result. Note that our test variable is Sepal length.

hist(iris$Sepal.Length)

shapiro.test(iris$Sepal.Length)
## 
##  Shapiro-Wilk normality test
## 
## data:  iris$Sepal.Length
## W = 0.97609, p-value = 0.01018

The Shapiro Wilk test result shows that the p - value is equal to 0.01018 which is less than 0.05, which conclude that the data i.e. Sepal length are not significantly Normally distributed. Hence we can not apply student’s t- test in this case. Readers are also notified that the histogram of Sepal length does not fairly Normally distributed.2. Now let’s try with another test variable say Sepal width. As in earlier case let’s perform the Shapiro Wilk test to see whether the Sepal Width are Normally distributed or not.

hist(iris$Sepal.Width)

shapiro.test(iris$Sepal.Width)
## 
##  Shapiro-Wilk normality test
## 
## data:  iris$Sepal.Width
## W = 0.98492, p-value = 0.1012

The histogram is clearly showing that the Sepal width are fairy Normally distributed. It is confirmed by Shapiro wilk test, the p - value is equal to 0.1012 which is greater than 0.05 indicating the data are fairly Normally distributed. In order to check whether there is presence of any outliers in the set, we can use box plot as below. Box plot can be plotted in R using function boxplot.

boxplot(iris$Sepal.Width)

AS we can observe some of the outliers in the box plot, it violets the assumption to go for student’s t-test. Even though, it is moot to apply t -test in such cases; here, without loss of generality, for practice purpose only, we are going to demonstrate how t- test can be performed in R.

This allow us to move to student’s t - test.

Now Suppose if we wish to see whether the mean Sepal width is significantly equal to 3 inches or not. Then this statement can be tested in R using the function t.test as below.

t.test(iris$Sepal.Width, mu = 3)
## 
##  One Sample t-test
## 
## data:  iris$Sepal.Width
## t = 1.611, df = 149, p-value = 0.1093
## alternative hypothesis: true mean is not equal to 3
## 95 percent confidence interval:
##  2.987010 3.127656
## sample estimates:
## mean of x 
##  3.057333

Here mu = 3 is the test value as we wanted to see whether the average Sepal width is fairly equal to 3 inches or not. The test result shows that the p - value is equal to 0.1093 which is greater than 0.05, indicating that there is no reason to reject the Null hypothesis(H0). Note that the Null hypothesis is “the true mean is significantly equal to 3 inches”. The test result suggests us to accept the H0.

Two sample test

Suppose if we wish to test whether two independent sample are drawn from two independent population with significantly equal means or not, the test of hypothesis follows as below:

  1. Null hypothesis(H0): \(\large\mu\)1 = \(\large\mu\)2 (There is no significant difference in the means of the populations from which the two samples have been drawn)

    Against

    Alternative hypothesis(H1): \(\large\mu\)1 \(\neq\) \(\large\mu\)2 (There is significant difference between the means of the population from which the samples have been drawn)

  2. Level of significance(\(\large\alpha\)) = 0.05

  3. The test statistic

    \({\displaystyle t = \frac{(\overline{x_1}-\overline{x_2})-(\mu_1 - \mu_2)_0}{\sqrt{\frac{S_1^2}{n_1}+\frac{S_2^2}{n_2}}}}\)

  4. Decision Rule: Reject H0, if \(p - value \leq \alpha\).

    Accept H0, otherwise

  5. Conclusion can be drawn on the basis of decision. If the H0 is rejected, then we can conclude that the samples have been drawn from the population having significantly different means.

But, priory we shouldn’t forget to check the underlying assumptions of student’s t-test.

Suppose, if we wish to test whether the average Sepal length of two species setosa and versicolor are significantly different or not, we proceed as below in R.

Subsetting and checking the underlying assumptions

As we are willing to test whether there is significant difference between the average Sepal length of two species setosa and versicolor, we need to subset the given data set iris into two subsets each for setosa and versicolor. In R sub-setting can be done in various ways. we shall learn one of them.

Let us create variables namely setosa.sepal and versicolor.sepal by using the function subset to store the Sepal length of setosa and versicolor respectively.

setosa.sepal = subset(iris,Species == "setosa", select = c(Species,Sepal.Length))
head(setosa.sepal)
##   Species Sepal.Length
## 1  setosa          5.1
## 2  setosa          4.9
## 3  setosa          4.7
## 4  setosa          4.6
## 5  setosa          5.0
## 6  setosa          5.4

similarly we can create subset for Sepal length for the species versicolor.

versicolor.sepal = subset(iris, Species == "versicolor", select = c(Species,Sepal.Length))
head(versicolor.sepal)
##       Species Sepal.Length
## 51 versicolor          7.0
## 52 versicolor          6.4
## 53 versicolor          6.9
## 54 versicolor          5.5
## 55 versicolor          6.5
## 56 versicolor          5.7

After sub-setting the test variables, they must be put to check whether the underlying assumptions are satisfied to go for t- test.

At the beginning let us check whether these two data set do follow Normal Distribution or not. For this we can use Histogram(fro graphical approach) and Shapiro wilk test( for empirical approach) as in the single sample test.

hist(setosa.sepal$Sepal.Length)

shapiro.test(setosa.sepal$Sepal.Length)
## 
##  Shapiro-Wilk normality test
## 
## data:  setosa.sepal$Sepal.Length
## W = 0.9777, p-value = 0.4595
hist(versicolor.sepal$Sepal.Length)

shapiro.test(versicolor.sepal$Sepal.Length)
## 
##  Shapiro-Wilk normality test
## 
## data:  versicolor.sepal$Sepal.Length
## W = 0.97784, p-value = 0.4647

The results show that both the two data set follow fairly Normal distribution as the p - value for both the test variables are \(> 0.05\). For decision rule please see the single sample test section.

obviously, as the data were collected or recorded independently, we can confirm data are independent. The test of equal variances for both the test is also mandatory to use t-test, but in this case we are not going to check for it. In the later sections we shall discuss about it. Readers are highly recommended to check for presence of any outliers in the both the data sets separately using boxplot.

boxplot(iris$Sepal.Length~iris$Species)

No outliers can be observed in the box plots of Sepal length of setosa and versicolor species. For virginica species, we can noice an outlier. Now we are all set to apply t.test to test whether there is significant difference between average Sepal length of the species setosa and versicolor.

t.test(setosa.sepal$Sepal.Length,versicolor.sepal$Sepal.Length)
## 
##  Welch Two Sample t-test
## 
## data:  setosa.sepal$Sepal.Length and versicolor.sepal$Sepal.Length
## t = -10.521, df = 86.538, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.1057074 -0.7542926
## sample estimates:
## mean of x mean of y 
##     5.006     5.936

Assuming the Sepal length is equally varied in the species setosa and versicolor, the welch two sample t-test output shows that there is significant difference between the average sepal length between setosa and versicolor species.(the p-value < 0.05). From this out put we can also report that the sample average Sepal length for setosa and versicolor species are 5.006 and 5.936 respectively.(Verzani 2011)

References

Verzani, John. 2011. Getting Started with Rstudio. “ O’Reilly Media, Inc.”


  1. Students t distribution gets its name from William Sealy Gosset who first published it in English literature in his 1908 paper titled Biometrika using his pseudonym “Student”.

  2. Readers are notified that box plot and normal qqplot is also widely used graphical approach to check whether the data set is fairly Normally distributed or not. In this section we only focus on histogram. In the later sections we shall discuss about it in more details