{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Two-way ANOVA\n", "\n", "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: Feb-2017" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import numpy as np\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": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Get and format the data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "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 = np.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": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## ANOVA with interaction" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " df sum_sq mean_sq F PR(>F)\n", "C(fetus) 2.0 324.008889 162.004444 2113.101449 1.051039e-27\n", "C(observer) 3.0 1.198611 0.399537 5.211353 6.497055e-03\n", "C(fetus):C(observer) 6.0 0.562222 0.093704 1.222222 3.295509e-01\n", "Residual 24.0 1.840000 0.076667 NaN NaN\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Programs\\WinPython-64bit-3.6.0.1Qt5\\python-3.6.0.amd64\\lib\\site-packages\\scipy\\stats\\_distn_infrastructure.py:875: RuntimeWarning: invalid value encountered in greater\n", " return (self.a < x) & (x < self.b)\n", "C:\\Programs\\WinPython-64bit-3.6.0.1Qt5\\python-3.6.0.amd64\\lib\\site-packages\\scipy\\stats\\_distn_infrastructure.py:875: RuntimeWarning: invalid value encountered in less\n", " return (self.a < x) & (x < self.b)\n", "C:\\Programs\\WinPython-64bit-3.6.0.1Qt5\\python-3.6.0.amd64\\lib\\site-packages\\scipy\\stats\\_distn_infrastructure.py:1814: RuntimeWarning: invalid value encountered in less_equal\n", " cond2 = cond0 & (x <= self.a)\n" ] } ], "source": [ "formula = 'hs ~ C(fetus) + C(observer) + C(fetus):C(observer)'\n", "lm = ols(formula, df).fit()\n", "print(anova_lm(lm))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "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.0" } }, "nbformat": 4, "nbformat_minor": 0 }