x and the vector
\(\mu\) = center with respect to \(\Sigma\) = cov. This is
(for vector x) defined as $$D^2 = (x - \mu)' \Sigma^{-1} (x - \mu).$$
The fit.models package makes the mahalanobis function generic. The
default method calls the mahalanobis function in the
stats package. Methods for fitted model objects return the squared
Mahalanobis distance of all the rows in the design (model) matrix.mahalanobis(x, ...)# S3 method for default
mahalanobis(x, center, cov, inverted = FALSE, ...)
# S3 method for lm
mahalanobis(x, ...)
x is
a fitted model object then the design matrix (model matrix) is used.solve for computing the inverse
of the covariance matrix (if inverted is FALSE). Additional
arguments are ignored when x is a fitted model object.FALSE, the
centering step is skipped. This argument is ignored when x is a
fitted model object.x is a fitted model object.TRUE, cov is supposed to contain
the inverse of the covariance matrix. This argument is ignored when
x is a fitted model object.default: the default S3 method calls stats::mahalanobis lm: the method for lm objects returns the squared mahalanobis distance
for each row in the design (model) matrix. These values are computed from the diagonal elements
of the hat matrix.
stack.lm <- lm(stack.loss ~ ., data = stackloss)
# Mahalanobis distance (not squared)
sqrt(mahalanobis(stack.lm))
Run the code above in your browser using DataLab