Prerequisites: Parametric and Non-Parametric Methods, Hypothesis TestingĀ
In this article, we are going to see how to conduct a Wilcoxon signed-Rank test in the Python programming language. Wilcoxon signed-rank test, also known as Wilcoxon matched pair test is a non-parametric hypothesis test that compares the median of two paired groups and tells if they are identically distributed or not.Ā
Wilcoxon signed-rank test using Ā Wilcoxon() function
In this approach, the user needs to call the Ā Wilcoxon() function with the required parameters from the scipy.stats library to conduct the Wilcoxon signed-rank test on the given data in the python programming language.
Syntax: wilcoxon(x, y, alternative=ātwo-sidedā)
Parameters:
- x: an array of sample observations from group 1
- y: an array of sample observations from group 2
- alternative: defines the alternative hypothesis. Default is ātwo-sidedā but other options include ālessā and āgreater.ā
This is a hypotheses test and the two hypotheses are as follows:
- Ho(Accepted): Sample distributions are equal.
- Ha(Rejected): Sample distributions are not equal.
Example 1: Conduct a basic Wilcoxon Signed-Rank Test in Python
In this example, we will be simply using the Wilcoxon() function from the scipy.stats library to Conduct a Wilcoxon signed-rank test of the given two groups in the python programming language.
Python
# Create data import scipy.stats as stats Ā
group1 = [ 456 , 564 , 54 , 554 , 54 , 51 , 1 , 12 , 45 , 5 ] group2 = [ 65 , 87 , 456 , 564 , 456 , 564 , 564 , 6 , 4 , 564 ] Ā
# conduct the Wilcoxon-Signed Rank Test stats.wilcoxon(group1, group2) |
Output:
WilcoxonResult(statistic=15.0, pvalue=0.2023283082009374)
Output Interpretation:
In the above example, the p-value is 0.2 which is less than the threshold(0.05) which is the alpha(0.05) i.e. p-value<alpha which means the sample is of the same distribution and the sample distributions are equal if in the case if the p-value>0.05 than it would be opposite.
Example 2: Conduct Wilcoxon Signed-Rank Test with CSV file
In this example, we will be using a data set of 121 lines containing the blood pressure after and the before blood pressure and then will be testing it using the wilcoxon() function in the python programming language.
The link to the dataset used: click here.
Python
import pandas as pd import scipy.stats as stats Ā
data = pd.read_csv( "gfg_data.csv" ) stats.wilcoxon(data[ 'bp_before' ], data[ 'bp_after' ]) |
Output:
WilcoxonResult(statistic=2234.5, pvalue=0.0014107333565442858)
Output Interpretation:
In the above example, the p-value is 0.001 which is less than the threshold(0.05) which is the alpha(0.05) i.e. p-value<alpha which means the sample is of the same distribution and the sample distributions are equal if in the case if the p-value>0.05 than it would be opposite.