| boxB | R Documentation |
Identifies univariate outliers by using methods based on BoxPlots
boxB(x, k=1.5, method=c('asymmetric', 'asymmetric2', 'resistant',
'standard', 'adjbox'), weights=NULL, id=NULL, exclude=NA,
verbose=TRUE)
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 |
method |
Character, identifies the method to be used: |
weights |
Optional numeric vector with units' weights associated to the observations in |
id |
Optional vector with identifiers of units in |
exclude |
Values of |
verbose |
Logical, if |
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).
The output is a list containing the following components:
quartiles |
The quartiles of |
fences |
The bounds of the interval (named |
excluded |
The identifiers or positions (when |
outliers |
The identifiers or positions (when |
lowOutl |
The identifiers or positions (when |
upOutl |
The identifiers or positions (when |
All the components are always returned; when no outliers are found outliers, lowOutl and upOutl are empty vectors.
Marcello D'Orazio mdo.statmatch@gmail.com
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
adjboxStats, mc, wtd.quantile, skew.misc
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.