Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonicanada/bb03666081735e7c5f20ba1f3b0479af to your computer and use it in GitHub Desktop.
Save tonicanada/bb03666081735e7c5f20ba1f3b0479af to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"example4_data = {\n",
" \"subject\": ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],\n",
" \"before\": [6.6, 6.5, 9.0, 10.3, 11.3, 8.1, 6.3, 11.6],\n",
" \"after\": [6.8, 2.4, 7.4, 8.5, 8.1, 6.1, 3.4, 2.0]\n",
"}\n",
"df_example3 = pd.DataFrame.from_dict(example4_data)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>subject</th>\n",
" <th>before</th>\n",
" <th>after</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A</td>\n",
" <td>6.6</td>\n",
" <td>6.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>B</td>\n",
" <td>6.5</td>\n",
" <td>2.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>C</td>\n",
" <td>9.0</td>\n",
" <td>7.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>D</td>\n",
" <td>10.3</td>\n",
" <td>8.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>E</td>\n",
" <td>11.3</td>\n",
" <td>8.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>F</td>\n",
" <td>8.1</td>\n",
" <td>6.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>G</td>\n",
" <td>6.3</td>\n",
" <td>3.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>H</td>\n",
" <td>11.6</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject before after\n",
"0 A 6.6 6.8\n",
"1 B 6.5 2.4\n",
"2 C 9.0 7.4\n",
"3 D 10.3 8.5\n",
"4 E 11.3 8.1\n",
"5 F 8.1 6.1\n",
"6 G 6.3 3.4\n",
"7 H 11.6 2.0"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_example3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These are our hypothesis:<br>\n",
"$$\n",
"H0: {\\overline{d}= 0}\n",
"$$\n",
"$$\n",
"H1: {\\overline{d} \\neq 0}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Where d is the mean of the difference between before and after."
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>subject</th>\n",
" <th>before</th>\n",
" <th>after</th>\n",
" <th>diff</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A</td>\n",
" <td>6.6</td>\n",
" <td>6.8</td>\n",
" <td>-0.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>B</td>\n",
" <td>6.5</td>\n",
" <td>2.4</td>\n",
" <td>4.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>C</td>\n",
" <td>9.0</td>\n",
" <td>7.4</td>\n",
" <td>1.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>D</td>\n",
" <td>10.3</td>\n",
" <td>8.5</td>\n",
" <td>1.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>E</td>\n",
" <td>11.3</td>\n",
" <td>8.1</td>\n",
" <td>3.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>F</td>\n",
" <td>8.1</td>\n",
" <td>6.1</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>G</td>\n",
" <td>6.3</td>\n",
" <td>3.4</td>\n",
" <td>2.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>H</td>\n",
" <td>11.6</td>\n",
" <td>2.0</td>\n",
" <td>9.6</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" subject before after diff\n",
"0 A 6.6 6.8 -0.2\n",
"1 B 6.5 2.4 4.1\n",
"2 C 9.0 7.4 1.6\n",
"3 D 10.3 8.5 1.8\n",
"4 E 11.3 8.1 3.2\n",
"5 F 8.1 6.1 2.0\n",
"6 G 6.3 3.4 2.9\n",
"7 H 11.6 2.0 9.6"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Here we compute the difference between (before and after)\n",
"df_example3['diff'] = df_example3.before - df_example3.after\n",
"df_example3"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"mean\": 3.1250000000000004,\n",
" \"std\": 2.911430674329817,\n",
" \"n\": 8\n",
"}\n"
]
}
],
"source": [
"#Now we compute mean and std of the diff column\n",
"mean_example4 = df_example3['diff'].mean()\n",
"std_example4 = df_example3['diff'].std()\n",
"n_example4 = len(df_example3)\n",
"params_example4 = {\n",
" \"mean\": mean_example4,\n",
" \"std\": std_example4,\n",
" \"n\": n_example4\n",
"}\n",
"print(json.dumps(params_example4, indent=4))"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.029346186386568\n"
]
}
],
"source": [
"#Here we compute the standard error\n",
"se_example4 = std_example4/np.sqrt(n_example4)\n",
"print(se_example4)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.035907687160183\n"
]
}
],
"source": [
"#And finally our t-value\n",
"t_value_example4 = mean_example4/se_example4\n",
"print(t_value_example4)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n"
]
}
],
"source": [
"dof_example4 = n_example4-1\n",
"print(dof_example4)"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.009477987786306367"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Using t-student distribtion\n",
"t.sf(t_value_example4, df=dof_example4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\frac{\\alpha}{2} = \\frac{0.05}{2} = 0.025 \\implies \\text{p_value} < 0.025 \\implies 0.009478 < 0.025 \\implies \\text{We can reject H0}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we see, we can reject HO with a p-value of 0.009478. So, based on u it's very likely that hypnotism in reducing pain"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment