Learn R Programming

specmine (version 4.0.0)

pca_robust: Robust PCA analysis

Description

Performs robust Principal Component Analysis using pcaPP::PCAgrid().

Usage

pca_robust(
  dataset,
  center = "median",
  scale = "mad",
  k = 10,
  write.file = FALSE,
  file.out = NULL,
  ...
)

Value

The object returned by pcaPP::PCAgrid(). Depending on the version of pcaPP, the result contains robust PCA scores, loadings, component deviations and the centring and scaling information used in the analysis. When scores are available, they can be accessed with pca.result$scores; loadings can be accessed with pca.result$loadings.

Arguments

dataset

Dataset to analyse. The numeric data matrix must be stored in dataset$data, with variables in rows and samples in columns.

center

Method used to centre the data. Common choices are "mean", "median" or NULL, according to the options supported by pcaPP::PCAgrid().

scale

Method used to scale the data. Common choices are "sd", "mad" or NULL, according to the options supported by pcaPP::PCAgrid().

k

Number of robust principal components to compute.

write.file

Logical indicating whether the robust PCA scores and loadings should be written to CSV files.

file.out

Output file prefix used when write.file = TRUE. The function creates <file.out>_scores.csv and <file.out>_loadings.csv.

...

Additional arguments passed to pcaPP::PCAgrid().

Details

The dataset is expected to contain variables in rows and samples in columns. The data matrix is transposed internally so that samples are treated as observations and variables as features.

Examples

Run this code
if (requireNamespace("pcaPP", quietly = TRUE)) {
  datamat <- matrix(
    rnorm(40),
    nrow = 4,
    ncol = 10,
    dimnames = list(
      paste0("feature", 1:4),
      paste0("sample", 1:10)
    )
  )

  dataset <- list(data = datamat)

  robust.result <- pca_robust(
    dataset,
    center = "median",
    scale = "mad",
    k = 2
  )

  is.list(robust.result)
}

# \donttest{
if (requireNamespace("pcaPP", quietly = TRUE)) {
  datamat <- matrix(
    rnorm(40),
    nrow = 4,
    ncol = 10,
    dimnames = list(
      paste0("feature", 1:4),
      paste0("sample", 1:10)
    )
  )

  dataset <- list(data = datamat)
  file.prefix <- file.path(tempdir(), "robust_pca_example")

  pca_robust(
    dataset,
    k = 2,
    write.file = TRUE,
    file.out = file.prefix
  )

  file.exists(paste0(file.prefix, "_scores.csv"))
  file.exists(paste0(file.prefix, "_loadings.csv"))
}
# }

Run the code above in your browser using DataLab