{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6f6aa857",
   "metadata": {},
   "source": [
    "# Exercise: Compute summary statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8f9bc8b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1be11d54",
   "metadata": {},
   "source": [
    "# Load the patient data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d2dfebd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('processed_data_predimed.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "09554c84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6245, 18)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "df95a10b",
   "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>patient-id</th>\n",
       "      <th>location-id</th>\n",
       "      <th>sex</th>\n",
       "      <th>age</th>\n",
       "      <th>smoke</th>\n",
       "      <th>bmi</th>\n",
       "      <th>waist</th>\n",
       "      <th>wth</th>\n",
       "      <th>htn</th>\n",
       "      <th>diab</th>\n",
       "      <th>hyperchol</th>\n",
       "      <th>famhist</th>\n",
       "      <th>hormo</th>\n",
       "      <th>p14</th>\n",
       "      <th>toevent</th>\n",
       "      <th>event</th>\n",
       "      <th>group</th>\n",
       "      <th>City</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Female</td>\n",
       "      <td>77</td>\n",
       "      <td>Never</td>\n",
       "      <td>25.92</td>\n",
       "      <td>94</td>\n",
       "      <td>0.657343</td>\n",
       "      <td>Yes</td>\n",
       "      <td>No</td>\n",
       "      <td>Yes</td>\n",
       "      <td>Yes</td>\n",
       "      <td>No</td>\n",
       "      <td>9</td>\n",
       "      <td>5.538672</td>\n",
       "      <td>No</td>\n",
       "      <td>MedDiet + VOO</td>\n",
       "      <td>Madrid</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>Female</td>\n",
       "      <td>68</td>\n",
       "      <td>Never</td>\n",
       "      <td>34.85</td>\n",
       "      <td>150</td>\n",
       "      <td>0.949367</td>\n",
       "      <td>Yes</td>\n",
       "      <td>No</td>\n",
       "      <td>Yes</td>\n",
       "      <td>Yes</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10</td>\n",
       "      <td>3.063655</td>\n",
       "      <td>No</td>\n",
       "      <td>MedDiet + Nuts</td>\n",
       "      <td>Madrid</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>Female</td>\n",
       "      <td>66</td>\n",
       "      <td>Never</td>\n",
       "      <td>37.50</td>\n",
       "      <td>120</td>\n",
       "      <td>0.750000</td>\n",
       "      <td>Yes</td>\n",
       "      <td>Yes</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>6</td>\n",
       "      <td>5.590691</td>\n",
       "      <td>No</td>\n",
       "      <td>MedDiet + Nuts</td>\n",
       "      <td>Madrid</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>Female</td>\n",
       "      <td>77</td>\n",
       "      <td>Never</td>\n",
       "      <td>29.26</td>\n",
       "      <td>93</td>\n",
       "      <td>0.628378</td>\n",
       "      <td>Yes</td>\n",
       "      <td>Yes</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>6</td>\n",
       "      <td>5.456537</td>\n",
       "      <td>No</td>\n",
       "      <td>MedDiet + VOO</td>\n",
       "      <td>Madrid</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Female</td>\n",
       "      <td>60</td>\n",
       "      <td>Never</td>\n",
       "      <td>30.02</td>\n",
       "      <td>104</td>\n",
       "      <td>0.662420</td>\n",
       "      <td>Yes</td>\n",
       "      <td>No</td>\n",
       "      <td>Yes</td>\n",
       "      <td>No</td>\n",
       "      <td>No</td>\n",
       "      <td>9</td>\n",
       "      <td>2.746064</td>\n",
       "      <td>No</td>\n",
       "      <td>Control</td>\n",
       "      <td>Madrid</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   patient-id  location-id     sex  age  smoke    bmi  waist       wth  htn  \\\n",
       "0           1            1  Female   77  Never  25.92     94  0.657343  Yes   \n",
       "1           2            1  Female   68  Never  34.85    150  0.949367  Yes   \n",
       "2           3            1  Female   66  Never  37.50    120  0.750000  Yes   \n",
       "3           4            1  Female   77  Never  29.26     93  0.628378  Yes   \n",
       "4           5            1  Female   60  Never  30.02    104  0.662420  Yes   \n",
       "\n",
       "  diab hyperchol famhist hormo  p14   toevent event           group    City  \n",
       "0   No       Yes     Yes    No    9  5.538672    No   MedDiet + VOO  Madrid  \n",
       "1   No       Yes     Yes   NaN   10  3.063655    No  MedDiet + Nuts  Madrid  \n",
       "2  Yes        No      No    No    6  5.590691    No  MedDiet + Nuts  Madrid  \n",
       "3  Yes        No      No    No    6  5.456537    No   MedDiet + VOO  Madrid  \n",
       "4   No       Yes      No    No    9  2.746064    No         Control  Madrid  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b4f6091",
   "metadata": {},
   "source": [
    "# 1. Did the mediterranean diet help prevent cardiovascular events?\n",
    "\n",
    "To answer this question, we need to compute how many cardiovascular \"events\" occured in each group of participants, separated by the diet they followed.\n",
    "In the data the column `event` contains `Yes` or `No`, indicating if that patient had an cardiovascular event. The column `group` contains which diet they followed.\n",
    "\n",
    "We first convert the column ``event'' to a binary value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "99b21627-1b48-44ee-bda2-312b0718bd59",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['event'] = df['event'].map({'Yes': 1, 'No': 0})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a63e59c5-fe50-433f-a529-f601c795db67",
   "metadata": {},
   "source": [
    "* Now compute the total number of events by diet group. Compare the numbers and see if you can answer the question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "00bb9eb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "group\n",
       "Control           96\n",
       "MedDiet + Nuts    69\n",
       "MedDiet + VOO     83\n",
       "Name: event, dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# your code here:\n",
    "df.groupby('group')['event'].sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b052a40a-ae68-4376-8557-541eafb3face",
   "metadata": {},
   "source": [
    "* Check how many patients had each group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "db946d0f-8204-43a3-853c-41981a9811f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "group\n",
       "Control           2016\n",
       "MedDiet + Nuts    2077\n",
       "MedDiet + VOO     2152\n",
       "Name: event, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# your code here:\n",
    "df.groupby('group').count()['event']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64339449-d766-4a2a-85d3-8aafac5533b7",
   "metadata": {},
   "source": [
    "There were no equal number in each group, so to be precise we need to put the numbers into perspective of the total. For that:\n",
    "* Calculate how many events occured relative to the amount of patients in each group (in percentage). \n",
    "Do this sepearated by diet group."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "13ad4130-2094-4e7a-a416-f0fd6e810413",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "group\n",
       "Control           4.761905\n",
       "MedDiet + Nuts    3.322099\n",
       "MedDiet + VOO     3.856877\n",
       "Name: event, dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# your code here:\n",
    "df.groupby('group')['event'].mean()*100"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9655c0ac-18e9-4297-9f6e-557bfe95ed5e",
   "metadata": {},
   "source": [
    "It seems that the control group had a higher percentage of events than the other two"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1940d3fe",
   "metadata": {},
   "source": [
    "# 2. Smoking\n",
    "\n",
    "Did smoking make a difference in the outcome of the study?\n",
    "Calculate how many events occured by diet group *and* smoking. The idea is that you arrive to a table like this:\n",
    "\n",
    "| group          |   Current |   Former |   Never |\n",
    "|:---------------|----------:|---------:|--------:|\n",
    "| Control        |     ...   |    ...   |   ...   |\n",
    "| MedDiet + Nuts |     ...   |    ...   |   ...   |\n",
    "| MedDiet + VOO  |     ...   |    ...   |   ...   |\n",
    "\n",
    "where each entry in the table has the percentage of events for each group. \n",
    "\n",
    "Hint: use `pivot_table`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5ab4e70e-6261-4a26-8ad9-14eae15be09c",
   "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>smoke</th>\n",
       "      <th>Current</th>\n",
       "      <th>Former</th>\n",
       "      <th>Never</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>group</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Control</th>\n",
       "      <td>4.92</td>\n",
       "      <td>8.04</td>\n",
       "      <td>3.47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MedDiet + Nuts</th>\n",
       "      <td>5.15</td>\n",
       "      <td>3.71</td>\n",
       "      <td>2.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MedDiet + VOO</th>\n",
       "      <td>6.90</td>\n",
       "      <td>5.46</td>\n",
       "      <td>2.55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "smoke           Current  Former  Never\n",
       "group                                 \n",
       "Control            4.92    8.04   3.47\n",
       "MedDiet + Nuts     5.15    3.71   2.73\n",
       "MedDiet + VOO      6.90    5.46   2.55"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# your code here\n",
    "(df.pivot_table(index='group', columns='smoke', values='event')*100).round(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "511c640e-8f0f-449f-af33-85061d89cfd3",
   "metadata": {},
   "source": [
    "# 3. Age differences?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a04ea5c-8a27-4e67-aafa-ba34580a8d7f",
   "metadata": {},
   "source": [
    "Finally, check that there were no big differences in the age between the groups.\n",
    "* Calculate the mean and standard deviation of the patient's age, separated by diet group.\n",
    "\n",
    "You should be getting a table where diet group are in the rows and gender in columns, like this\n",
    "\n",
    "| group          |   Female |   Male |\n",
    "|:---------------|---------:|-------:|\n",
    "| Control        |     68   |   66.4 |\n",
    "| MedDiet + Nuts |     67.4 |   65.8 |\n",
    "| MedDiet + VOO  |     67.7 |   66.1 |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "196fd111-72bc-4b87-b8fb-293547a8c83d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# your code here:\n",
    "df.pivot_table(index='group', columns='smoke', values='age')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
