Learn R Programming

specmine (version 4.0.0)

pca_analysis_dataset: Classical PCA analysis

Description

Performs classical Principal Component Analysis (PCA) on a specmine dataset using stats::prcomp().

Usage

pca_analysis_dataset(
  dataset,
  scale = TRUE,
  center = TRUE,
  write.file = FALSE,
  file.out = NULL,
  ...
)

Value

An object of class prcomp. The returned object contains:

  • x: a matrix of PCA scores, with one row per sample;

  • rotation: a matrix of PCA loadings, with one row per variable;

  • sdev: the standard deviations of the principal components;

  • center: the centring values used by the PCA;

  • scale: the scaling values used by the PCA when scale = TRUE;

  • call: the matched function call.

The row names of x correspond to sample names when the columns of dataset$data are named. The row names of rotation correspond to variable names when the rows of dataset$data are named.

Arguments

dataset

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

scale

Logical indicating whether variables should be scaled before PCA. The default is TRUE.

center

Logical indicating whether variables should be centred before PCA. The default is TRUE.

write.file

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

file.out

Output file prefix used when write.file = TRUE. Two files are created: <file.out>_scores.csv and <file.out>_loadings.csv.

...

Additional arguments passed to stats::prcomp().

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
datamat <- matrix(
  rnorm(40),
  nrow = 4,
  ncol = 10,
  dimnames = list(
    paste0("feature", 1:4),
    paste0("sample", 1:10)
  )
)

dataset <- list(data = datamat)

pca.result <- pca_analysis_dataset(
  dataset,
  scale = TRUE,
  center = TRUE
)

dim(pca.result$x)
dim(pca.result$rotation)

# \donttest{
file.prefix <- file.path(tempdir(), "pca_example")

pca_analysis_dataset(
  dataset,
  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