R: Principal Components Analysis. (2024)

pcomp {SciViews}R Documentation

Description

Perform a principal components analysis on a matrix or data frame and returna pcomp object.

Usage

pcomp(x, ...)## S3 method for class 'formula'pcomp(formula, data = NULL, subset, na.action, method = c("svd", "eigen"), ...)## Default S3 method:pcomp(x, method = c("svd", "eigen"), scores = TRUE, center = TRUE, scale = TRUE, tol = NULL, covmat = NULL, subset = rep(TRUE, nrow(as.matrix(x))), ...)## S3 method for class 'pcomp'print(x, ...)## S3 method for class 'pcomp'summary(object, loadings = TRUE, cutoff = 0.1, ...)## S3 method for class 'summary.pcomp'print(x, digits = 3, loadings = x$print.loadings, cutoff = x$cutoff, ...)## S3 method for class 'pcomp'plot(x, which = c("screeplot", "loadings", "correlations", "scores"), choices = 1L:2L, col = par("col"), bar.col = "gray", circle.col = "gray", ar.length = 0.1, pos = NULL, labels = NULL, cex = par("cex"), main = paste(deparse(substitute(x)), which, sep = " - "), xlab, ylab, ...)## S3 method for class 'pcomp'screeplot(x, npcs = min(10, length(x$sdev)), type = c("barplot", "lines"), col = "cornsilk", main = deparse(substitute(x)), ...)## S3 method for class 'pcomp'points(x, choices = 1L:2L, type = "p", pch = par("pch"), col = par("col"), bg = par("bg"), cex = par("cex"), ...)## S3 method for class 'pcomp'lines(x, choices = 1L:2L, groups, type = c("p", "e"), col = par("col"), border = par("fg"), level = 0.9, ...)## S3 method for class 'pcomp'text(x, choices = 1L:2L, labels = NULL, col = par("col"), cex = par("cex"), pos = NULL, ...)## S3 method for class 'pcomp'biplot(x, choices = 1L:2L, scale = 1, pc.biplot = FALSE, ...)## S3 method for class 'pcomp'pairs(x, choices = 1L:3L, type = c("loadings", "correlations"), col = par("col"), circle.col = "gray", ar.col = par("col"), ar.length = 0.05, pos = NULL, ar.cex = par("cex"), cex = par("cex"), ...)## S3 method for class 'pcomp'predict(object, newdata, dim = length(object$sdev), ...)## S3 method for class 'pcomp'correlation(x, newvars, dim = length(x$sdev), ...)scores(x, ...)## S3 method for class 'pcomp'scores(x, labels = NULL, dim = length(x$sdev), ...)

Arguments

x

A matrix or data frame with numeric data.

...

Arguments passed to or from other methods. If 'xis a formula one might specifyscale =,tol =orcovmat ='.

formula

A formula with no response variable, referring only to numericvariables.

data

An optional data frame (or similar: see model.frame())containing the variables in the formula formula =. By default the variablesare taken from environment(formula).

subset

An optional vector used to select rows (observations) of thedata matrix x.

na.action

A function which indicates what should happen when the datacontain NAs. The default is set by the na.action = setting ofoptions(), and is na.fail() if that is not set. The 'factory-fresh'default is na.omit().

method

Either "svd" (using prcomp()), "eigen" (usingprincomp()), or an abbreviation.

scores

A logical value indicating whether the score on each principalcomponent should be calculated.

center

A logical value indicating whether the variables should beshifted to be zero centered. Alternately, a vector of length equal thenumber of columns of x can be supplied. The value is passed to scale =.Note that this argument is ignored for method = "eigen" and the dataset isalways centered in this case.

scale

A logical value indicating whether the variables should bescaled to have unit variance before the analysis takes place. The default isTRUE, which in general, is advisable. Alternatively, a vector of lengthequal the number of columns of x can be supplied. The value is passed toscale().

tol

Only when method = "svd". A value indicating the magnitudebelow which components should be omitted. (Components are omitted if theirstandard deviations are less than or equal to tol times the standarddeviation of the first component.) With the default null setting, nocomponents are omitted. Other settings for tol = could be tol = 0 ortol = sqrt(.Machine$double.eps), which would omit essentially constantcomponents.

