{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Two-way ANOVA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ANOVA with interaction: \n", "\n", "*Measurement of fetal head circumference, by four observers in three fetuses, from a study investigating the\n", "reproducibility of ultrasonic fetal head circumference data.*\n", "\n", "Author: Thomas Haslwanter, Date: June-2014" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab inline\n", "import pandas as pd\n", "import urllib\n", "from statsmodels.formula.api import ols\n", "from statsmodels.stats.anova import anova_lm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get and format the data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "inFile = 'altman_12_6.txt'\n", "url_base = 'https://raw.githubusercontent.com/thomas-haslwanter/statsintro_python/master/ipynb/Data/data_altman/'\n", "url = url_base + inFile\n", "data = genfromtxt(urllib.request.urlopen(url), delimiter=',')\n", "\n", "# Bring them in dataframe-format\n", "df = pd.DataFrame(data, columns=['hs', 'fetus', 'observer'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ANOVA with interaction" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " df sum_sq mean_sq F PR(>F)\n", "C(fetus) 2 324.008889 162.004444 2113.101449 1.051039e-27\n", "C(observer) 3 1.198611 0.399537 5.211353 6.497055e-03\n", "C(fetus):C(observer) 6 0.562222 0.093704 1.222222 3.295509e-01\n", "Residual 24 1.840000 0.076667 NaN NaN\n" ] } ], "source": [ "formula = 'hs ~ C(fetus) + C(observer) + C(fetus):C(observer)'\n", "lm = ols(formula, df).fit()\n", "print(anova_lm(lm))" ] } ], "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 }