This function identifies and corrects outliers in a dataset using principal
component analysis (PCA). It scales the data, performs PCA, computes idiosyncratic
components, and replaces values that fall outside a defined outlier threshold with
the median of 5 previous values. The outlier threshold is determined using the
interquartile range (IQR) method.
Usage
correct_outliers(data, r)
Value
A list containing:
data
A matrix with corrected data where outliers are replaced by the median of previous values.
outliers
A binary matrix (same dimensions as the input data) indicating the position of outliers.
Arguments
data
A numeric matrix or data frame where rows represent observations and
columns represent variables.
r
An integer specifying the number of principal components to use for PCA.