boxB: BoxPlot based outlier detection

View source: R/boxB.R

boxBR Documentation

BoxPlot based outlier detection

Description

Identifies univariate outliers by using methods based on BoxPlots

Usage

boxB(x, k=1.5, method=c('asymmetric', 'asymmetric2', 'resistant', 
     'standard', 'adjbox'), weights=NULL, id=NULL, exclude=NA, 
     verbose=TRUE)

Arguments

x

Numeric data vector on which outlier detection will be performed.

k

Single nonnegative constant that determines the extension of the 'whiskers'. Commonly used values are 1.5 (default, inner fence), or 3 (outer fence, rarely used). The constant k is used by all the methods;but, with method="adjbox" only the value 1.5 is allowed (see Details).

method

Character, identifies the method to be used: method="resistant" (or equivalently method="standard") provides the ‘standard’ boxplot fences; method="asymmetric" is a modification of standard method (Kimber, 1990) to deal with (moderately) skewed data; method="asymmetric2" is the modification proposed by Walker et al (2018) to deal with asymmetric distributions;method="adjbox" uses Hubert and Vandervieren (2008) adjusted boxplot for skewed distributions.

weights

Optional numeric vector with units' weights associated to the observations in x; it must have the same length of x. Only nonnegative weights (without missing values) are allowed. Weights are used in estimating the quartiles (see Details). Note that they are ignored (with a warning) when method="adjbox".

id

Optional vector with identifiers of units in x; it must have the same length of x. If missing (id=NULL, default) the identifiers will be set equal to the positions in the vector (i.e. id=1:length(x)).

exclude

Values of x that will be excluded by the analysis. Missing values (NA and NaN) are always excluded, whatever the value of this argument (default is exclude=NA).

verbose

Logical, if TRUE (default) messages are printed (skewness measure, number of outliers in each tail, etc.). Set it to FALSE to suppress them, e.g. when the function is called in a loop.

Details

When method="resistant" (or "standard") the outlying observations are those outside the interval:

[Q_1 - k \times IQR;\quad Q_3 + k \times IQR]

where Q_1 and Q_3 are respectively the 1st and the 3rd quartile of x, while IQR=(Q_3 - Q_1) is the Inter-Quartile Range. The value k=1.5 (said 'inner fences') is commonly used when drawing a boxplot. Values k=2 and k=3 provide middle and outer fences, respectively.

When method="asymmetric" the outlying observations are those outside the interval:

[Q_1 - 2k \times (Q_2-Q_1);\quad Q_3 + 2k \times (Q_3-Q_2)]

being Q_2 the median; such a modification allows to account for slight skewness of the distribution. A warning is issued when Q_2-Q_1 or Q_3-Q_2 is (almost) zero, because in such a case the corresponding fence collapses on the quartile.

When method="asymmetric2" the outlying observations are identified using the Walker et al (2018) modified boxplot:

[Q_1 - k \times IQR \times \frac{(1-B)}{(1+B)};\quad Q_3 + k \times IQR \times \frac{(1+B)}{(1-B)}]

being B the Bowley's skewness coefficient (see skew.misc). In the degenerate cases the function issues a warning: when IQR=0 the Bowley's coefficient is undefined and the fences are set equal to the quartiles; when |B|=1 (i.e. Q_2 equals Q_1 or Q_3) the symmetric fences Q_1 - k \times IQR and Q_3 + k \times IQR are used.

Finally, when method="adjbox" the outlying observations are identified using the method proposed by Hubert and Vandervieren (2008) and based on the Medcouple measure of skewness; in practice the bounds are:

[Q_1-k \times e^{aM} \times IQR;\quad Q_3+k \times e^{bM}\times IQR ]

Where M is the medcouple; when M > 0 (positive skewness) then a = -4 and b = 3; on the contrary a = -3 and b = 4 for negative skewness (M < 0). This adjustment of the boxplot, according to Hubert and Vandervieren (2008), works with moderate skewness (-0.6 \leq M \leq 0.6). The bounds of the adjusted boxplot are derived by applying the function adjboxStats in the package robustbase, with coef=1.5. The medcouple is computed by mc; if the default call fails, the function tries again with doScale=TRUE.

When weights are available (passed via the argument weights) then they are used in the computation of the quartiles for the methods "resistant", "standard", "asymmetric" and "asymmetric2". In particular, the quartiles are derived using the function wtd.quantile in the package Hmisc, with weights treated as frequencies (normwt=FALSE). Note that the definition of the (weighted) quantiles used by wtd.quantile differs slightly from the one used by quantile (type 7) in the unweighted case, so the results may differ even when all the weights are equal.

The medcouple has no weighted version, therefore with method="adjbox" the weights are ignored (a warning is issued).

Value

The output is a list containing the following components:

quartiles

The quartiles of x after discarding the values in the exclude argument (and the missing values). When weights are provided (and method is not "adjbox") they are used in quartiles estimation through the function wtd.quantile in the package Hmisc.

fences

The bounds of the interval (named lower and upper), values outside the interval are detected as outliers.

excluded

The identifiers or positions (when id=NULL) of units in x excluded by the computations, according to the argument exclude (missing values included).

outliers

The identifiers or positions (when id=NULL) of units in x detected as outliers.

lowOutl

The identifiers or positions (when id=NULL) of units in x detected as outliers in the lower tail of the distribution.

upOutl

The identifiers or positions (when id=NULL) of units in x detected as outliers in the upper tail of the distribution.

All the components are always returned; when no outliers are found outliers, lowOutl and upOutl are empty vectors.

Author(s)

Marcello D'Orazio mdo.statmatch@gmail.com

References

McGill, R., Tukey, J. W. and Larsen, W. A. (1978) ‘Variations of box plots’. The American Statistician, 32, pp. 12-16.

Kimber, A.C. (1990) ‘Exploratory data analysis for possibly censored data from skewed distributions’. Applied Statistics, 39, pp. 21-30.

Hubert, M., and Vandervieren, E. (2008) ‘An Adjusted Boxplot for Skewed Distributions’, Computational Statistics and Data Analysis, 52, pp. 5186-5201.

Walker, M.L., Dovoedo, Y.H., Chakraborti, S., and Hilton, C.W. (2018) ‘An Improved Boxplot for Univariate Data’. The American Statistician, 72, pp. 348-353

See Also

adjboxStats, mc, wtd.quantile, skew.misc

Examples


set.seed(321)
x <- rnorm(30, 50, 10)
x[10] <- 1
x[20] <- 100

out <- boxB(x = x, k = 1.5, method = 'asymmetric')
out$fences
out$outliers
x[out$outliers]

out <- boxB(x = x, k = 1.5, method = 'asymmetric2')
out$fences
out$outliers
x[out$outliers]

out <- boxB(x = x, k = 1.5, method = 'adjbox')
out$fences
out$outliers
x[out$outliers]

# missing values are excluded
x[24] <- NA
x.ids <- paste0('obs',1:30)
out <- boxB(x = x, k = 1.5, method = 'adjbox', id = x.ids)
out$excluded
out$fences
out$outliers

# weights are used in the estimation of the quartiles
set.seed(111)
w <- round(runif(n = 30, min=1, max=10))
out <- boxB(x = x, k = 1.5, method = 'asymmetric', id = x.ids, weights = w)
out$excluded
out$quartiles
out$fences
out$outliers
out$lowOutl
out$upOutl

# with method='adjbox' weights are ignored (a warning is issued)
out <- boxB(x = x, k = 1.5, method = 'adjbox', id = x.ids, weights = w, 
            verbose = FALSE)
out$fences


univOutl documentation built on Nov. 26, 2026, 1:07 a.m.