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=VarwithinVarbetween. 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.
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
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.
Using the pandas read_csv function, import the file migraines.csv and show the data.
In [ ]:
Visualize the data. Use as many different plots as you need to get a sense of the distributions. What are your initial impressions?
<seaborn.axisgrid.FacetGrid at 0x7fe75c14aca0>
Computing the F-like statistic
In the next several exercises, you will compute the F-like statistic for your data.
Each quantity you compute in this section should be assigned to a variable.
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.
Drug A 3.0
Drug B 6.0
Drug C 6.0
Find the grand median (the median of the whole sample). HINT: Use np.median.
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.
Compute the denominator of the F-like statistic. This represents variation within groups. HINT: Numpy and pandas will handle columns automatically.
Compute F-like, which is the ratio variation within groupsvariation among groups.
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.
Write a function that will compute the F-like statistic for this dataset or one of the same size.
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.
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.
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.
Use the list of p-values from your two-group comparisons and the Benjamini-Hochberg method below to determine which two-group comparisons have a significant difference at α=0.05. See more details in "Controlling the False Positive Rate with Multiple Comparisons Corrections" PDF on Week 6 on CCLE site.
import statsmodels.stats.multitest as smm smm.multipletests(pvals, alpha=my_alpha, method='fdr_bh')
File "<ipython-input-95-d55528c377ca>", line 1 np.sum(store => 3) ^SyntaxError: invalid syntax