ReplaceMissingValues: ReplaceMissingValues.

View source: R/ReplaceMissingValues.R

ReplaceMissingValuesR Documentation

ReplaceMissingValues.

Description

ReplaceMissingValues will replace missing values within a numeric matrix based on a principal component analysis.

Usage

ReplaceMissingValues(x, ncomp = 10, silent = FALSE)

Arguments

x

Numeric matrix.

ncomp

Number of components to be used.

silent

FALSE, suppress messages setting silent=TRUE.

Details

The 'nipals' algorithm is used to basically perform a PCA on the sparse matrix. Missing values are imputed based on the major components observed. Please check also the 'impute.nipals' function from mixOmics which should basically give the same functionality since the 04/2021 update.

Value

A matrix of similar dimensions as x without missing values.

Examples

# load raw data and sample description
raw <- MetabolomicsBasics::raw
sam <- MetabolomicsBasics::sam

idx <- apply(raw, 2, CheckForOutliers, group = sam$GT, n_sd = 5, method = "logical")
sum(idx) # 215 values would be classified as outlier using a five-sigma band
old_vals <- raw[idx] # keep outlier values for comparison
raw_filt <- raw
raw_filt[idx] <- NA
raw_means <- apply(raw, 2, function(x) {
  sapply(split(x, sam$GT), mean, na.rm = TRUE)[as.numeric(sam$GT)]
})[idx]
raw_repl <- ReplaceMissingValues(x = raw_filt)
new_vals <- raw_repl[idx]
par(mfrow = c(2, 1))
breaks <- seq(-0.7, 1.3, 0.05)
hist(raw_means - old_vals, breaks = breaks, main = "", xlab = "Outliers", las = 1)
hist(raw_means - new_vals, breaks = breaks, main = "", xlab = "Replaced values", las = 1)

MetabolomicsBasics documentation built on Nov. 2, 2023, 5:07 p.m.