Skip to content

Instantly share code, notes, and snippets.

@dcasmr
Created November 22, 2018 07:39
Show Gist options
  • Save dcasmr/52f40cd88d4a3b3023c24e40b1e10baf to your computer and use it in GitHub Desktop.
Save dcasmr/52f40cd88d4a3b3023c24e40b1e10baf to your computer and use it in GitHub Desktop.
Wilcoxon Signed Rank test in Python 3
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import scipy.stats as stats\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.read_csv(\"C:/Users/ouaga/Dropbox/PythonScripts/Stats_tests/Life_Expectancy_Before_After.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>WHO region</th>\n",
" <th>Male_life_2010</th>\n",
" <th>Male_life_2015</th>\n",
" <th>Female_life_2010</th>\n",
" <th>Female_life_2015</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Africa</td>\n",
" <td>55.6</td>\n",
" <td>58.3</td>\n",
" <td>58.4</td>\n",
" <td>61.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Americas</td>\n",
" <td>72.3</td>\n",
" <td>74.0</td>\n",
" <td>78.4</td>\n",
" <td>79.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>South-East Asia</td>\n",
" <td>65.8</td>\n",
" <td>67.3</td>\n",
" <td>68.7</td>\n",
" <td>70.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Europe</td>\n",
" <td>71.5</td>\n",
" <td>73.2</td>\n",
" <td>79.0</td>\n",
" <td>80.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Eastern Mediterranean</td>\n",
" <td>66.5</td>\n",
" <td>67.3</td>\n",
" <td>69.3</td>\n",
" <td>70.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Western Pacific</td>\n",
" <td>73.5</td>\n",
" <td>74.5</td>\n",
" <td>77.8</td>\n",
" <td>78.7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" WHO region Male_life_2010 Male_life_2015 Female_life_2010 \\\n",
"0 Africa 55.6 58.3 58.4 \n",
"1 Americas 72.3 74.0 78.4 \n",
"2 South-East Asia 65.8 67.3 68.7 \n",
"3 Europe 71.5 73.2 79.0 \n",
"4 Eastern Mediterranean 66.5 67.3 69.3 \n",
"5 Western Pacific 73.5 74.5 77.8 \n",
"\n",
" Female_life_2015 \n",
"0 61.8 \n",
"1 79.9 \n",
"2 70.7 \n",
"3 80.2 \n",
"4 70.3 \n",
"5 78.7 "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compute the Wilcoxon signed Rank test- with nan_policy='omit' missing values are deleted otherwise we receive nan"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Paired t-test for Males"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The test statistic is: 0.0\n",
"The p-value is: 0.02728\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\ouaga\\Anaconda3\\lib\\site-packages\\scipy\\stats\\morestats.py:2397: UserWarning: Warning: sample size too small for normal approximation.\n",
" warnings.warn(\"Warning: sample size too small for normal approximation.\")\n"
]
}
],
"source": [
"(test_statistic, p_value) = stats.wilcoxon(df['Male_life_2010'],df['Male_life_2015'])\n",
"print(\"The test statistic is: \", round(test_statistic,5))\n",
"print(\"The p-value is: \", round(p_value,5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Paired t-test for females"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The test statistic is: 0.0\n",
"The p-value is: 0.02771\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\ouaga\\Anaconda3\\lib\\site-packages\\scipy\\stats\\morestats.py:2397: UserWarning: Warning: sample size too small for normal approximation.\n",
" warnings.warn(\"Warning: sample size too small for normal approximation.\")\n"
]
}
],
"source": [
"(test_statistic, p_value) = stats.wilcoxon(df['Female_life_2010'],df['Female_life_2015'])\n",
"print(\"The test statistic is: \", round(test_statistic,5))\n",
"print(\"The p-value is: \", round(p_value,5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment