Creates a "pcaviz" data structure from PCA results (e.g., prcomp output, and accompanying data. The data structure is specifically designed to facilitate plotting PCA results and relationships between estimated principal components and related data (e.g., geographic co-ordinates).

pcaviz(out.pca, x = NULL, sdev = NULL, var = NULL, rotation = NULL,
       dat = NULL, pc.cols)

# S3 method for pcaviz
summary(object, …)

# S3 method for summary.pcaviz
print(x, n = 4, …)

# S3 method for pcaviz
print(x, n = 4, …)

Arguments

out.pca

Output of prcomp or princomp (stats package), or output of rpca (rsvd package), providing results of principal components analysis. Alternatively, PCA results (or results of applying another dimensionality reduction technique) may be provided via inputs x, rotation and dat (see below).

x

For function "pcaviz", this is a matrix or data frame containing the data samples projected onto the principal components (also sometimes referred to as "rotated data" or "scores"). This is equivalent to the x component returned by functions prcomp and rpca, and the scores component returned by function princomp. For function print.pcaviz, this should be an object of class "pcaviz"; for function print.summary.pcaviz, this should be the output of function summary.pcaviz.

sdev

Optional vector containing the standard deviations of the PCs or, equivalently, the square root of the eigenvalues. This is the same as the sdev component returned by functions prcomp, princomp and rpca.

var

Optional value specifying the total variance. This should be equal to the sum of the variances of the individual PCs. It can be computed as sum(apply(X,2,var)), in which X is the target matrix supplied as input to the PCA. This is the same as sum(sdev^2) when sdev is returned by prcomp or princomp, and is the same as the var component returned by rpca.

rotation

Optional matrix or data frame containing the eigenvectors (alternatively called "variable loadings" or "rotation matrix"). This is can be chosen as the rotation component returned by functions prcomp and rpca, or the loadings component returned by function princomp. Additional data columns beyond the eigenvectors may also be supplied so long as all PC loadings are included.

dat

Optional data frame supplying additional information about the data samples. It should be a data frame with one row for each data sample, and should have as many rows as the rotated data. Optionally, this data frame may also supply the rotated data, as indicated by input pc.cols. This is useful if the PCA results and accompanying data are already combined into one table.

pc.cols

Indicates which columns of input dat contain the principal components (rotated data). The names of the columns (not numeric indices) must be supplied. By default, when pc.cols is not specified and the rotated data are not already supplied by x or out.pca, the columns of dat with prefix "PC" are interpreted as principal components.

object

An object of class "pcaviz".

n

Maximum number of variables of each category to display in the summary.

Additional print and summary arguments. These additional arguments are not used here.

Value

pcaviz returns a list with class "pcaviz" containing the following components:

data

Data frame containing the rotated variables as well as accompanying data.

data.coltypes

Names of the columns in data that contain the rotated variables.

sdev

If not NULL, this is a vector containing the standard deviations of the PCs; equivalently, the square roots of the eigenvalues.

rotation

If not NULL, this is a data frame containing the eigenvectors ("loadings") and, optionally, other data columns.

basis

A matrix with one column per PC that is used to keep track of the principal axes after transformation.

transformed.pcs

A logical value indicating whether at least one transformation (e.g., a rotation) has been applied to the PCs. Initially set to FALSE.

It is advised to avoid directly manipulating these components as much as possible. Instead, it is preferrable to use package functions designed for manipulating pcaviz objects, e.g., subset, pcaviz_rotate, pcaviz_abbreviate_var.

See also

Examples

# Create pcaviz object from prcomp output. data(iris) a1 <- pcaviz(prcomp(iris[1:4]),dat = iris) # Create pcaviz object from princomp output. a2 <- pcaviz(princomp(iris[1:4]),dat = iris) # An alternative way to create pcaviz object from prcomp output. out <- prcomp(iris[1:4]) a3 <- pcaviz(x = out$x,sdev = out$sdev,var = sum(out$sdev^2), rotation = out$rotation,dat = iris)
# NOT RUN { # Print summaries of the pcaviz objects. summary(a1) summary(a2) summary(a3) # These vignettes demonstrate various ways pcaviz objects # can be created. vignette("iris") vignette("popres") vignette("regmap") # }