Duncan post hoc test in Python


import numpy as np
import pandas as pd
from scipy.stats import f_oneway
from statsmodels.stats.multicomp import pairwise_tukeyhsd

# enter data for three groups
a = [85, 86, 88, 75, 78, 94, 98, 79, 71, 80]
b = [91, 92, 93, 90, 97, 94, 82, 88, 95, 96]
c = [79, 78, 88, 94, 92, 85, 83, 85, 82, 81]

# perform one-way ANOVA
f_oneway(a, b, c)


# create DataFrame to hold data
df = pd.DataFrame(
    {
        "score": [
            85,
            86,
            88,
            75,
            78,
            94,
            98,
            79,
            71,
            80,
            91,
            92,
            93,
            90,
            97,
            94,
            82,
            88,
            95,
            96,
            79,
            78,
            88,
            94,
            92,
            85,
            83,
            85,
            82,
            81,
        ],
        "group": np.repeat(["a", "b", "c"], repeats=10),
    }
)
# perform Tukey's test
tukey = pairwise_tukeyhsd(endog=df["score"], groups=df["group"], alpha=0.05)

# df
print(df)
# display results
print(tukey)
# Here’s how to interpret the output:

# P-value for the difference in means between a and b: .0158
# P-value for the difference in means between a and c: .8864
# P-value for the difference in means between b and c: .0453
# Thus, we would conclude that there is a statistically significant difference
# between the means of groups a and b and groups b and c,
# but not a statistically significant difference between the means of groups a and c.