# How to perform pairwise comparisons (in Python, using statsmodels)

## Task

When analyzing data from a completely randomized single-factor design, suppose that you have performed an ANOVA and noticed that there’s a significant difference between at least one pair of treatment levels. How can pairwise comparisons help us explore which pairs of treatment levels are different?

Related tasks:

- How to do a one-way analysis of variance (ANOVA)
- How to perform post-hoc analysis with Tukey’s HSD test

## Solution

The solution below uses an example dataset that details the counts of insects in an agricultural experiment with six types of insecticides, labeled A through F. (See how to quickly load some sample data.)

1
2
3

from rdatasets import data
df = data('InsectSprays')
df

count | spray | |
---|---|---|

0 | 10 | A |

1 | 7 | A |

2 | 20 | A |

3 | 14 | A |

4 | 14 | A |

... | ... | ... |

67 | 10 | F |

68 | 26 | F |

69 | 26 | F |

70 | 24 | F |

71 | 13 | F |

72 rows × 2 columns

Before we perform any post hoc analysis, we need to see if the count of insects depends on the type of insecticide given by conducting a one way ANOVA. (See also how to do a one-way analysis of variance (ANOVA).)

1
2
3
4

from statsmodels.formula.api import ols
model = ols('count ~ spray', data = df).fit()
import statsmodels.api as sm
sm.stats.anova_lm(model, typ=1)

df | sum_sq | mean_sq | F | PR(>F) | |
---|---|---|---|---|---|

spray | 5.0 | 2668.833333 | 533.766667 | 34.702282 | 3.182584e-17 |

Residual | 66.0 | 1015.166667 | 15.381313 | NaN | NaN |

At the 5% significance level, we see that the count differs according to the type of insecticide used. We assume that the model assumptions are met, but do not verify that here.

If we would like to compare the pairs without any corrections, we can use the ‘pairwise t test’ in the `scikit_posthocs`

package.

1
2

import scikit_posthocs as sp
sp.posthoc_ttest(df, val_col='count', group_col='spray', p_adjust=None, pool_sd=True )

A | B | C | D | E | F | |
---|---|---|---|---|---|---|

A | 1.000000e+00 | 6.044761e-01 | 7.266893e-11 | 9.816910e-08 | 2.753922e-09 | 1.805998e-01 |

B | 6.044761e-01 | 1.000000e+00 | 8.509776e-12 | 1.212803e-08 | 3.257986e-10 | 4.079858e-01 |

C | 7.266893e-11 | 8.509776e-12 | 1.000000e+00 | 8.141205e-02 | 3.794750e-01 | 2.794343e-13 |

D | 9.816910e-08 | 1.212803e-08 | 8.141205e-02 | 1.000000e+00 | 3.794750e-01 | 4.035610e-10 |

E | 2.753922e-09 | 3.257986e-10 | 3.794750e-01 | 3.794750e-01 | 1.000000e+00 | 1.054387e-11 |

F | 1.805998e-01 | 4.079858e-01 | 2.794343e-13 | 4.035610e-10 | 1.054387e-11 | 1.000000e+00 |

Techniques to adjust the above table for multiple comparisons include the Bonferroni correction, Fisher’s Least Significant Difference (LSD) method, Dunnett’s procedure, and Scheffe’s method. These can be used in place of ‘None’ for the `p.adjust`

argument; see details here.

You can also determine the magnitude of these differences; see how to perform post-hoc analysis with Tukey’s HSD test.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Krtin Juneja (KJUNEJA@falcon.bentley.edu)