{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import statsmodels.api as sm\n",
    "from statsmodels.formula.api import ols\n",
    "from dautil import data\n",
    "from dautil import nb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lr = nb.LatexRenderer(chapter=2)\n",
    "lr.render(r'var(\\hat{\\epsilon}_i)=\\hat{\\sigma}^2_i(1-h_{ii})')\n",
    "lr.render(r'\\hat{\\sigma}^2_i=\\frac{1}{n - p - 1 \\;\\;}\\sum_{j}^{n}\\;\\;\\;\\forall \\;\\;\\; j \\neq i')\n",
    "lr.render(r'H=X(X^{\\;\\prime}X)^{-1}X^{\\;\\prime}')\n",
    "lr.render(r'CookD = \\frac{\\sum_{j=1}^n (\\hat Y_j - \\hat Y_{j(i)})^2}{p\\ \\text{MSE}}')\n",
    "lr.render(r'DFFITS = \\frac{\\hat{Y_i}-\\hat{Y_{i(i)}}}{s_{(i)}\\sqrt{h_{ii}}}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dawb = data.Worldbank()\n",
    "countries = dawb.get_countries()[['name', 'iso2c']]\n",
    "population = dawb.download(indicator=[dawb.get_name('pop_grow'), dawb.get_name('gdp_pcap'),\n",
    "                                    dawb.get_name('primary_education')],\n",
    "                         country=countries['iso2c'], start=2014, end=2014)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "population = dawb.rename_columns(population)\n",
    "population_model = ols(\"pop_grow ~ gdp_pcap + primary_education\",\n",
    "                       data=population).fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "fig, ax = plt.subplots(figsize=(19.2, 14.4))\n",
    "fig = sm.graphics.influence_plot(population_model, ax=ax, criterion=\"cooks\")\n",
    "plt.grid()"
   ]
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
