View source: R/ReplaceMissingValues.R
ReplaceMissingValues | R Documentation |
ReplaceMissingValues
will replace missing values within
a numeric matrix based on a principal component analysis.
ReplaceMissingValues(x, ncomp = 10, silent = FALSE)
x |
Numeric matrix. |
ncomp |
Number of components to be used. |
silent |
FALSE, suppress messages setting silent=TRUE. |
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.
A matrix of similar dimensions as x without missing values.
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.