-
-
Save garthj88/5988860 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mathieu Blondel, February 2012 | |
# Port to Python of examples in chapter 5 of | |
# "Introductory Statistics with R" by Peter Dalgaard | |
import numpy as np | |
from scipy.stats import ttest_1samp, wilcoxon, ttest_ind, mannwhitneyu | |
# daily intake of energy in kJ for 11 women | |
daily_intake = np.array([5260,5470,5640,6180,6390,6515, | |
6805,7515,7515,8230,8770]) | |
# one sample t-test | |
# null hypothesis: expected value = 7725 | |
t_statistic, p_value = ttest_1samp(daily_intake, 7725) | |
# p_value < 0.05 => alternative hypothesis: | |
# data deviate significantly from the hypothesis that the mean | |
# is 7725 at the 5% level of significance | |
print "one-sample t-test", p_value | |
# one sample wilcoxon-test | |
z_statistic, p_value = wilcoxon(daily_intake - 7725) | |
print "one-sample wilcoxon-test", p_value | |
energ = np.array([ | |
# energy expenditure in mJ and stature (0=obese, 1=lean) | |
[9.21, 0], | |
[7.53, 1], | |
[7.48, 1], | |
[8.08, 1], | |
[8.09, 1], | |
[10.15, 1], | |
[8.40, 1], | |
[10.88, 1], | |
[6.13, 1], | |
[7.90, 1], | |
[11.51, 0], | |
[12.79, 0], | |
[7.05, 1], | |
[11.85, 0], | |
[9.97, 0], | |
[7.48, 1], | |
[8.79, 0], | |
[9.69, 0], | |
[9.68, 0], | |
[7.58, 1], | |
[9.19, 0], | |
[8.11, 1]]) | |
# similar to expend ~ stature in R | |
group1 = energ[:, 1] == 0 | |
group1 = energ[group1][:, 0] | |
group2 = energ[:, 1] == 1 | |
group2 = energ[group2][:, 0] | |
# two-sample t-test | |
# null hypothesis: the two groups have the same mean | |
# this test assumes the two groups have the same variance... | |
# (can be checked with tests for equal variance) | |
# independent groups: e.g., how boys and girls fare at an exam | |
# dependent groups: e.g., how the same class fare at 2 different exams | |
t_statistic, p_value = ttest_ind(group1, group2) | |
# p_value < 0.05 => alternative hypothesis: | |
# they don't have the same mean at the 5% significance level | |
print "two-sample t-test", p_value | |
# two-sample wilcoxon test | |
# a.k.a Mann Whitney U | |
u, p_value = mannwhitneyu(group1, group2) | |
print "two-sample wilcoxon-test", p_value | |
# pre and post-menstrual energy intake | |
intake = np.array([ | |
[5260, 3910], | |
[5470, 4220], | |
[5640, 3885], | |
[6180, 5160], | |
[6390, 5645], | |
[6515, 4680], | |
[6805, 5265], | |
[7515, 5975], | |
[7515, 6790], | |
[8230, 6900], | |
[8770, 7335], | |
]) | |
pre = intake[:, 0] | |
post = intake[:, 1] | |
# paired t-test: doing two measurments on the same experimental unit | |
# e.g., before and after a treatment | |
t_statistic, p_value = ttest_1samp(post - pre, 0) | |
# p < 0.05 => alternative hypothesis: | |
# the difference in mean is not equal to 0 | |
print "paired t-test", p_value | |
# alternative to paired t-test when data has an ordinary scale or when not | |
# normally distributed | |
z_statistic, p_value = wilcoxon(post - pre) | |
print "paired wilcoxon-test", p_value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment