{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# Lab 6: Comparing Three or More Groups with ANOVA\n", "## Created by Jane and Kristin, modified by Xinyue" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "Often, we have to compare data from three or more groups to see if one or more of them is different from the others. To do this, scientists use a statistic called the _F_- statistic, defined as $F=\\frac{Var_{between}}{Var_{within}}$. The variability between groups is the sum of the squared differences between the group means and the grand mean. The variability within groups is the sum of the group variances. In LS 40, we will use a variation of the F-statistic that does not require squaring, which is called the _F_-like statistic." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "1. Import pandas, Numpy, Seaborn and Pyplot." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "In this lab, we will examine the results of a pharmaceutical company's study comparing the effectiveness of different pain relief medications on migraine headaches. For the experiment, 27 volunteers were selected and 9 were randomly assigned to one of three\n", "drug formulations. The subjects were instructed to take the drug during their next migraine headache episode and to report their pain on a scale of 1 = no pain to 10 = extreme pain 30 minutes after taking the drug." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "2. Using the pandas `read_csv` function, import the file `migraines.csv` and show the data." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | Drug A | \n", "Drug B | \n", "Drug C | \n", "
---|---|---|---|
0 | \n", "4 | \n", "7 | \n", "6 | \n", "
1 | \n", "5 | \n", "8 | \n", "7 | \n", "
2 | \n", "4 | \n", "4 | \n", "6 | \n", "
3 | \n", "2 | \n", "5 | \n", "6 | \n", "
4 | \n", "2 | \n", "4 | \n", "7 | \n", "
5 | \n", "4 | \n", "6 | \n", "5 | \n", "
6 | \n", "3 | \n", "5 | \n", "6 | \n", "
7 | \n", "3 | \n", "8 | \n", "5 | \n", "
8 | \n", "3 | \n", "7 | \n", "5 | \n", "
\n", " F-like=\\[\\frac{n_{a}|\\widetilde{a}-\\widetilde{G}|+n_{b}|\\widetilde{b}-\\widetilde{G}|+n_{c}|\\widetilde{c}-\\widetilde{G}|}{\\Sigma|a_{i}-\\widetilde{a}|+\\Sigma|b_{i}-\\widetilde{b}|+\\Sigma|c_{i}-\\widetilde{c}|}\\]\n", "
\n", "\n", " \n", "Each quantity you compute in this section should be assigned to a variable." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "4. Find the median of each column. HINT: pandas data frames have a built-in function for columnwise medians, so you can just use `df.median()`, where `df` is the name of your data frame." ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Drug A 3.0\n", "Drug B 6.0\n", "Drug C 6.0\n", "dtype: float64" ] }, "execution_count": 92, "metadata": { }, "output_type": "execute_result" } ], "source": [ "meds = migraines.median()\n", "meds" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "5. Find the grand median (the median of the whole sample). HINT: Use `np.median`." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "grand = np.median(migraines)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "6. Find the numerator of the _F_-like statistic (variation among groups). HINT: When working with data frames and NumPy arrays, you can do computations like addition and multiplication directly, without for loops (unlike in regular Python). Also, Numpy has `abs` and `sum` functions." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "36.0" ] }, "execution_count": 27, "metadata": { }, "output_type": "execute_result" } ], "source": [ "num = np.sum(len(migraines) * abs(meds - grand)) \n", "num" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "7. Compute the denominator of the _F_-like statistic. This represents variation within groups. HINT: Numpy and pandas will handle columns automatically." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "24.0" ] }, "execution_count": 29, "metadata": { }, "output_type": "execute_result" } ], "source": [ "den = np.sum(np.sum(np.abs(migraines - meds)))\n", "den" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "8. Compute _F_-like, which is the ratio $\\frac{\\text{variation among groups}}{\\text{variation within groups}}$." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 30, "metadata": { }, "output_type": "execute_result" } ], "source": [ "num/den" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Bootstrapping" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "We now want to find a p-value for our data by simulating the null hypothesis. This, of course, means computing the _F_-like statistic each time, which takes a lot of code and would make a mess in the bootstrap loop. Instead, we will package our code into a function and call this function whereever necessary." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "9. Write a function that will compute the _F_-like statistic for this dataset or one of the same size." ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def Flike(df):\n", " meds = df.median()\n", " grand = np.median(df)\n", " num = np.sum(len(df) * abs(meds - grand))\n", " den = np.sum(np.sum(np.abs(df - meds)))\n", " result = num/den\n", " return(result)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "We now want to simulate the null hypothesis that there is no difference between the groups. To do this, we have to make all the data into one dataset, sample pseudo-groups from it, and compute the _F_-like statistic for the resampled data." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "10. Use the code `alldata = np.concatenate([migraine[\"Drug A\"], migraine[\"Drug B\"], migraine[\"Drug C\"]])` (this assumes your data frame is called \"migraine\") to put all the data into one 1-D array. " ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "alldata = np.concatenate([migraines[\"Drug A\"], migraines[\"Drug B\"], migraines[\"Drug C\"]])" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "11. Sample three groups of the appropriate size from `alldata`. Assign each to a variable." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "A = np.random.choice(alldata, len(\"Drug A\"))\n", "B = np.random.choice(alldata, len(\"Drug B\"))\n", "C = np.random.choice(alldata, len(\"Drug C\"))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "12. Make the three samples into a data frame. To do this, use the NumPy function `column_stack` to put the 1-D arrays side by side and then use the pandas function `DataFrame` to convert the result into a data frame." ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " | 0 | \n", "1 | \n", "2 | \n", "
---|---|---|---|
0 | \n", "3 | \n", "5 | \n", "5 | \n", "
1 | \n", "8 | \n", "5 | \n", "6 | \n", "
2 | \n", "5 | \n", "3 | \n", "4 | \n", "
3 | \n", "7 | \n", "5 | \n", "2 | \n", "
4 | \n", "3 | \n", "4 | \n", "6 | \n", "
5 | \n", "4 | \n", "3 | \n", "5 | \n", "