{ "metadata": { "name": "multi_way_anova" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Multi-way ANOVA\n", "===============\n", "\n", "Some notes on who to run a multi-way ANOVA in Python.\n", "Primary sources include [Ben Bolker's book](http://www.math.mcmaster.ca/~bolker/emdbook/chap9A.pdf)\n", "and the [Statsmodels docs](http://statsmodels.sourceforge.net/devel/examples/generated/example_interactions.html#two-way-anova)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from urllib2 import urlopen\n", "\n", "import numpy as np\n", "\n", "import statsmodels.api as sm\n", "\n", "import pandas\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "from statsmodels.formula.api import ols\n", "\n", "from statsmodels.graphics.api import interaction_plot, abline_plot\n", "\n", "from statsmodels.stats.anova import anova_lm\n", "\n", "url = 'http://stats191.stanford.edu/data/kidney.table'\n", "kidney_table = pandas.read_table(url, delimiter=\" *\")\n", "\n", "Days = kidney_table['Days']\n", "Duration = kidney_table" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "kidney_lm = ols('np.log(Days+1) ~ C(Duration) * C(Weight)', kidney_table).fit()\n", "anova_lm(kidney_lm)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " | df | \n", "sum_sq | \n", "mean_sq | \n", "F | \n", "PR(>F) | \n", "
---|---|---|---|---|---|
C(Duration) | \n", "1 | \n", "2.339693 | \n", "2.339693 | \n", "4.358293 | \n", "0.041562 | \n", "
C(Weight) | \n", "2 | \n", "16.971291 | \n", "8.485645 | \n", "15.806745 | \n", "0.000004 | \n", "
C(Duration):C(Weight) | \n", "2 | \n", "0.635658 | \n", "0.317829 | \n", "0.592040 | \n", "0.556748 | \n", "
Residual | \n", "54 | \n", "28.989198 | \n", "0.536837 | \n", "NaN | \n", "NaN | \n", "