{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b27d13f8",
   "metadata": {},
   "source": [
    "# Sujet 6 : Autour du Paradoxe de Simpson\n",
    "\n",
    "### Marie-Félicia Béclin\n",
    "\n",
    "En 1972-1974, à Whickham, une ville du nord-est de l'Angleterre, située à environ 6,5 kilomètres au sud-ouest de Newcastle upon Tyne, un sondage d'un sixième des électeurs a été effectué afin d'éclairer des travaux sur les maladies thyroïdiennes et cardiaques (Tunbridge et al. 1977). Une suite de cette étude a été menée vingt ans plus tard (Vanderpump et al. 1995). Certains des résultats avaient trait au tabagisme et cherchaient à savoir si les individus étaient toujours en vie lors de la seconde étude. Par simplicité, nous nous restreindrons aux femmes et parmi celles-ci aux 1314 qui ont été catégorisées comme \"fumant actuellement\" ou \"n'ayant jamais fumé\". Il y avait relativement peu de femmes dans le sondage initial ayant fumé et ayant arrêté depuis (162) et très peu pour lesquelles l'information n'était pas disponible (18). La survie à 20 ans a été déterminée pour l'ensemble des femmes du premier sondage.\n",
    "\n",
    "Les données sont disponibles dans ce fichier CSV. Vous trouverez sur chaque ligne si la personne fume ou non, si elle est vivante ou décédée au moment de la seconde étude, et son âge lors du premier sondage."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4aee39e",
   "metadata": {},
   "source": [
    "# Import des différentes bibliothèques utilisées"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "aba6c422",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "plt.style.use('ggplot')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5fe499c",
   "metadata": {},
   "source": [
    "### Consignes\n",
    "\n",
    "Représentez dans un tableau le nombre total de femmes vivantes et décédées sur la période en fonction de leur habitude de tabagisme. Calculez dans chaque groupe (fumeuses / non fumeuses) le taux de mortalité (le rapport entre le nombre de femmes décédées dans un groupe et le nombre total de femmes dans ce groupe). Vous pourrez proposer une représentation graphique de ces données et calculer des intervalles de confiance si vous le souhaitez. En quoi ce résultat est-il surprenant ?\n",
    "Reprenez la question 1 (effectifs et taux de mortalité) en rajoutant une nouvelle catégorie liée à la classe d'âge. On considérera par exemple les classes suivantes : 18-34 ans, 34-54 ans, 55-64 ans, plus de 65 ans. En quoi ce résultat est-il surprenant ? Arrivez-vous à expliquer ce paradoxe ? De même, vous pourrez proposer une représentation graphique de ces données pour étayer vos explications.\n",
    "\n",
    "Afin d'éviter un biais induit par des regroupements en tranches d'âges arbitraires et non régulières, il est envisageable d'essayer de réaliser une régression logistique. Si on introduit une variable Death valant 1 ou 0 pour indiquer si l'individu est décédé durant la période de 20 ans, on peut étudier le modèle Death ~ Age pour étudier la probabilité de décès en fonction de l'âge selon que l'on considère le groupe des fumeuses ou des non fumeuses. Ces régressions vous permettent-elles de conclure sur la nocivité du tabagisme ? Vous pourrez proposer une représentation graphique de ces régressions (en n'omettant pas les régions de confiance)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d77e6e53",
   "metadata": {},
   "source": [
    "# I) Import des données"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ea2c1273",
   "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>Smoker</th>\n",
       "      <th>Status</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Yes</td>\n",
       "      <td>Alive</td>\n",
       "      <td>21.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Yes</td>\n",
       "      <td>Alive</td>\n",
       "      <td>19.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>No</td>\n",
       "      <td>Dead</td>\n",
       "      <td>57.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>No</td>\n",
       "      <td>Alive</td>\n",
       "      <td>47.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Yes</td>\n",
       "      <td>Alive</td>\n",
       "      <td>81.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Smoker Status   Age\n",
       "0    Yes  Alive  21.0\n",
       "1    Yes  Alive  19.3\n",
       "2     No   Dead  57.5\n",
       "3     No  Alive  47.1\n",
       "4    Yes  Alive  81.4"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_file = \"Subject6_smoking.csv\"\n",
    "data  = pd.read_csv(data_file, skiprows=0)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c568934",
   "metadata": {},
   "source": [
    "# II) Vérification et gestion des données manquantes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "856b90b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({ \"smoker\" : data[\"Smoker\"].apply(lambda val : 1 if val==\"Yes\" else 0),\n",
    "                    \"alive\" : data[\"Status\"].apply(lambda val : 1 if val==\"Alive\" else 0),\n",
    "                   \"age\": data[\"Age\"]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "5ce60288",
   "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>smoker</th>\n",
       "      <th>alive</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1314.000000</td>\n",
       "      <td>1314.000000</td>\n",
       "      <td>1314.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.442922</td>\n",
       "      <td>0.719178</td>\n",
       "      <td>47.359361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.496921</td>\n",
       "      <td>0.449572</td>\n",
       "      <td>19.160667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>18.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>31.300000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>44.800000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>60.600000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>89.900000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            smoker        alive          age\n",
       "count  1314.000000  1314.000000  1314.000000\n",
       "mean      0.442922     0.719178    47.359361\n",
       "std       0.496921     0.449572    19.160667\n",
       "min       0.000000     0.000000    18.000000\n",
       "25%       0.000000     0.000000    31.300000\n",
       "50%       0.000000     1.000000    44.800000\n",
       "75%       1.000000     1.000000    60.600000\n",
       "max       1.000000     1.000000    89.900000"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a9d7d5b",
   "metadata": {},
   "source": [
    "Il n'y a pas de données manquantes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d201f4cf",
   "metadata": {},
   "source": [
    "# III) Première visualisation des données"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4b304b34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Le nombre total de femmes dans l'étude est 1314\n",
      "Le nombre de femme morte pendant l'étude est 369\n",
      "Le nombre de femme encore en vie après l'étude est 945\n",
      "Le nombre de femme non-fumeuse 732\n",
      "Le nombre de femme fumeuse 582\n"
     ]
    }
   ],
   "source": [
    "print(\"Le nombre total de femmes dans l'étude est \" +str(len(df[\"alive\"])))\n",
    "print(\"Le nombre de femme morte pendant l'étude est \"+ str(np.sum(df[\"alive\"] ==0)))\n",
    "print(\"Le nombre de femme encore en vie après l'étude est \"+ str(np.sum(df[\"alive\"])))\n",
    "print(\"Le nombre de femme non-fumeuse \"+ str(np.sum(df[\"smoker\"] ==0)))\n",
    "print(\"Le nombre de femme fumeuse \"+ str(np.sum(df[\"smoker\"])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cc456fe9",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_smoker = df[df[\"smoker\"] ==1]\n",
    "df_nonsmoker = df[df[\"smoker\"] ==0]\n",
    "df_alive = df[df[\"alive\"] ==1]\n",
    "df_dead = df[df[\"alive\"] ==0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e91fd4c9",
   "metadata": {},
   "source": [
    "On définit une fonction qui calcul de taux de mortalité pour n'importe quel jeux de données comportant la colone \"alive\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c88f2d79",
   "metadata": {},
   "outputs": [],
   "source": [
    "def mortalite(df):\n",
    "    return (np.sum(df[\"alive\"] ==0)/len(df[\"alive\"]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65dab701",
   "metadata": {},
   "source": [
    "On définit une fonction qui calcul de taux de fumeuses pour n'importe quel jeux de données comportant la colone \"smoker\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f5155e8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def smoke_rate(df):\n",
    "    return (np.sum(df[\"smoker\"] ==1)/len(df[\"smoker\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1ab15069",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Pourcentage des participantes par âge des groupes fumeuses et non-fumeuses')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 29756 characters]\n",
      "text/plain": [
       "<Figure size 1080x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots( figsize =(15,7))\n",
    "ax.hist(df_smoker['age'],\n",
    "        bins = int(np.max(df_smoker['age']) - np.min(df_smoker['age'])),\n",
    "        density = True,\n",
    "        alpha = 0.5, \n",
    "        label =\"Fumeuses\" )\n",
    "\n",
    "ax.hist(df_nonsmoker['age'], \n",
    "        bins = int(np.max(df_nonsmoker['age']) - np.min(df_nonsmoker['age'])), \n",
    "        density = True, \n",
    "        alpha = 0.5, \n",
    "        label = \"Non Fumeuses\")\n",
    "plt.legend()\n",
    "plt.title(\"Pourcentage des participantes par âge des groupes fumeuses et non-fumeuses\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c3e6886c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Pourcentage des participantes par âge des femmes en vie et décédées')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 28852 characters]\n",
      "text/plain": [
       "<Figure size 1080x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots( figsize =(15,7))\n",
    "ax.hist(df_alive['age'], \n",
    "        bins = int(np.max(df_alive['age']) - np.min(df_alive['age'])),\n",
    "        density = True,\n",
    "        alpha = 0.5, \n",
    "        label =\"En vie\" )\n",
    "ax.hist(df_dead['age'],\n",
    "        bins = int(np.max(df_dead['age']) - np.min(df_dead['age'])), \n",
    "        density = True, \n",
    "        alpha = 0.5, \n",
    "        label = \"Décédées\")\n",
    "plt.legend()\n",
    "plt.title(\"Pourcentage des participantes par âge des femmes en vie et décédées\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0f85bd8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\BECLIN Marie-Felicia\\AppData\\Roaming\\Python\\Python37\\site-packages\\matplotlib\\cbook\\__init__.py:1376: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
      "  X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 9860 characters]\n",
      "text/plain": [
       "<Figure size 1080x504 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots( figsize =(15,7))\n",
    "ax.boxplot([df_alive[\"age\"],df_dead[\"age\"]])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61890097",
   "metadata": {},
   "source": [
    "Les personnes décédées sont plus agées"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0a47e120",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'whiskers': [<matplotlib.lines.Line2D at 0x24302698ac8>,\n",
       "  <matplotlib.lines.Line2D at 0x24302684048>],\n",
       " 'caps': [<matplotlib.lines.Line2D at 0x2430277de48>,\n",
       "  <matplotlib.lines.Line2D at 0x243026f9748>],\n",
       " 'boxes': [<matplotlib.lines.Line2D at 0x243027737c8>],\n",
       " 'medians': [<matplotlib.lines.Line2D at 0x243026f9f08>],\n",
       " 'fliers': [<matplotlib.lines.Line2D at 0x2430280af88>],\n",
       " 'means': []}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.boxplot(df_alive[\"age\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12d505cb",
   "metadata": {},
   "source": [
    "# IV) Première analyse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "45d218f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Le taux de mortalité des femmes non-fumeuses 0.31420765027322406\n",
      "Le taux de mortalité des femmes fumeuses 0.23883161512027493\n",
      "Le taux de fumeuses parmi les femmes décédées 0.37669376693766937\n",
      "Le taux de fumeuses parmi les femmes encore en vie 0.4687830687830688\n"
     ]
    }
   ],
   "source": [
    "print(\"Le taux de mortalité des femmes non-fumeuses \"+ str(mortalite(df_nonsmoker)))\n",
    "print(\"Le taux de mortalité des femmes fumeuses \"+ str(mortalite(df_smoker)))\n",
    "print(\"Le taux de fumeuses parmi les femmes décédées \"+ str(smoke_rate(df_dead)))\n",
    "print(\"Le taux de fumeuses parmi les femmes encore en vie \"+ str(smoke_rate(df_alive)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5641b18b",
   "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>Fumeuse</th>\n",
       "      <th>Non Fumeuse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>décédées</th>\n",
       "      <td>23.883162</td>\n",
       "      <td>31.420765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>en vie</th>\n",
       "      <td>76.116838</td>\n",
       "      <td>68.579235</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Fumeuse  Non Fumeuse\n",
       "décédées  23.883162    31.420765\n",
       "en vie    76.116838    68.579235"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 9216 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tab = pd.DataFrame({\"Fumeuse\" : [mortalite(df_smoker)*100 , (1-mortalite(df_smoker))*100],\n",
    "\"Non Fumeuse\": [mortalite(df_nonsmoker)*100 , (1-mortalite(df_nonsmoker))*100]}, index = [\"décédées\", \"en vie\"])\n",
    "plt.bar([\"Fumeuse\", \"Non-fumeuse\"],tab.loc[\"décédées\"], label = 'décédées' )\n",
    "plt.bar([\"Fumeuse\", \"Non-fumeuse\"],tab.loc[\"en vie\"], bottom = tab.loc[\"décédées\"], label = 'en vie')\n",
    "plt.legend()\n",
    "tab.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35f303a9",
   "metadata": {},
   "source": [
    "Le taux de décés est plus important chez les non-fumeuses, c'est assez surprenant. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0296de1",
   "metadata": {},
   "source": [
    "# V) Analyse par classe d'âge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fa0f643c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"classe\"] = df[\"age\"].apply(lambda a : 1 if a<35 else 2 if a<55 else 3 if a <65 else 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8401743f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_smoker = df[df[\"smoker\"] ==1]\n",
    "df_nonsmoker = df[df[\"smoker\"] ==0]\n",
    "df_alive = df[df[\"alive\"] ==1]\n",
    "df_dead = df[df[\"alive\"] ==0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "970e42e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, \"Mortalités des femmes non-fumeuses et fumeuses en fonction de leur classe d'âge\")"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 27808 characters]\n",
      "text/plain": [
       "<Figure size 1080x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "barWidth = 0.25\n",
    "mortalite_smoker = [mortalite(df_smoker[df_smoker[\"classe\"] == i]) for i in range(1,5)]\n",
    "mortalite_nonsmoker = [mortalite(df_nonsmoker[df_nonsmoker[\"classe\"] == i]) for i in range(1,5)]\n",
    "classe = [\"18-34 ans\", \"34-54 ans\", \"55-64 ans\", \"plus de 65 ans\"]\n",
    "r1 = range(len(mortalite_smoker))\n",
    "r2 = [x + barWidth for x in r1]\n",
    "fig, ax = plt.subplots(1,1,figsize=(15,10))\n",
    "plt.bar(r1,mortalite_smoker, width = barWidth,label = 'Fumeuses' )\n",
    "plt.bar(r2,mortalite_nonsmoker,width = barWidth, label = 'Non - Fumeuses')\n",
    "plt.xticks([r + barWidth / 2 for r in range(len(mortalite_smoker))],classe)\n",
    "plt.legend()\n",
    "plt.title(\"Mortalités des femmes non-fumeuses et fumeuses en fonction de leur classe d'âge\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9bdc254c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "la proportion de femme agées de plus de 65 ans chez les fumeuses est 8%\n",
      "la proportion de femme agées de plus de 65 ans chez les non-fumeuses est 26%\n"
     ]
    }
   ],
   "source": [
    "print(\"la proportion de femme agées de plus de 65 ans chez les fumeuses est \" + str(int(100*np.sum(df_smoker[\"classe\"]==4)/len(df_smoker[\"classe\"]))) +\"%\")\n",
    "print(\"la proportion de femme agées de plus de 65 ans chez les non-fumeuses est \"+ str(int(100*np.sum(df_nonsmoker[\"classe\"]==4)/len(df_nonsmoker[\"classe\"])))+\"%\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "932c7506",
   "metadata": {},
   "source": [
    "La mortalité des fumeuses est plus importante chez les jeunes femmes que chez les non-fumeuses. En revanche pour les plus de 65 ans la mortalité est presque la même. La proportion des femmes agées de plus 65 ans est plus grande dans la classe des non-fumeuse que des fumeuses, cela fait augmenter la mortalité dans ce groupe. Le taux de mortalité des non fumeuses est supérieur à celle des fumeuses car le groupe est plus agé. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "113f39da",
   "metadata": {},
   "source": [
    "# VI) Analyse par classe d'âge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6228d5f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import confusion_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c375e66",
   "metadata": {},
   "source": [
    "## Regression logistique sans la variable fumeur/non-fumeur"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9af4a729",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "82.97872340425532\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\BECLIN Marie-Felicia\\AppData\\Roaming\\Python\\Python37\\site-packages\\sklearn\\utils\\validation.py:73: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  return f(**kwargs)\n"
     ]
    }
   ],
   "source": [
    "X = df[[\"smoker\",\"age\"]]\n",
    "y = df[[\"alive\"]]\n",
    "\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, y, random_state = 0)\n",
    "\n",
    "modele_regLog = LogisticRegression(random_state = 0, solver = 'liblinear', multi_class = 'auto')\n",
    "\n",
    "modele_regLog.fit(x_train,y_train)\n",
    "\n",
    "\n",
    "precision = modele_regLog.score(x_test,y_test)\n",
    "print(precision*100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "ba3de71d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 51  37]\n",
      " [ 19 222]]\n"
     ]
    }
   ],
   "source": [
    "matrix_threshold = confusion_matrix(\n",
    "    y_true=y_test, y_pred=modele_regLog.predict(x_test)\n",
    ")\n",
    "print(matrix_threshold)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "7e7a8b72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, \"probabilité prédite de décés en fonction de l'age\")"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38532 characters]\n",
      "text/plain": [
       "<Figure size 1080x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model_output = modele_regLog.predict_proba(x_test)\n",
    "probas_pred = model_output[:, np.where(modele_regLog.classes_ == 0 )[0]]\n",
    "\n",
    "fig, ax = plt.subplots(1,1,figsize = (15,10))\n",
    "\n",
    "plt.scatter(x_test[x_test['smoker'] ==1][\"age\"],probas_pred[x_test['smoker'] ==1], label = 'fumeur'  )\n",
    "plt.scatter(x_test[x_test['smoker'] ==0][\"age\"],probas_pred[x_test['smoker'] ==0], label = 'non - fumeur'  )\n",
    "plt.legend()\n",
    "plt.title(\"probabilité prédite de décés en fonction de l'age\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32ab8709",
   "metadata": {},
   "source": [
    "La courbe de fumeur est au dessus. La probabilité de décès est donc plus élévée pour cette catégorie"
   ]
  }
 ],
 "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.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
