Learn R Programming

Rdimtools (version 1.0.6)

do.pca: Principal Component Analysis

Description

do.pca performs a classical principal component analysis (PCA) using RcppArmadillo package for faster and efficient computation.

Usage

do.pca(
  X,
  ndim = 2,
  cor = FALSE,
  preprocess = c("center", "scale", "cscale", "decorrelate", "whiten")
)

Arguments

X

an \((n\times p)\) matrix whose rows are observations and columns represent independent variables.

ndim

an integer-valued target dimension.

cor

mode of eigendecomposition. FALSE for decomposing covariance matrix, and TRUE for correlation matrix.

preprocess

an option for preprocessing the data. This supports three methods, where default is "center". See also aux.preprocess for more details.

Value

a named list containing

Y

an \((n\times ndim)\) matrix whose rows are embedded observations.

vars

a vector containing variances of projected data onto principal components.

projection

a \((p\times ndim)\) whose columns are principal components.

trfinfo

a list containing information for out-of-sample prediction.

References

pearson_liii_1901Rdimtools

Examples

Run this code
# NOT RUN {
## use iris data
data(iris)
set.seed(100)
subid = sample(1:150,50)
X     = as.matrix(iris[subid,1:4])
lab   = as.factor(iris[subid,5])

## try different preprocessing procedure
out1 <- do.pca(X, ndim=2, preprocess="center")
out2 <- do.pca(X, ndim=2, preprocess="decorrelate")
out3 <- do.pca(X, ndim=2, preprocess="whiten")

## embeddings for each procedure
Y1 <- out1$Y; Y2 <- out2$Y; Y3 <- out3$Y

## visualize
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,3))
plot(Y1, col=lab, pch=19, main="PCA::'center'")
plot(Y2, col=lab, pch=19, main="PCA::'decorrelate'")
plot(Y3, col=lab, pch=19, main="PCA::'whiten'")
par(opar)

# }

Run the code above in your browser using DataLab