I have tried looking through multiple statistics modules for Python but can't seem to find any that support one-way ANOVA
post hoc tests.
Asked
Active
Viewed 2.6k times
23

Dataman
- 3,457
- 3
- 19
- 31

david_adler
- 9,690
- 6
- 57
- 97
-
`TukeyHSD` is built into R. Does that do what you're looking for? – David Robinson Apr 17 '13 at 00:46
-
statsmodels has a `tukeyhsd` function in the sandbox, but I've not tried it. – Thomas K Apr 17 '13 at 17:06
-
I am looking for the same function, I guess I will use RPy, an interface to R which seems to work like a charm! – PierreE May 29 '13 at 15:58
-
See [scikit-posthocs](https://github.com/maximtrp/scikit-posthocs). – Maksim Terpilowski Aug 10 '18 at 12:28
2 Answers
45
one way ANOVA can be used like
from scipy import stats
f_value, p_value = stats.f_oneway(data1, data2, data3, data4, ...)
This is one way ANOVA and it returns F value and P value.
There is significant difference If the P value is below your setting.
The Tukey-kramer HSD test can be used like
from statsmodels.stats.multicomp import pairwise_tukeyhsd
print pairwise_tukeyhsd(Data, Group)
This is multicomparison. The output is like
Multiple Comparison of Means - Tukey HSD,FWER=0.05
================================================
group1 group2 meandiff lower upper reject
------------------------------------------------
0 1 -35.2153 -114.8741 44.4434 False
0 2 46.697 -40.4993 133.8932 False
0 3 -7.5709 -87.49 72.3482 False
1 2 81.9123 5.0289 158.7956 True
1 3 27.6444 -40.8751 96.164 False
2 3 -54.2679 -131.4209 22.8852 False
------------------------------------------------
Please refer to this site how to set the arguments.
The tukeyhsd of statsmodels doesn't return P value.
So, if you want to know P value, calculate from these outputted value or use R.

ami_GS
- 852
- 15
- 21
-
3After saving the output into a variable `res`, you can get p-values by applying `psturng(np.abs(res.meandiffs / res.std_pairs), len(res.groupsunique), res.df_total)`, where `psturng` comes from `from statsmodels.stats.libqsturng import psturng` – Javier de la Rosa Nov 07 '16 at 04:56
-
2I have added p-values to the table and output attributes using a method similar to that suggested by @JavierdelaRosa. Should be added soon -- see the [pull request](https://github.com/statsmodels/statsmodels/pull/5418). – SpinUp __ A Davis Dec 13 '18 at 04:13
-
@ami_GS how do you get it to print the results? When I do it, it just prints"tukeyHSDResults object at 0x0DBEBB10....sorry for basic question... – joe5 Jan 10 '19 at 22:03