Learn R Programming

ldt (version 0.5.3)

s.pca: Principal Component Analysis

Description

This function performs PCA on the columns of a matrix.

Usage

s.pca(x, center = TRUE, scale = TRUE, newX = NULL)

Value

A list with the following items:

removed0Var

An integer vector showing the zero-based indices of removed columns with zero variances.

directions

Directions matrix.

stds

An integer vector showing the standard deviation of the principal components.

stds2Ratio

Shows stds^2/sum(stds^2).

projections

Projections matrix if newX is provided.

Arguments

x

A numeric matrix with variables in the columns.

center

Logical value indicating whether to demean the columns of x.

scale

Logical value indicating whether to scale the columns of x to unit variance.

newX

A numeric matrix to be used in projection. Its structure must be similar to x.

Details

The main purpose of exporting this statistics helper method is to show the inner calculations of the package.

See Also

get.options.pca

Examples

Run this code

set.seed(340)
data <- matrix(rnorm(500), nrow = 50, ncol = 10)

# using prcomp function
resR = prcomp(data, center = TRUE, scale. = TRUE)

# using s.pca in this package
res = s.pca(data,TRUE,TRUE,data)

# res$projections and resR$x must be equal
# res$directions and t(resR$rotation) must be equal

# ----- ANOTHER EXAMPLE: PCA where there is a constant variable:
data <- data.frame( x = rnorm(100), y = rnorm(100), z = rep(0, 100))

# using s.pca in this package
res <- s.pca(data)

# using prcomp function
res_invalid <- try(prcomp(data, center = TRUE,
                          scale. = TRUE))
# Fails, we should remove 'z' first

Run the code above in your browser using DataLab