Video link https://youtu.be/CvtprDnVZGA
In this example we will try to simplify and demistify practical aspects of the T test also known as the Student’s T Test
T-test or Student’s t-test, is a statistical method of testing hypotheses about the mean of a small sample drawn from a normally distributed population and the population standard deviation is unknown. It was developed by William Sealy Gosset in 1908 , he was an Englishman who published his results under the pseudonym Student.
Bernard Lewis Welch adapted the Student’s T Test and created a two-sample test based known as, Welch’s t-test, or unequal variances t-test, which is a two-sample location test used to test the hypothesis that two populations have equal means.
All data used in these examples are generated locally and there is no external dependency. The code for creating the dataset is also included here.
What is the goal of a T Test
Let us say that we collect the age of patients of heart-disease patients from two countries.
How would you compare and say that the data is significantly different or similar to each other.
Since age is a numeric value one obvious answer would be that we can take the mean , standard deviation of both the samples and see if the are similar. But the mean can be easily influenced by outliers (extreme values) in our data sets.
T-test is a scientific and statistically proven test which you can use.
We assume that both data samples are sampled from normal distributions with equal variances.
The null hypothesis is that the two means are equal and the alternative hypothisis is that the two means are not equal
You might have one data set from a city which you want to compare against a national standard. when we use only one data distribution then we need to provide a yardstick against which we can compare the mean of our data distribution.
eg. Mean = 8 (in R we represent it as mu = 8) or Mean = 10 (mu = 10)
You might have two data set from two countries or cities and want to compare them against each other. When we compare two data distributions
By default the confidence interval is taken as 0.95 or 95% so that we can say that the probability of the dataset being similar is 95%. We can change the confidence interval (confidence level) as per our need. (eg 0.8 for 80%)
It depends on what question you pose in your hypothesis.
one.sided - e.g. Does the height of boys higher than girls in a class
two.sided - e.g. Does the height of boys and girls significantly different in a class(it could be higher or lower hence two tailed or two.sided)
For one.sided test we also need to provide the alternative.(eg. less, greater or two.sided)
Let us say that we want to compare if the mean is 8 (or any other value) or not
less - That the mean of the distribution is less than 8
greater - That the mean of the distribution is greater than 8
two.sided - That the mean of the distribution is not equal to 8 (in other words it could be less than 8 or more than 8. It is comparing on both sides of 8 hence a two.sided test).
If you two two dataset for comparison, eg. you are using the data from two different cities then is is unpaired. As the data is not for a specific person.
If the data is for the same person e.g. the weight of person before treatment and after treatment then it is paired data as the date is tied to the same person.
You would see an example of both in this document.
t - critical value depending on the significance level of the test.
df - degree of freedom (Degree of freedom = the size of the data sample minus one) df = n−1.
p - level of significance (usually 0.05 representing 95% confidence level).
#Create the data
#install.packages(wakefield)
library(wakefield)
MaleAdults <- wakefield::age(n= 100, x = 18:110)
FemaleAdults <- wakefield::age(n= 100, x = 18:110)
MaleKids <- wakefield::age(n= 100, x = 0:17 )
The MaleAdults dataset which we created above had 100 records and has ages between 18 to 110
MaleAdults
FALSE [1] 20 35 72 76 50 66 38 104 59 97 67 55 21 46 18 47 73 104
FALSE [19] 26 37 39 60 83 24 83 43 57 48 44 76 58 27 32 18 70 43
FALSE [37] 60 90 46 37 92 109 104 97 25 46 44 76 43 33 64 95 61 88
FALSE [55] 95 107 68 51 34 35 104 104 53 101 53 70 74 29 23 108 65 68
FALSE [73] 76 70 70 43 40 54 38 56 49 62 45 74 58 74 61 98 32 92
FALSE [91] 53 101 71 72 30 90 29 74 56 22
The FemaleAdults dataset which we created above had 100 records and has ages between 18 to 110
FemaleAdults
FALSE [1] 58 64 50 31 88 79 108 89 82 37 35 103 72 22 47 46 21 25
FALSE [19] 71 106 76 78 92 52 58 89 109 59 24 86 110 85 51 23 108 80
FALSE [37] 83 99 63 54 98 96 64 90 104 23 21 99 72 63 87 101 20 71
FALSE [55] 102 63 97 80 78 45 46 91 58 87 71 22 104 46 79 77 109 46
FALSE [73] 97 85 62 72 75 83 74 110 77 98 49 103 20 91 61 51 20 102
FALSE [91] 35 84 23 45 92 55 90 74 87 24
The MaleKids dataset which we created above had 100 records and has ages between 0 to 17 years
MaleKids
FALSE [1] 9 8 10 16 3 6 13 17 1 11 3 10 6 11 5 16 0 3 16 5 9 5 4 11 12
FALSE [26] 15 7 3 4 7 9 6 7 3 17 13 16 2 17 17 8 15 9 4 8 15 9 13 16 8
FALSE [51] 8 11 10 3 14 5 16 2 14 14 7 5 8 0 7 10 12 14 7 0 13 1 3 7 1
FALSE [76] 9 5 16 5 0 1 17 7 10 16 6 7 11 1 10 13 11 9 4 6 11 2 15 14 0
In statistics, Welch’s t-test, or unequal variances t-test, is a two-sample location test which is used to test the hypothesis that two populations have equal means. It is named for its creator, Bernard Lewis Welch, and is an adaptation of Student’s t-test two sample t test.
We compare the means of each distribution against each other
Interpreting the results
In the following case the p value = 0.56 which is higher than 0.05 threshold(for 95% confidence level)
To keep it simple
remember that if the p value is less than 0.05 then we accept the alternative hypothesis
If p value > 0.05 (assuming 95% confidence interval) then we accept the null hypothesis
so we will say that at 95% conf. levels there is no significant difference between the means of two samples The t value provides the significance
#remember that if the p value is less than 0.05 then we accept the alternative hypothesis
#If p value > 0.05 (assuming 95% confidence interval) then we accept the null hypothesis
#so we will say that at 95% conf. levels there is no significant difference between the means of #two samples.
t.test(MaleAdults, FemaleAdults, alternative ="two.sided")
FALSE
FALSE Welch Two Sample t-test
FALSE
FALSE data: MaleAdults and FemaleAdults
FALSE t = -2.4486, df = 197.05, p-value = 0.01522
FALSE alternative hypothesis: true difference in means is not equal to 0
FALSE 95 percent confidence interval:
FALSE -16.320861 -1.759139
FALSE sample estimates:
FALSE mean of x mean of y
FALSE 60.58 69.62
Notice the difference in the output based on the alternative being used.
#remember that if the p value is less than 0.05 then we accept the alternative hypothesis
#If p value > 0.05 (assuming 95% confidence interval) then we accept the null hypothesis
#so we will say that at 95% conf. levels there is no significant difference between the means of #two samples
#----------------------
t.test(MaleAdults, MaleKids, alternative ="less")
FALSE
FALSE Welch Two Sample t-test
FALSE
FALSE data: MaleAdults and MaleKids
FALSE t = 20.255, df = 106.92, p-value = 1
FALSE alternative hypothesis: true difference in means is less than 0
FALSE 95 percent confidence interval:
FALSE -Inf 56.28137
FALSE sample estimates:
FALSE mean of x mean of y
FALSE 60.58 8.56
#----------------------
t.test(MaleAdults, MaleKids, alternative ="greater")
FALSE
FALSE Welch Two Sample t-test
FALSE
FALSE data: MaleAdults and MaleKids
FALSE t = 20.255, df = 106.92, p-value < 2.2e-16
FALSE alternative hypothesis: true difference in means is greater than 0
FALSE 95 percent confidence interval:
FALSE 47.75863 Inf
FALSE sample estimates:
FALSE mean of x mean of y
FALSE 60.58 8.56
#remember that if the p value is less than 0.05 then we accept the alternative hypothesis
#If p value > 0.05 (assuming 95% confidence interval) then we accept the null hypothesis
#so we will say that at 95% conf. levels there is no significant difference between the means of two samples
t.test(MaleAdults, mu = 40, alternative ="two.sided")
FALSE
FALSE One Sample t-test
FALSE
FALSE data: MaleAdults
FALSE t = 8.1722, df = 99, p-value = 1.025e-12
FALSE alternative hypothesis: true mean is not equal to 40
FALSE 95 percent confidence interval:
FALSE 55.58314 65.57686
FALSE sample estimates:
FALSE mean of x
FALSE 60.58
Just for fun let us compare the same sample with it. In the following example we will compare MaleAdults with MaleAdults. You would notice that
t = 0, df = 198, p-value = 1.
remember that if the p value is less than 0.05 then we accept the alternative hypothesis
If p value > 0.05 (assuming 95% confidence interval) then we accept the null hypothesis
so we will say that at 95% conf. levels there is no significant difference between the means of #two samples.
Do a two.sided t.test using the same data
# Read the output of the t test carefully
t.test(MaleAdults, MaleAdults, alternative ="two.sided")
FALSE
FALSE Welch Two Sample t-test
FALSE
FALSE data: MaleAdults and MaleAdults
FALSE t = 0, df = 198, p-value = 1
FALSE alternative hypothesis: true difference in means is not equal to 0
FALSE 95 percent confidence interval:
FALSE -7.023182 7.023182
FALSE sample estimates:
FALSE mean of x mean of y
FALSE 60.58 60.58
Use alternative =“less” option, read the alternative hypothesis** and see how it changes based on the option used
# Read the output of the t test carefully
t.test(MaleAdults, MaleAdults, alternative ="less")
FALSE
FALSE Welch Two Sample t-test
FALSE
FALSE data: MaleAdults and MaleAdults
FALSE t = 0, df = 198, p-value = 0.5
FALSE alternative hypothesis: true difference in means is less than 0
FALSE 95 percent confidence interval:
FALSE -Inf 5.885551
FALSE sample estimates:
FALSE mean of x mean of y
FALSE 60.58 60.58
Use an alternative =“greater” option, read the alternative hypothisis and see how it changes based on the option used
# Read the output of the t test carefully
t.test(MaleAdults, MaleAdults, alternative ="greater")
FALSE
FALSE Welch Two Sample t-test
FALSE
FALSE data: MaleAdults and MaleAdults
FALSE t = 0, df = 198, p-value = 0.5
FALSE alternative hypothesis: true difference in means is greater than 0
FALSE 95 percent confidence interval:
FALSE -5.885551 Inf
FALSE sample estimates:
FALSE mean of x mean of y
FALSE 60.58 60.58
t-Test: Two-Sample Assuming Equal Variance
t-Test: Two-Sample Assuming Unequal Variance
#-------------------
# t-Test: Two-Sample Assuming Equal Variance
#--------------------
t.test(MaleAdults, FemaleAdults, var.equal = TRUE, alternative ="two.sided")
FALSE
FALSE Two Sample t-test
FALSE
FALSE data: MaleAdults and FemaleAdults
FALSE t = -2.4486, df = 198, p-value = 0.01521
FALSE alternative hypothesis: true difference in means is not equal to 0
FALSE 95 percent confidence interval:
FALSE -16.320644 -1.759356
FALSE sample estimates:
FALSE mean of x mean of y
FALSE 60.58 69.62
#-------------------
# t-Test: Two-Sample Assuming Unequal Variance
#--------------------
t.test(MaleAdults, FemaleAdults, var.equal = FALSE, alternative ="two.sided")
FALSE
FALSE Welch Two Sample t-test
FALSE
FALSE data: MaleAdults and FemaleAdults
FALSE t = -2.4486, df = 197.05, p-value = 0.01522
FALSE alternative hypothesis: true difference in means is not equal to 0
FALSE 95 percent confidence interval:
FALSE -16.320861 -1.759139
FALSE sample estimates:
FALSE mean of x mean of y
FALSE 60.58 69.62
# By default the confidene interval or confidence level is 0.95 (95% probability that a )
#----------------------------
t.test(MaleAdults, mu = 8, alternative ="less" )
FALSE
FALSE One Sample t-test
FALSE
FALSE data: MaleAdults
FALSE t = 20.879, df = 99, p-value = 1
FALSE alternative hypothesis: true mean is less than 8
FALSE 95 percent confidence interval:
FALSE -Inf 64.76137
FALSE sample estimates:
FALSE mean of x
FALSE 60.58
t.test(MaleAdults, mu = 8, alternative ="less" , conf.level = 0.8)
FALSE
FALSE One Sample t-test
FALSE
FALSE data: MaleAdults
FALSE t = 20.879, df = 99, p-value = 1
FALSE alternative hypothesis: true mean is less than 8
FALSE 80 percent confidence interval:
FALSE -Inf 62.70864
FALSE sample estimates:
FALSE mean of x
FALSE 60.58
Prepare the data
library(flextable) # We are using this package only to display the data below. You don't need it to do a t test
set_flextable_defaults(fonts_ignore=TRUE)
df <- data.frame(Name = c('A', 'B', 'C','D' ,'E','F','G','H','I','J','K')
,MarksInClass10 = c( 80, 89 , 68 , 76 ,69 ,45 ,78 ,89 ,67 ,68 ,69 )
,MarksInclass12 = c(92 ,68 ,74 , 88 ,65 ,86 ,67 ,81 , 62,88, 91))
flextable(df)
Name | MarksInClass10 | MarksInclass12 |
A | 80 | 92 |
B | 89 | 68 |
C | 68 | 74 |
D | 76 | 88 |
E | 69 | 65 |
F | 45 | 86 |
G | 78 | 67 |
H | 89 | 81 |
I | 67 | 62 |
J | 68 | 88 |
K | 69 | 91 |
Run paired t-test
Let us run the paired t- test using the paired = TRUE option
# Here is the t-test using paired = TRUE option which indicates that the data is paired.
t.test(df$MarksInClass10,df$MarksInclass12,paired = TRUE, alternative = "two.sided", conf.level = 0.95)
FALSE
FALSE Paired t-test
FALSE
FALSE data: df$MarksInClass10 and df$MarksInclass12
FALSE t = -1.0815, df = 10, p-value = 0.3049
FALSE alternative hypothesis: true difference in means is not equal to 0
FALSE 95 percent confidence interval:
FALSE -17.805103 6.168739
FALSE sample estimates:
FALSE mean of the differences
FALSE -5.818182
#remember that if the p value is less than 0.05 then we accept the alternative hypothesis
#If p value > 0.05 (assuming 95% confidence interval) then we accept the null hypothesis
#so we will say that at 95% conf. levels there is no significant difference between the means of two #samples.(p-value = 0.56)
dt <- t.test(MaleAdults, mu = 8, alternative ="two.sided")
#You can access some of the values as shown below
dt$statistic
FALSE t
FALSE 20.87913
dt$conf.int
FALSE [1] 55.58314 65.57686
FALSE attr(,"conf.level")
FALSE [1] 0.95
dt$p.value
FALSE [1] 4.779791e-38
# Show the p value in decimal points instead of the scientific notation
format(dt$p.value, scientific = FALSE)
FALSE [1] "0.00000000000000000000000000000000000004779791"
dt$estimate
FALSE mean of x
FALSE 60.58
dt$stderr
FALSE [1] 2.518304
dt$null.value
FALSE mean
FALSE 8
dt$alternative
FALSE [1] "two.sided"
Here is a list of frequenty asked questions and their answers for you.
FAQ : Is T-Test and Student’s T Test different?
Answer: No, T-Test and Student’s T Test is the same. A two sample T-test is also known as Welch’s t-test.
FAQ : What is the minimum number of records needed to do a reliable t test?
Answer: At-least 10.
FAQ : Do we need to have same number of records in two data sets when we do a two sample t test?
Answer: No. For example we can have 10 records in one data set and 15 in another, remember we are only comparing the means of both datasets so it does not matter how many records each data set has.
FAQ : In perfect world if we compare the same dataset with it in a two sample T-test then what happens.
Answer : In that situation t = 0 and p = 1. depending upon the cases t values can go positive or negative. where as p value would be between 0 and 1.
FAQ : It is hard to interpret the results of t-test, is there an easy way to interpret? Answer :Remember that if the p value is less than 0.05 then we accept the alternative hypothesis If p value > 0.05 (assuming 95% confidence interval) then we accept the null hypothesis.