T-test

The T-test is a type of test most commonly used in inferential statistics. This test is most commonly used in scenarios where we need to understand if there is a significant difference between the means of two groups. For example, say we have a dataset of students from certain classes. The dataset contains the height of each student. We are checking whether the average height is 175 cm or not:

  • Population: All students in that class
  • Parameter of interestμ, the population of a classroom
  • Null hypothesis: The average height is μ = 175
  • Alternative hypothesisμ > 175
  • Confidence level: α = 0.05

We have listed all the parameters. Now, we can use hypothesis testing:

  1. Let's first set up the dataset:
import numpy as np
height = np.array([172, 184, 174, 168, 174, 183, 173, 173, 184, 179, 171, 173, 181, 183, 172, 178, 170, 182, 181, 172, 175, 170, 168, 178, 170, 181, 180, 173, 183, 180, 177, 181, 171, 173, 171, 182, 180, 170, 172, 175, 178, 174, 184, 177, 181, 180, 178, 179, 175, 170, 182, 176, 183, 179, 177])
  1. Next, we are going to use the stats module from the SciPy library. Note that in the previous examples, we used the statsmodels API library. We could also continue using that, but our intention here is to introduce you to the new modules of the SciPy library. Let's import the library:
from scipy.stats import ttest_1samp
import numpy as np
  1. Now, let's use the NumPy library to compute the average height:
height_average = np.mean(height)
print("Average height is = {0:.3f}".format(height_average))

The output of the preceding code is as follows:

Average height is = 175.618
  1. Now, let's use the T-test to compute the new P-value:
tset,pval = ttest_1samp(height, 175)

print("P-value = {}".format(pval))

if pval < 0.05:
print("We are rejecting the null Hypothesis.")
else:
print("We are accepting the null hypothesis.")

The output of the preceding code is as follows:

Average height is = 175.618
P-value = 0.35408130524750125
We are accepting the null hypothesis

Note that our significance level (alpha = 0.05) and the computed P-value is 0.354. Since it is greater than alpha, we are accepting the null hypothesis. This means that the average height of students is 175 cm with a 95% confidence value. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85