Learning Outcomes
By the end of this section, you should be able to:
- 4.4.1 Set up and apply one-way analysis of variance hypothesis test.
- 4.4.2 Use Python to conduct one-way analysis of variance.
In Testing Claims Based on One Sample, we reviewed methods to conduct hypothesis testing for two means; however, there are scenarios where a researcher might want to compare three or more means to determine if any of the means are different from one another. For example, a medical researcher might want to compare three different pain relief medications to compare the average time to provide relief from a migraine headache. Analysis of variance (ANOVA) is a statistical method that allows a researcher to compare three or more means and determine if the means are all statistically the same or if at least one mean is different from the others. The one-way ANOVA focuses on one independent variable. For two independent variables, a method called “two-way ANOVA” is applicable, but this method is beyond the scope of this text. We explore the one-way ANOVA next.
One-Way ANOVA
To compare three or more means, the sample sizes for each group can be different sample sizes (samples sizes do not need to be identical). For one-way ANOVA hypothesis testing, we will follow the same general outline of steps for hypothesis testing that was discussed in Hypothesis Testing.
The first step will be to write the null and alternative hypotheses. For one-way ANOVA, the null and alternative hypotheses are always stated as the following:
: All population means are equal:
: At least one population mean is different from the others.
Here are the requirements to use this procedure:
- The samples are random and selected from approximately normal distributions.
- The samples are independent of one another.
- The population variances are approximately equal.
In this discussion, we assume that the population variances are not equal.
When these requirements are met, the F-distribution is used as the basis for conducting the hypothesis test.
The F-distribution is a skewed distribution (skewed to the right), and the shape of the distribution depends on two different degrees of freedom, referred to as degrees of freedom for the numerator and degrees of freedom for the denominator.
Figure 4.10 shows the typical shape of the F-distribution:
The test statistic for this hypothesis test will be the ratio of two variances, namely the ratio of the variance between samples to the ratio of the variances within the samples.
The numerator of the test statistic (variance between samples) is sometimes referred to as variation due to treatment or explained variation.
The denominator of the test statistic (variance within samples) is sometimes referred to as variation due to error, or unexplained variation.
The details for a manual calculation of the test statistic are provided next; however, it is very common to use software for ANOVA analysis.
For the purposes of this discussion, we will assume we are comparing three means, but keep in mind the ANOVA method can be applied to three or more means.
- Find the mean for each of the three samples. Label these means as . Find the variance for each of the three samples. Label these variances as .
- Find the grand mean (label this as ). This is the sum of all the data values from the three samples, divided by the overall sample size.
- Calculate the quantity called “sum of squares between (SSB)” according to the following formula:
- Calculate the quantity called “sum of squares within (SSW)” according to the following formula:
- Calculate the degrees of freedom for the numerator () and degrees of freedom for the denominator ().
For degrees of freedom for the numerator, this is calculated as the number of groups minus 1. For example, if a medical researcher is comparing three different pain relief medications to compare the average time to provide relief from a migraine headache, there are three groups, and thus the degrees of freedom for the numerator would be .
For degrees of freedom for the denominator, this is calculated as the total of the sample sizes minus the number of groups. - Calculate the quantity called “mean square between (MSB)” according to the following formula:
- Calculate the quantity called “mean square within (MSW)” according to the following formula:
- Calculate the test statistic (), according to the following formula:
Once the test statistic is obtained, the p-value is calculated as the area to the right of the test statistic under the -distribution curve (note that the ANOVA hypothesis test is always considered a “right-tail” test).
Usually, the results of these computations are organized in an ANOVA summary table like Table 4.17.
Variation | Sum of Squares | Degrees of Freedom | Mean Squares | Test Statistic | p-value |
---|---|---|---|---|---|
Between | SSB | MSB | Area to the right of the F test statistic | ||
Within | SSW | MSW | N/A |
Using Python for One-Way ANOVA
As mentioned earlier, due to the complexity of these calculations, technology is typically used to calculate the test statistic and p-value.
Using Python, the f_oneway()
function is provided as part of the scipy
library, and this function provides both the test statistic and p-value for a one-way ANOVA hypothesis test.
The syntax is to call the function with the data arrays as arguments, as in:
f_oneway(Array1, Array2, Array3, …)
The function returns both the F test statistic and the p-value for the one-way ANOVA hypothesis test.
The details of this Python function are shown in Example 4.25.
Here is a step-by-step example to illustrate this ANOVA hypothesis testing process.
Example 4.25
Problem
An airline manager claims that the average arrival delays for flights from Boston to Dallas are the same for three airlines: Airlines A, B, and C. The following arrival delay data (in minutes) is collected on samples of flights for the three airlines (Table 4.18):
Airline A | Airline B | Airline C |
---|---|---|
8 | 13 | 11 |
12 | 0 | 15 |
7 | 2 | 9 |
9 | 7 | 11 |
11 | 6 | 12 |
4 | 5 | N/A |
Use this sample data to test the claim that the average arrival delays are the same for three airlines: Airlines A, B, and C. Use a level of significance of 0.05 for this analysis. Assume the samples are independent and taken from approximately normal distributions and each population has approximately the same variance.
Solution
The first step in the hypothesis testing procedure is to write the null and alternative hypothesis based on the claim.
For an ANOVA hypothesis test, the setup of the null and alternative hypotheses is shown here. Notice that the claim mentions “the average arrival delays are the same for three airlines,” which corresponds to the null hypothesis.
: All three population means are equal: [Claim]
: At least one population mean is different from the others.
The following Python program generates the corresponding F test statistic and p-value for this ANOVA analysis:
Python Code
# import libraries
import numpy as np
from scipy.stats import f_oneway
# enter airline arrival data
AirlineA = np.array([8, 12, 7, 9, 11, 4])
AirlineB = np.array([13, 0, 2, 7, 6, 5])
AirlineC = np.array([11, 15, 9, 11, 12])
#f_oneway function returns both the test statistic and p-value
f_oneway(AirlineA, AirlineB, AirlineC)
The resulting output will look like this:
F_onewayResult(statistic=4.388264306955828, pvalue=0.03314999857295516)
From this Python output, the test statistic is and the p-value is 0.033.
The final step in the hypothesis testing procedure is to come to a final decision regarding the null hypothesis. This is accomplished by comparing the p-value with the level of significance. In this example, the p-value is 0.033 and the level of significance is 0.05: since the p-value level of significance, the decision is to “reject the null hypothesis.”
Conclusion: The decision is to reject the null hypothesis. The claim corresponds to the null hypothesis. This scenario corresponds to the first row in Table 4.18 and thus the conclusion is: There is enough evidence to reject the claim that the average arrival delays are the same for three airlines. This implies that at least one of the means is different from the others.
Note: The ANOVA test method does not specify which mean is different from the others. Further statistical analysis is needed to make this determination.