In one of our previous articles, we discussed how to calculate the test statistic in a chi-square test of independence or goodness-of-fit test. We also discussed that the test statistic in a chi-square test follows the chi-square distribution. So, how can we calculate the p-value from the test statistic in a chi-square test? In this article, we will discuss that.
Let’s say our test statistic is 6.4, and the degrees of freedom is 5. We can use the following Python code to calculate the p-value in that case:
from scipy.stats import chi2
# Calculate p-value for test statistic 6.4 and degrees of freedom = 5
test_statistic = 6.4
p_value = chi2.sf(test_statistic, df=5)
print("p-value: ", p_value)
Here, we are using the chi2.sf() function from the scipy.stats module to calculate the p-value from the test statistic. Please note that chi2.sf() is the survival function and it is the same as (1 – chi2.cdf()) function. But, in some cases, the survival function gives more accurate result than the (1 – chi2.cdf()) function.
The output of the above program will be like the following:
p-value: 0.2692187989871035








































0 Comments