Example and Visualization of Tukey Test

A Tukey Test is a way to compare more than 2 datasets and see which are statistically significant. I will demo this using the Auto MPG Dataset from UCI

import pandas as pd 

# load in dataset 
df = pd.read_csv('auto-mpg.csv')

# update orgin column 
df.loc[df['origin'] == 1, 'origin'] = 'US'
df.loc[df['origin'] == 2, 'origin'] = 'Germany'
df.loc[df['origin'] == 3, 'origin'] = 'Japan'

df.head()
mpgcylindersdisplacementhorsepowerweightaccelerationmodel yearorigincar name
018.08307.0130350412.070USchevrolet chevelle malibu
115.08350.0165369311.570USbuick skylark 320
218.08318.0150343611.070USplymouth satellite
316.08304.0150343312.070USamc rebel sst
417.08302.0140344910.570USford torino

I want to see if the number of cylinders is statistically different depending on the origin of the car.

from statsmodels.stats.multicomp import MultiComparison

cardata = MultiComparison(df['cylinders'], df['origin'])
results = cardata.tukeyhsd()
results.summary()
Multiple Comparison of Means - Tukey HSD, FWER=0.05
group1group2meandiffp-adjlowerupperreject
GermanyJapan-0.05590.9-0.58050.4688False
GermanyUS2.09190.0011.65952.5242True
JapanUS2.14770.0011.7352.5605True

We see that the number of cylinders in cars that originated in Germany and Japan are not statistically significant, we also see that cars that originated in the US have a statistcally different number of cylinders than cars that originated in Japan or Germany.

Now lets visualize this.

results.plot_simultaneous();

png

The X-Axis is the number of cylinders and we see why the US had a statistically significant result, due to having a much higher mean number of cylinders.

We can also highlight one of the groups using comparison_name. I’m going to highlight Japan.

results.plot_simultaneous(comparison_name = 'Japan');

png

This shows that Germany intersects the confidence interval of Germany and this is why they were not statistically different.