-
Notifications
You must be signed in to change notification settings - Fork 8
diagnostics
Ivan Svetunkov edited this page Feb 19, 2026
·
3 revisions
The diagnostics module provides functions for detecting outliers in fitted regression models. The outlier_dummy() function identifies observations that lie outside the expected distribution bounds and creates dummy variables that can be used to re-estimate the model with outlier effects removed.
# R — function is in the greybox namespace
library(greybox)# Python
from greybox.diagnostics import outlier_dummy, OutlierResult- Extract residuals from the fitted model
- Compute standardised (
rstandard) or studentised (rstudent) residuals using leverage (hat values) - Determine critical bounds based on the model's distribution family and the specified confidence level
- Flag observations whose standardised residuals fall outside the bounds
- Return a matrix of dummy variables (one column per outlier)
| Parameter | Type | Default | Description |
|---|---|---|---|
object |
alm |
— | Fitted alm model |
level |
numeric | 0.999 |
Confidence level for outlier detection |
type |
character | "rstandard" |
Residual type: "rstandard" or "rstudent"
|
| Parameter | Type | Default | Description |
|---|---|---|---|
model |
ALM |
— | Fitted ALM model |
level |
float |
0.999 |
Confidence level for outlier detection |
type |
str |
"rstandard" |
Residual type: "rstandard" or "rstudent"
|
| Field | Description |
|---|---|
$outliers |
Matrix of dummy variables (or NULL if no outliers) |
$statistic |
Critical values used for detection |
$id |
Indices of outlier observations |
$level |
Confidence level used |
$type |
Residual type used |
$errors |
Standardised/studentised residuals |
| Attribute | Type | Description |
|---|---|---|
.outliers |
np.ndarray or None
|
Matrix of dummy variables (n_obs x n_outliers), or None if no outliers |
.statistic |
np.ndarray |
Lower and upper critical bounds |
.id |
np.ndarray |
Indices of outlier observations |
.level |
float |
Confidence level used |
.type |
str |
Residual type used ("rstandard" or "rstudent") |
.errors |
np.ndarray |
The standardised/studentised residuals |
The critical bounds are computed using the quantile function of the distribution family:
| Distribution | Quantile Distribution |
|---|---|
dnorm, dlnorm, ds, dls, dbcnorm, dfnorm, drectnorm
|
Normal |
dlaplace, dllaplace
|
Laplace |
dlogis |
Logistic |
dt |
Student's t (df = residual df) |
dgnorm, dlgnorm
|
Generalised Normal |
dgamma |
Gamma |
dexp |
Exponential |
# R
library(greybox)
x <- rnorm(100)
y <- 2 * x + rnorm(100)
y[50] <- 100 # inject outlier
model <- alm(y ~ x, distribution="dnorm")
result <- outlierdummy(model, level=0.999)
print(result$id) # Which observations are outliers
print(result$outliers) # Dummy variable matrix# Python
import numpy as np
from greybox.alm import ALM
from greybox.formula import formula
from greybox.diagnostics import outlier_dummy
np.random.seed(42)
x = np.random.randn(100)
y = 2 * x + np.random.randn(100)
y[50] = 100 # inject outlier
data = {"y": y, "x": x}
y_vec, X = formula("y ~ x", data)
model = ALM(distribution="dnorm")
model.fit(X, y_vec)
result = outlier_dummy(model, level=0.999)
print(f"Outlier indices: {result.id}")
print(f"Critical bounds: {result.statistic}")# Python — add outlier dummies to the model
if result.outliers is not None:
X_with_dummies = np.column_stack([X, result.outliers])
model2 = ALM(distribution="dnorm")
model2.fit(X_with_dummies, y_vec)
print(f"Original scale: {model.scale:.4f}")
print(f"With dummies: {model2.scale:.4f}")# Python — rstudent is more sensitive to single outliers
result_student = outlier_dummy(model, level=0.999, type="rstudent")
print(f"Outliers (rstudent): {result_student.id}")| R Function | Python Function |
|---|---|
outlierdummy() |
outlier_dummy() |
- Cook, R.D. and Weisberg, S. (1982). Residuals and Influence in Regression. Chapman and Hall.
- Svetunkov, I. (2023). Statistics for Business Analytics. https://openforecast.org/sba/