ANALYSIS TOOL BOX

Hypothesis Testing

OneWayANOVA

Perform a one-way analysis of variance (ANOVA) and post-hoc Tukey tests.

hypothesis-testingstatisticssignificanceinference

Introduction

With only two groups, a t-test settles the question of whether their means differ. With three or more — regions, dosages, product tiers — running pairwise t-tests inflates your false-positive rate, which is exactly the problem ANOVA solves. OneWayANOVA tests whether at least one group mean differs from the rest, and then automatically runs Tukey's HSD post-hoc test so you know which specific pairs of groups are actually different, not just that some difference exists somewhere.

Teaching Note

One-way ANOVA is essential for:

  • Comparing sales performance across different retail regions
  • Testing the efficacy of multiple drug treatments relative to a control
  • Analyzing variations in machinery output under different operating temperatures
  • Evaluating differences in customer satisfaction scores across product tiers
  • Assessing the impact of various educational methods on student scores
  • Comparing investment returns across different asset classes
  • Validating whether different marketing campaigns result in distinct conversion rates

Parameters

ParameterTypeDefaultDescription
dataframerequiredThe pandas DataFrame containing the experimental data.
outcome_columnrequiredName of the column containing the continuous outcome variable (dependent variable).
grouping_columnrequiredName of the column containing the categorical grouping labels (independent variable).
signficance_level0.05The alpha level used to determine statistical significance. Defaults to 0.05.
homogeneity_of_varianceTrueIf True, assumes equality of variance between groups. If False, adjusts calculations for unequal variances. Defaults to True.
use_welch_correctionFalseIf True, applies Welch's correction to the ANOVA calculation, which is recommended when group variances or sample sizes are unequal. Defaults to False.
plot_sample_distributionsTrueIf True, displays a kernel density estimate of simulated sample means for each group to visualize differences. Defaults to True.
color_palette'Set2'Seaborn color palette name used for the lines and fills in the plot. Defaults to 'Set2'.
title_for_plot'One-Way ANOVA'Main title text to display at the top of the plot. Defaults to 'One-Way ANOVA'.
subtitle_for_plot'Shows the distribution of the sample means for each group.'Subtitle text to display below the main title.
caption_for_plotNoneCaption text displayed at the bottom of the plot. Defaults to None.
data_source_for_plotNoneOptional text identifying the data source, displayed in the caption area. Defaults to None.
show_y_axisFalseIf True, displays the y-axis (density scale) on the distribution plot. Defaults to False.
title_y_indent1.1Vertical position for the main title relative to the axes. Defaults to 1.1.
subtitle_y_indent1.05Vertical position for the subtitle relative to the axes. Defaults to 1.05.
caption_y_indent-0.15Vertical position for the caption relative to the axes. Defaults to -0.15.
figure_size(8, 6)Tuple specifying the (width, height) of the figure in inches. Defaults to (8, 6).

Returns

A statsmodels Summary object containing the results of Tukey's Honestly Significant Difference (HSD) post-hoc test, including pairwise group differences, confidence intervals, and rejection decisions.

Example

python
from analysistoolbox.hypothesis_testing import OneWayANOVA
import pandas as pd
import numpy as np

# Compare average sales across three different regions
sales_df = pd.DataFrame({
    'sales': np.random.normal(500, 50, 60),
    'region': ['East', 'West', 'South'] * 20
})
tukey_results = OneWayANOVA(
    dataframe=sales_df,
    outcome_column='sales',
    grouping_column='region'
)
print(tukey_results)