F_ONEWAY
Overview
The F_ONEWAY
function performs a one-way analysis of variance (ANOVA) test for two or more independent samples, determining whether the means of the groups are statistically different. It wraps the scipy.stats.f_oneway function, which calculates the F statistic and p-value for the null hypothesis that all group means are equal. The test assumes samples are independent and normally distributed. Optionally, Welch’s ANOVA (for unequal variances) can be performed by setting equal_var
to FALSE
.
The F statistic is computed as:
where a higher F value indicates greater evidence against the null hypothesis.
This wrapper simplifies the function to accept only the most commonly used parameters: a 2D list of samples and the equal_var
flag. Parameters such as axis
, nan_policy
, and keepdims
are excluded for Excel compatibility. For more details, 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:
=F_ONEWAY(samples, [equal_var])
samples
(2D list, required): Table of numeric values, where each column is a group/sample. Must have at least two columns (groups) and three rows.equal_var
(bool, optional, default=TRUE
): IfTRUE
, assumes equal population variances (standard ANOVA). IfFALSE
, performs Welch’s ANOVA.
The function returns a single row with two values: the F statistic and the p-value ({F, p}
). If the input is invalid, an error message (string) is returned.
Examples
Example 1: Basic Case (Two Groups)
Inputs:
samples | equal_var | |
---|---|---|
1.1 | 2.2 | TRUE |
1.3 | 2.4 | |
1.2 | 2.3 |
Excel formula:
=F_ONEWAY({1.1,2.2;1.3,2.4;1.2,2.3})
Expected output:
F statistic | p-value |
---|---|
92.000 | 0.000 |
Example 2: Three Groups, Equal Variance
Inputs:
samples | equal_var | ||
---|---|---|---|
1.1 | 2.2 | 3.1 | TRUE |
1.3 | 2.4 | 3.3 | |
1.2 | 2.3 | 3.2 |
Excel formula:
=F_ONEWAY({1.1,2.2,3.1;1.3,2.4,3.3;1.2,2.3,3.2})
Expected output:
F statistic | p-value |
---|---|
236.000 | 0.000 |
Example 3: Three Groups, Unequal Variance (Welch’s ANOVA)
Inputs:
samples | equal_var | ||
---|---|---|---|
1.1 | 2.2 | 3.1 | FALSE |
1.3 | 2.4 | 3.3 | |
1.2 | 2.3 | 3.2 |
Excel formula:
=F_ONEWAY({1.1,2.2,3.1;1.3,2.4,3.3;1.2,2.3,3.2}, FALSE)
Expected output:
F statistic | p-value |
---|---|
236.000 | 0.000 |
Example 4: All Arguments Specified
Inputs:
samples | equal_var | |
---|---|---|
10.0 | 20.0 | TRUE |
12.0 | 22.0 | |
11.0 | 21.0 |
Excel formula:
=F_ONEWAY({10.0,20.0;12.0,22.0;11.0,21.0}, TRUE)
Expected output:
F statistic | p-value |
---|---|
144.000 | 0.000 |
Python Code
from scipy.stats import f_oneway as scipy_f_oneway
from typing import List, Optional, Union
def f_oneway(samples: List[List[float]], equal_var: bool = True) -> Union[List[List[Optional[float]]], str]:
"""
Performs a one-way ANOVA test for two or more independent samples.
Args:
samples: 2D list of float values. Each column represents a group/sample.
equal_var: If True, assumes equal population variances (standard ANOVA). If False, performs Welch's ANOVA.
Returns:
2D list with a single row: [F statistic, p-value], or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate samples is a 2D list with at least two columns (groups)
if not (isinstance(samples, list) and all(isinstance(row, list) for row in samples) and len(samples) >= 2):
return "Invalid input: samples must be a 2D list with at least two rows."
# Transpose if input is rows as groups (Excel may pass as columns or rows)
n_rows = len(samples)
n_cols = len(samples[0]) if n_rows > 0 else 0
if n_cols < 2:
return "Invalid input: samples must have at least two columns (groups)."
if n_rows < 3:
return "Invalid input: samples must have at least three rows."
# Check all elements are numeric and each group has at least two values
try:
groups = []
for col in range(n_cols):
group = [float(samples[row][col]) for row in range(n_rows)]
valid_group = [v for v in group if v is not None]
if len(valid_group) < 2:
return "Invalid input: each group must contain at least two numeric values."
# Check for at least two unique values in the group
if len(set(valid_group)) < 2:
return "Invalid input: each group must contain at least two unique values."
groups.append(valid_group)
except Exception:
return "Invalid input: samples must contain only numeric values."
# Call scipy.stats.f_oneway (only standard ANOVA supported in this version)
try:
result = scipy_f_oneway(*groups)
stat = float(result.statistic)
pvalue = float(result.pvalue)
except Exception as e:
return f"scipy.stats.f_oneway error: {e}"
# Check for nan/inf
for val in [stat, pvalue]:
if isinstance(val, float) and (val != val or val in [float('inf'), float('-inf')]):
return "Invalid result: output contains nan or inf."
return [[stat, pvalue]]