covmat

A covariance matrix, or a covariance list as returned bycov.wt() (and cov.mve() or cov.mcd() from package MASS). Ifsupplied, this is used rather than the covariance matrix of x.

object

A 'pcomp' object.

loadings

Do we also summarize the loadings?

cutoff

The cutoff value below which loadings are replaced by whitespaces in the table. That way, larger values are easier to spot and to readin large tables.

digits

The number of digits to print.

which

The graph to plot.

choices

Which principal axes to plot. For 2D graphs, specify twointegers.

col

The color to use in graphs.

bar.col

The color of bars in the screeplot.

circle.col

The color for the circle in the loadings or correlationsplots.

ar.length

The length of the arrows in the loadings and correlationsplots.

pos

The position of text relative to arrows in loadings andcorrelation plots.

labels

The labels to write. If NULL default values are computed.

cex

The factor of expansion for text (labels) in the graphs.

main

The title of the graph.

xlab

The label of the x-axis.

ylab

The label of the y-axis.

npcs

The number of principal components to represent in the screeplot.

type

The type of screeplot ("barplot" or "lines") or pairs plot("loadings" or "correlations").

pch

The type of symbol to use.

bg

The background color for symbols.

groups

A grouping factor.

border

The color of the border.

level

The probability level to use to draw the ellipse.

pc.biplot

Do we create a Gabriel's biplot (see biplot())?

ar.col

Color of arrows.

ar.cex

Expansion factor for terxt on arrows.

newdata

New individuals with observations for the same variables asthose used for calculating the PCA. You can then plot these additionalindividuals in the scores plot.

dim

The number of principal components to keep.

newvars

New variables with observations for same individuals as thoseused for mcalculating the PCA. Correlation with PCs is calculated. You canthen plot these additional variables in the correlation plot.

Details

pcomp() is a generic function with "formula" and "default"methods. It is essentially a wrapper around prcomp() and princomp() toprovide a coherent interface and object for both methods.

A 'pcomp' object is created. It inherits from 'pca' (as in labdsvpackage, but not compatible with the 'pca' object of package ade4) and of'princomp'.

For more information on calculation done, refer to prcomp() formethod = "svd" or princomp() for method = "eigen".

Value

A c("pcomp", "pca", "princomp") object.

Note

The signs of the columns of the loadings and scores are arbitrary, andso may differ between functions for PCA, and even between different builds ofR.

Author(s)

Philippe Grosjean phgrosjean@sciviews.org, but the core code isindeed in package stats.

See Also

vectorplot(), prcomp(), princomp(), loadings(),Correlation()

Examples

# We will analyze mtcars without the Mercedes data (rows 8:14)data(mtcars)cars.pca <- pcomp(~ mpg + cyl + disp + hp + drat + wt + qsec, data = mtcars, subset = -(8:14))cars.pcasummary(cars.pca)screeplot(cars.pca)# Loadings are extracted and plotted like this(cars.ldg <- loadings(cars.pca))plot(cars.pca, which = "loadings") # Equivalent to vectorplot(cars.ldg)# Similarly, correlations of variables with PCs are extracted and plotted(cars.cor <- Correlation(cars.pca))plot(cars.pca, which = "correlations") # Equivalent to vectorplot(cars.cor)# One can add supplementary variables on this graphlines(Correlation(cars.pca, newvars = mtcars[-(8:14), c("vs", "am", "gear", "carb")]))# Plot the scoresplot(cars.pca, which = "scores", cex = 0.8) # Similar to plot(scores(x)[, 1:2])# Add supplementary individuals to this plot (labels), also points() or lines()text(predict(cars.pca, newdata = mtcars[8:14, ]), col = "gray", cex = 0.8)# Pairs plot for 3 PCsiris.pca <- pcomp(iris[, -5])pairs(iris.pca, col = (2:4)[iris$Species])

[Package SciViews version 0.9-13.1 Index]

R: Principal Components Analysis. (2024)

References

Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 5665

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.