SIEGELSLOPES
Overview
The SIEGELSLOPES
function computes the Siegel repeated medians estimator for robust linear regression, providing estimates for the slope and intercept of the best-fit line through a set of points. This method is highly robust to outliers, with a breakdown point of 50%, making it suitable for datasets with significant contamination or non-normal errors. The calculation is based on the repeated medians approach:
The intercept is estimated as the median of y - slope * x
(hierarchical method). For more details, see the scipy.stats.siegelslopes documentation .
This example function is provided as-is without any representation of accuracy.
Excel does not provide a direct equivalent to Siegel’s repeated medians regression. While Excel’s LINEST and related functions (SLOPE, INTERCEPT, RSQ) perform ordinary least squares regression, they are not robust to outliers. Robust regression methods like Siegel’s estimator are not available in Excel natively and require manual implementation or third-party add-ins.
Usage
To use the function in Excel:
=SIEGELSLOPES(y, [x], [method])
y
(2D list, required): Dependent variable. Each row is an observation.x
(2D list, optional, default: sequence 0, 1, …, n-1): Independent variable. Must have the same number of rows asy
.method
(string, optional, default: “hierarchical”): Method for intercept estimation. Either “hierarchical” or “separate”.
The function returns a 1x2 array: [slope, intercept]
(both floats), or an error message (string) if the input is invalid.
Examples
Example 1: Simple Linear Data
Inputs:
y | x |
---|---|
1 | 0 |
2 | 1 |
3 | 2 |
4 | 3 |
Excel formula:
=SIEGELSLOPES({1;2;3;4}, {0;1;2;3})
Expected output:
Slope | Intercept |
---|---|
1 | 1 |
Example 2: Data with Outlier
y | x |
---|---|
1 | 0 |
2 | 1 |
100 | 2 |
4 | 3 |
Excel formula:
=SIEGELSLOPES({1;2;100;4}, {0;1;2;3})
Expected output:
Slope | Intercept |
---|---|
1 | 1 |
Example 3: Default x (sequence)
y |
---|
2 |
4 |
6 |
8 |
Excel formula:
=SIEGELSLOPES({2;4;6;8})
Expected output:
Slope | Intercept |
---|---|
2 | 2 |
Example 4: Separate Method
y | x |
---|---|
1 | 0 |
2 | 1 |
3 | 2 |
4 | 3 |
Excel formula:
=SIEGELSLOPES({1;2;3;4}, {0;1;2;3}, "separate")
Expected output:
Slope | Intercept |
---|---|
1 | 1 |
Python Code
from scipy.stats import siegelslopes as scipy_siegelslopes
def siegelslopes(y, x=None, method="hierarchical"):
"""
Compute the Siegel repeated medians estimator for robust linear regression.
Args:
y: 2D list of dependent variable values. Each row is an observation. Must have at least two rows.
x: 2D list of independent variable values (optional). Must have the same number of rows as y if provided.
method: Method for intercept estimation ("hierarchical" or "separate").
Returns:
A 1x2 list: [slope, intercept] (both floats), or an error message (str) if input is invalid.
The input y must be a 2D list with at least two rows.
This example function is provided as-is without any representation of accuracy.
"""
# Validate y
if not isinstance(y, list) or len(y) < 2:
return "Invalid input: y must be a 2D list with at least two rows."
try:
y_flat = [float(row[0]) if isinstance(row, list) else float(row) for row in y]
except Exception:
return "Invalid input: y must contain numeric values."
n = len(y_flat)
# Validate x
if x is not None:
if not isinstance(x, list) or len(x) != n:
return "Invalid input: x must be a 2D list with the same number of rows as y."
try:
x_flat = [float(row[0]) if isinstance(row, list) else float(row) for row in x]
except Exception:
return "Invalid input: x must contain numeric values."
else:
x_flat = list(range(n))
# Validate method
if method not in ("hierarchical", "separate"):
return "Invalid input: method must be 'hierarchical' or 'separate'."
try:
res = scipy_siegelslopes(y_flat, x_flat, method=method)
slope = float(res.slope)
intercept = float(res.intercept)
# Disallow nan/inf
if any([slope != slope, intercept != intercept]):
return "Computation resulted in NaN."
if any([abs(slope) == float('inf'), abs(intercept) == float('inf')]):
return "Computation resulted in infinite value."
return [[slope, intercept]]
except Exception as e:
return f"siegelslopes error: {e}"