Draw an outlier map showing the score distance and orthogonal
distance of each observation from a projection or principal component
analysis. The score distance measures how far an observation lies from the
centre within the projection subspace, while the orthogonal distance
measures how far it lies from the subspace. Pass object (the output of
stats::prcomp() or an rrcov Pca* function); otherwise supply scores and
loadings together with the original data. For a prcomp object, use its
rank. argument to set the number of retained components.
When object is a PCA-like object and show_thresholds = TRUE, the
score-distance and orthogonal-distance cutoffs are drawn as dashed lines and
observations are coloured by type:
small score and orthogonal distance.
large score distance, small orthogonal distance.
small score distance, large orthogonal distance.
large score and orthogonal distance.
The cutoffs are only defined for PCA-like objects, so show_thresholds is
ignored when scores and loadings are passed directly.
outlier_map(
object = NULL,
data = NULL,
scores = NULL,
loadings = NULL,
show_thresholds = TRUE,
...
)A ggplot object.
Optionally, the output of stats::prcomp() or an rrcov Pca*
function. For a prcomp object, set the number of retained components with
its rank. argument. If supplied, the scores and loadings arguments are
ignored.
The original data matrix or data frame used to compute the
projection, scaled if the projection was computed on scaled data. This is
required to compute the orthogonal distances, except when object is a
rrcov Pca* object (which stores them).
A matrix or data frame of scores, with one column per retained
component. Ignored if object is supplied.
A matrix or data frame of loadings, with one column per
retained component. Ignored if object is supplied.
If TRUE (the default) and object is a PCA-like
object, the score-distance and orthogonal-distance cutoffs are drawn as
dashed lines and observations are coloured by type. Ignored when scores
and loadings are passed directly.
Additional arguments passed to ggplot2::geom_point().
Rob J Hyndman
Hyndman, R J (2026) "That's weird: Anomaly detection using R", Chapter 9, https://OTexts.com/weird/.
Y <- oldfaithful[, c("duration", "waiting")]
prcomp(Y, scale = TRUE, rank. = 1) |>
outlier_map(data = Y)
Run the code above in your browser using DataLab