{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Risk Analysis of the Space Shuttle: Pre-Challenger Prediction of Failure"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this document we reperform some of the analysis provided in \n",
    "*Risk Analysis of the Space Shuttle: Pre-Challenger Prediction of Failure* by *Siddhartha R. Dalal, Edward B. Fowlkes, Bruce Hoadley* published in *Journal of the American Statistical Association*, Vol. 84, No. 408 (Dec., 1989), pp. 945-957 and available at http://www.jstor.org/stable/2290069. \n",
    "\n",
    "On the fourth page of this article, they indicate that the maximum likelihood estimates of the logistic regression using only temperature are: $\\hat{\\alpha}=5.085$ and $\\hat{\\beta}=-0.1156$ and their asymptotic standard errors are $s_{\\hat{\\alpha}}=3.052$ and $s_{\\hat{\\beta}}=0.047$. The Goodness of fit indicated for this model was $G^2=18.086$ with 21 degrees of freedom. Our goal is to reproduce the computation behind these values and the Figure 4 of this article, possibly in a nicer looking way."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Technical information on the computer on which the analysis is run"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will be using the python3 language using the pandas, statsmodels, numpy, matplotlib and seaborn libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) \n",
      "[GCC 7.2.0]\n",
      "uname_result(system='Linux', node='3a716011d2b6', release='4.4.0-116-generic', version='#140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018', machine='x86_64', processor='x86_64')\n",
      "IPython 6.4.0\n",
      "IPython.core.release 6.4.0\n",
      "PIL 5.2.0\n",
      "PIL.Image 5.2.0\n",
      "PIL._version 5.2.0\n",
      "_csv 1.0\n",
      "_ctypes 1.1.0\n",
      "_curses b'2.2'\n",
      "decimal 1.70\n",
      "argparse 1.1\n",
      "backcall 0.1.0\n",
      "cffi 1.11.5\n",
      "csv 1.0\n",
      "ctypes 1.1.0\n",
      "cycler 0.10.0\n",
      "dateutil 2.7.3\n",
      "decimal 1.70\n",
      "decorator 4.3.0\n",
      "distutils 3.6.4\n",
      "ipaddress 1.0\n",
      "ipykernel 4.8.2\n",
      "ipykernel._version 4.8.2\n",
      "ipython_genutils 0.2.0\n",
      "ipython_genutils._version 0.2.0\n",
      "ipywidgets 7.2.1\n",
      "ipywidgets._version 7.2.1\n",
      "jedi 0.12.1\n",
      "json 2.0.9\n",
      "jupyter_client 5.2.3\n",
      "jupyter_client._version 5.2.3\n",
      "jupyter_core 4.4.0\n",
      "jupyter_core.version 4.4.0\n",
      "kiwisolver 1.0.1\n",
      "logging 0.5.1.2\n",
      "matplotlib 2.2.2\n",
      "matplotlib.backends.backend_agg 2.2.2\n",
      "numpy 1.13.3\n",
      "numpy.core 1.13.3\n",
      "numpy.core.multiarray 3.1\n",
      "numpy.core.umath b'0.4.0'\n",
      "numpy.lib 1.13.3\n",
      "numpy.linalg._umath_linalg b'0.1.5'\n",
      "numpy.matlib 1.13.3\n",
      "optparse 1.5.3\n",
      "pandas 0.22.0\n",
      "_libjson 1.33\n",
      "parso 0.3.0\n",
      "patsy 0.5.0\n",
      "patsy.version 0.5.0\n",
      "pexpect 4.6.0\n",
      "pickleshare 0.7.4\n",
      "platform 1.0.8\n",
      "prompt_toolkit 1.0.15\n",
      "ptyprocess 0.6.0\n",
      "pygments 2.2.0\n",
      "pyparsing 2.2.0\n",
      "pytz 2018.5\n",
      "re 2.2.1\n",
      "scipy 1.1.0\n",
      "scipy._lib.decorator 4.0.5\n",
      "scipy._lib.six 1.2.0\n",
      "scipy.fftpack._fftpack b'$Revision: $'\n",
      "scipy.fftpack.convolve b'$Revision: $'\n",
      "scipy.integrate._dop b'$Revision: $'\n",
      "scipy.integrate._ode $Id$\n",
      "scipy.integrate._odepack  1.9 \n",
      "scipy.integrate._quadpack  1.13 \n",
      "scipy.integrate.lsoda b'$Revision: $'\n",
      "scipy.integrate.vode b'$Revision: $'\n",
      "scipy.interpolate._fitpack  1.7 \n",
      "scipy.interpolate.dfitpack b'$Revision: $'\n",
      "scipy.linalg 0.4.9\n",
      "scipy.linalg._fblas b'$Revision: $'\n",
      "scipy.linalg._flapack b'$Revision: $'\n",
      "scipy.linalg._flinalg b'$Revision: $'\n",
      "scipy.ndimage 2.0\n",
      "scipy.optimize._cobyla b'$Revision: $'\n",
      "scipy.optimize._lbfgsb b'$Revision: $'\n",
      "scipy.optimize._minpack  1.10 \n",
      "scipy.optimize._nnls b'$Revision: $'\n",
      "scipy.optimize._slsqp b'$Revision: $'\n",
      "scipy.optimize.minpack2 b'$Revision: $'\n",
      "scipy.signal.spline 0.2\n",
      "scipy.sparse.linalg.eigen.arpack._arpack b'$Revision: $'\n",
      "scipy.sparse.linalg.isolve._iterative b'$Revision: $'\n",
      "scipy.special.specfun b'$Revision: $'\n",
      "scipy.stats.mvn b'$Revision: $'\n",
      "scipy.stats.statlib b'$Revision: $'\n",
      "seaborn 0.8.1\n",
      "seaborn.external.husl 2.1.0\n",
      "seaborn.external.six 1.10.0\n",
      "six 1.11.0\n",
      "statsmodels 0.9.0\n",
      "statsmodels.__init__ 0.9.0\n",
      "traitlets 4.3.2\n",
      "traitlets._version 4.3.2\n",
      "urllib.request 3.6\n",
      "zlib 1.0\n",
      "zmq 17.1.0\n",
      "zmq.sugar 17.1.0\n",
      "zmq.sugar.version 17.1.0\n"
     ]
    }
   ],
   "source": [
    "def print_imported_modules():\n",
    "    import sys\n",
    "    for name, val in sorted(sys.modules.items()):\n",
    "        if(hasattr(val, '__version__')): \n",
    "            print(val.__name__, val.__version__)\n",
    "#        else:\n",
    "#            print(val.__name__, \"(unknown version)\")\n",
    "def print_sys_info():\n",
    "    import sys\n",
    "    import platform\n",
    "    print(sys.version)\n",
    "    print(platform.uname())\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import statsmodels.api as sm\n",
    "import seaborn as sns\n",
    "\n",
    "print_sys_info()\n",
    "print_imported_modules()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading and inspecting data\n",
    "Let's start by reading data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "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>Date</th>\n",
       "      <th>Count</th>\n",
       "      <th>Temperature</th>\n",
       "      <th>Pressure</th>\n",
       "      <th>Malfunction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4/12/81</td>\n",
       "      <td>6</td>\n",
       "      <td>66</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11/12/81</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3/22/82</td>\n",
       "      <td>6</td>\n",
       "      <td>69</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11/11/82</td>\n",
       "      <td>6</td>\n",
       "      <td>68</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4/04/83</td>\n",
       "      <td>6</td>\n",
       "      <td>67</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6/18/82</td>\n",
       "      <td>6</td>\n",
       "      <td>72</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>8/30/83</td>\n",
       "      <td>6</td>\n",
       "      <td>73</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>11/28/83</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2/03/84</td>\n",
       "      <td>6</td>\n",
       "      <td>57</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>4/06/84</td>\n",
       "      <td>6</td>\n",
       "      <td>63</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>8/30/84</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>10/05/84</td>\n",
       "      <td>6</td>\n",
       "      <td>78</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>11/08/84</td>\n",
       "      <td>6</td>\n",
       "      <td>67</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1/24/85</td>\n",
       "      <td>6</td>\n",
       "      <td>53</td>\n",
       "      <td>200</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>4/12/85</td>\n",
       "      <td>6</td>\n",
       "      <td>67</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>4/29/85</td>\n",
       "      <td>6</td>\n",
       "      <td>75</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>6/17/85</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>7/2903/85</td>\n",
       "      <td>6</td>\n",
       "      <td>81</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>8/27/85</td>\n",
       "      <td>6</td>\n",
       "      <td>76</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>10/03/85</td>\n",
       "      <td>6</td>\n",
       "      <td>79</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>10/30/85</td>\n",
       "      <td>6</td>\n",
       "      <td>75</td>\n",
       "      <td>200</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>11/26/85</td>\n",
       "      <td>6</td>\n",
       "      <td>76</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1/12/86</td>\n",
       "      <td>6</td>\n",
       "      <td>58</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date  Count  Temperature  Pressure  Malfunction\n",
       "0     4/12/81      6           66        50            0\n",
       "1    11/12/81      6           70        50            1\n",
       "2     3/22/82      6           69        50            0\n",
       "3    11/11/82      6           68        50            0\n",
       "4     4/04/83      6           67        50            0\n",
       "5     6/18/82      6           72        50            0\n",
       "6     8/30/83      6           73       100            0\n",
       "7    11/28/83      6           70       100            0\n",
       "8     2/03/84      6           57       200            1\n",
       "9     4/06/84      6           63       200            1\n",
       "10    8/30/84      6           70       200            1\n",
       "11   10/05/84      6           78       200            0\n",
       "12   11/08/84      6           67       200            0\n",
       "13    1/24/85      6           53       200            2\n",
       "14    4/12/85      6           67       200            0\n",
       "15    4/29/85      6           75       200            0\n",
       "16    6/17/85      6           70       200            0\n",
       "17  7/2903/85      6           81       200            0\n",
       "18    8/27/85      6           76       200            0\n",
       "19   10/03/85      6           79       200            0\n",
       "20   10/30/85      6           75       200            2\n",
       "21   11/26/85      6           76       200            0\n",
       "22    1/12/86      6           58       200            1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"https://app-learninglab.inria.fr/moocrr/gitlab/moocrr-session3/moocrr-reproducibility-study/blob/master/data/shuttle.csv\")\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We know from our previous experience on this data set that filtering data is a really bad idea. We will therefore process it as such."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 8436 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "pd.set_option('mode.chained_assignment',None) # this removes a useless warning from pandas\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "data[\"Frequency\"]=data.Malfunction/data.Count\n",
    "data.plot(x=\"Temperature\",y=\"Frequency\",kind=\"scatter\",ylim=[0,1])\n",
    "plt.grid(True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Logistic regression\n",
    "\n",
    "Let's assume O-rings independently fail with the same probability which solely depends on temperature. A logistic regression should allow us to estimate the influence of temperature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>Generalized Linear Model Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>      <td>Frequency</td>    <th>  No. Observations:  </th>  <td>    23</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                 <td>GLM</td>       <th>  Df Residuals:      </th>  <td>    21</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model Family:</th>       <td>Binomial</td>     <th>  Df Model:          </th>  <td>     1</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Link Function:</th>        <td>logit</td>      <th>  Scale:             </th> <td>  1.0000</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>               <td>IRLS</td>       <th>  Log-Likelihood:    </th> <td> -3.9210</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>           <td>Wed, 24 Oct 2018</td> <th>  Deviance:          </th> <td>  3.0144</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>               <td>11:05:55</td>     <th>  Pearson chi2:      </th>  <td>  5.00</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Iterations:</th>         <td>6</td>        <th>  Covariance Type:   </th> <td>nonrobust</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "       <td></td>          <th>coef</th>     <th>std err</th>      <th>z</th>      <th>P>|z|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Intercept</th>   <td>    5.0850</td> <td>    7.477</td> <td>    0.680</td> <td> 0.496</td> <td>   -9.570</td> <td>   19.740</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Temperature</th> <td>   -0.1156</td> <td>    0.115</td> <td>   -1.004</td> <td> 0.316</td> <td>   -0.341</td> <td>    0.110</td>\n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                 Generalized Linear Model Regression Results                  \n",
       "==============================================================================\n",
       "Dep. Variable:              Frequency   No. Observations:                   23\n",
       "Model:                            GLM   Df Residuals:                       21\n",
       "Model Family:                Binomial   Df Model:                            1\n",
       "Link Function:                  logit   Scale:                          1.0000\n",
       "Method:                          IRLS   Log-Likelihood:                -3.9210\n",
       "Date:                Wed, 24 Oct 2018   Deviance:                       3.0144\n",
       "Time:                        11:05:55   Pearson chi2:                     5.00\n",
       "No. Iterations:                     6   Covariance Type:             nonrobust\n",
       "===============================================================================\n",
       "                  coef    std err          z      P>|z|      [0.025      0.975]\n",
       "-------------------------------------------------------------------------------\n",
       "Intercept       5.0850      7.477      0.680      0.496      -9.570      19.740\n",
       "Temperature    -0.1156      0.115     -1.004      0.316      -0.341       0.110\n",
       "===============================================================================\n",
       "\"\"\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import statsmodels.api as sm\n",
    "\n",
    "data[\"Success\"]=data.Count-data.Malfunction\n",
    "data[\"Intercept\"]=1\n",
    "\n",
    "logmodel=sm.GLM(data['Frequency'], data[['Intercept','Temperature']], \n",
    "                family=sm.families.Binomial(sm.families.links.logit)).fit()\n",
    "\n",
    "logmodel.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The maximum likelyhood estimator of the intercept and of Temperature are thus $\\hat{\\alpha}=5.0849$ and $\\hat{\\beta}=-0.1156$. This **corresponds** to the values from the article of Dalal *et al.* The standard errors are $s_{\\hat{\\alpha}} = 7.477$ and $s_{\\hat{\\beta}} = 0.115$, which is **different** from the $3.052$ and $0.04702$ reported by Dallal *et al.* The deviance is $3.01444$ with 21 degrees of freedom. I cannot find any value similar to the Goodness of fit ($G^2=18.086$) reported by Dalal *et al.* There seems to be something wrong. Oh I know, I haven't indicated that my observations are actually the result of 6 observations for each rocket launch. Let's indicate these weights (since the weights are always the same throughout all experiments, it does not change the estimates of the fit but it does influence the variance estimates)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>Generalized Linear Model Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>      <td>Frequency</td>    <th>  No. Observations:  </th>  <td>    23</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                 <td>GLM</td>       <th>  Df Residuals:      </th>  <td>    21</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model Family:</th>       <td>Binomial</td>     <th>  Df Model:          </th>  <td>     1</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Link Function:</th>        <td>logit</td>      <th>  Scale:             </th> <td>  1.0000</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>               <td>IRLS</td>       <th>  Log-Likelihood:    </th> <td> -23.526</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>           <td>Wed, 24 Oct 2018</td> <th>  Deviance:          </th> <td>  18.086</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>               <td>11:05:55</td>     <th>  Pearson chi2:      </th>  <td>  30.0</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Iterations:</th>         <td>6</td>        <th>  Covariance Type:   </th> <td>nonrobust</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "       <td></td>          <th>coef</th>     <th>std err</th>      <th>z</th>      <th>P>|z|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Intercept</th>   <td>    5.0850</td> <td>    3.052</td> <td>    1.666</td> <td> 0.096</td> <td>   -0.898</td> <td>   11.068</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Temperature</th> <td>   -0.1156</td> <td>    0.047</td> <td>   -2.458</td> <td> 0.014</td> <td>   -0.208</td> <td>   -0.023</td>\n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                 Generalized Linear Model Regression Results                  \n",
       "==============================================================================\n",
       "Dep. Variable:              Frequency   No. Observations:                   23\n",
       "Model:                            GLM   Df Residuals:                       21\n",
       "Model Family:                Binomial   Df Model:                            1\n",
       "Link Function:                  logit   Scale:                          1.0000\n",
       "Method:                          IRLS   Log-Likelihood:                -23.526\n",
       "Date:                Wed, 24 Oct 2018   Deviance:                       18.086\n",
       "Time:                        11:05:55   Pearson chi2:                     30.0\n",
       "No. Iterations:                     6   Covariance Type:             nonrobust\n",
       "===============================================================================\n",
       "                  coef    std err          z      P>|z|      [0.025      0.975]\n",
       "-------------------------------------------------------------------------------\n",
       "Intercept       5.0850      3.052      1.666      0.096      -0.898      11.068\n",
       "Temperature    -0.1156      0.047     -2.458      0.014      -0.208      -0.023\n",
       "===============================================================================\n",
       "\"\"\""
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "logmodel=sm.GLM(data['Frequency'], data[['Intercept','Temperature']], \n",
    "                family=sm.families.Binomial(sm.families.links.logit),\n",
    "                var_weights=data['Count']).fit()\n",
    "\n",
    "logmodel.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Good, now I have recovered the asymptotic standard errors $s_{\\hat{\\alpha}}=3.052$ and $s_{\\hat{\\beta}}=0.047$.\n",
    "The Goodness of fit (Deviance) indicated for this model is $G^2=18.086$ with 21 degrees of freedom (Df Residuals).\n",
    "\n",
    "**I have therefore managed to fully replicate the results of the Dalal *et al.* article**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Predicting failure probability\n",
    "The temperature when launching the shuttle was 31°F. Let's try to estimate the failure probability for such temperature using our model.:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15652 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "data_pred = pd.DataFrame({'Temperature': np.linspace(start=30, stop=90, num=121), 'Intercept': 1})\n",
    "data_pred['Frequency'] = logmodel.predict(data_pred)\n",
    "data_pred.plot(x=\"Temperature\",y=\"Frequency\",kind=\"line\",ylim=[0,1])\n",
    "plt.scatter(x=data[\"Temperature\"],y=data[\"Frequency\"])\n",
    "plt.grid(True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hideCode": false,
    "hidePrompt": false,
    "scrolled": true
   },
   "source": [
    "This figure is very similar to the Figure 4 of Dalal *et al.* **I have managed to replicate the Figure 4 of the Dalal *et al.* article.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Computing and plotting uncertainty"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Following the documentation of [Seaborn](https://seaborn.pydata.org/generated/seaborn.regplot.html), I use regplot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21944 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.set(color_codes=True)\n",
    "plt.xlim(30,90)\n",
    "plt.ylim(0,1)\n",
    "sns.regplot(x='Temperature', y='Frequency', data=data, logistic=True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**I think I have managed to correctly compute and plot the uncertainty of my prediction.** Although the shaded area seems very similar to [the one obtained by with R](https://app-learninglab.inria.fr/moocrr/gitlab/moocrr-session3/moocrr-reproducibility-study/tree/master/challenger.pdf), I can spot a few differences (e.g., the blue point for temperature 63 is outside)... Could this be a numerical error ? Or a difference in the statistical method ? It is not clear which one is \"right\"."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Hide code",
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
