{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from statsmodels.formula.api import ols\n",
    "import dautil as dl\n",
    "from statsmodels.stats.anova import anova_lm\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lr = dl.nb.LatexRenderer(chapter=3, start=23)\n",
    "lr.render(r'\\mu_{ij} = \\mu + \\alpha_i + \\beta_j + \\gamma_{ij}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = dl.data.Weather.load().dropna()\n",
    "df['RAIN'] = df['RAIN'] > 0\n",
    "formula = 'WIND_SPEED ~ C(RAIN) + C(WIND_DIR)'\n",
    "lm = ols(formula, df).fit()\n",
    "hb = dl.HTMLBuilder()\n",
    "hb.h1('ANOVA Applied to Weather Data')\n",
    "hb.h2('ANOVA results')\n",
    "hb.add_df(anova_lm(lm), index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('anova')\n",
    "dl.nb.RcWidget(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df['WIND_DIR'] = dl.data.Weather.categorize_wind_dir(df)\n",
    "hb.h2('Truncated Contingency table')\n",
    "hb.add_df(df.groupby([df['RAIN'], df['WIND_DIR']]).count().head(3),index=True)\n",
    "\n",
    "sns.pointplot(y='WIND_SPEED', x='WIND_DIR',\n",
    "              hue='RAIN', data=df[['WIND_SPEED', 'RAIN', 'WIND_DIR']])\n",
    "HTML(hb.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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
}
