ONEWAY_ANOVA
Overview
The ONEWAY_ANOVA
function performs a one-way analysis of variance (ANOVA) to determine whether there are statistically significant differences between the means of up to three independent groups. This test is commonly used in statistics to compare the means of different samples and is based on the F-distribution. The function uses the scipy.stats
package for computation. For more information, see the scipy.stats.f_oneway documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=ONEWAY_ANOVA(group_one, group_two, [group_three])
group_one
(2D list of float, required): First group of sample data.group_two
(2D list of float, required): Second group of sample data.group_three
(2D list of float, optional): Third group of sample data.
The function returns a 2D list with one row: [F-statistic, p-value]
, both as floats. If an error occurs, a 2D list of strings describing the error is returned.
Examples
Example 1: Compare Three Groups
Suppose you have three groups of data:
Group 1 | Group 2 | Group 3 |
---|---|---|
23 | 30 | 25 |
20 | 29 | 27 |
22 | 31 | 26 |
21 | 32 | 24 |
24 | 28 | 23 |
In Excel:
=ONEWAY_ANOVA({23;20;22;21;24}, {30;29;31;32;28}, {25;27;26;24;23})
Expected output:
F-statistic | p-value |
---|---|
32.67 | 0.00001 |
This means there is a statistically significant difference between the group means.
Example 2: Compare Two Groups
Group 1 | Group 2 |
---|---|
10 | 15 |
12 | 14 |
11 | 16 |
13 | 15 |
In Excel:
=ONEWAY_ANOVA({10;12;11;13}, {15;14;16;15})
Expected output:
F-statistic | p-value |
---|---|
21.0 | 0.0038 |
A low p-value indicates a significant difference between the two groups.
Example 3: No Significant Difference
Group 1 | Group 2 |
---|---|
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
In Excel:
=ONEWAY_ANOVA({5;6;7;8}, {5;6;7;8})
Expected output:
F-statistic | p-value |
---|---|
0.0 | 1.0 |
This means there is no significant difference between the group means.
Example 4: Three Groups, Small Sample
Group 1 | Group 2 | Group 3 |
---|---|---|
1 | 3 | 5 |
2 | 4 | 6 |
In Excel:
=ONEWAY_ANOVA({1;2}, {3;4}, {5;6})
Expected output:
F-statistic | p-value |
---|---|
12.0 | 0.0761 |
This means the difference is not statistically significant at the 0.05 level.
Python Code
import scipy.stats
def oneway_anova(group_one, group_two, group_three=None):
"""
Performs a one-way ANOVA test to compare the means of two or three independent groups.
Args:
group_one: 2D list of float, required. First group of sample data.
group_two: 2D list of float, required. Second group of sample data.
group_three: 2D list of float, optional. Third group of sample data.
Returns:
list: [[F-statistic (float), p-value (float)]] if successful, otherwise list of list of str with error message.
This example function is provided as-is without any representation of accuracy.
"""
def flatten(group):
if not isinstance(group, list):
return None
flat = []
for row in group:
if isinstance(row, list):
flat.extend(row)
else:
flat.append(row)
return flat
groups = []
for g in [group_one, group_two, group_three]:
if g is not None:
flat = flatten(g)
if flat is None or not all(isinstance(x, (int, float)) for x in flat):
return [["Each group must be a 2D list of numbers."]]
groups.append(flat)
if len(groups) < 2:
return [["At least two groups are required."]]
try:
f_stat, p_val = scipy.stats.f_oneway(*groups)
return [[round(float(f_stat), 2), round(float(p_val), 5)]]
except Exception as e:
return [[str(e)]]
Live Notebook
Edit this function in a live notebook .