| pcvar | R Documentation |
The function returns the proportion of variance explained by each axis in a principal component analysis (PCA).
pcvar( x = NULL, sdev = NULL, dimensions = "all", rounding = 4, as.percent = TRUE )
x |
An object of class |
sdev |
An alternative way to input standard deviations, as a numeric vector. |
dimensions |
The number of dimensions to return, or (by default) "all". |
rounding |
The number of decimal places to return. |
as.percent |
A logical factor specifying whether to report values as a percentage. |
This function outputs a named vetor with decimal (numeric) or percent (character)
proportions of variance from a PCA. It will take either an object of class prcomp
or standard deviations can be passed to sdev. The function also works with PCA
objects created by gm.prcomp.
The same information can be found by running summary on a prcomp object
but the simpler output of pcvar makes it convenient to be passes to plotting
functions, such various geom's in ggplot2.
Dave Angelini david.r.angelini@gmail.com [aut, cre]
# PCA
car.pca <- prcomp(mtcars[,-c(2,8:11)], center = TRUE, scale. = TRUE)
# Report proprotional variance from each PC axis
pcvar(car.pca)
pcvar(car.pca, as.percent = FALSE)
pcvar(car.pca, rounding = 2, as.percent = FALSE)
pcvar(car.pca, dimensions = dim(car.pca$x)[2])
# Standard deviations can be passed directly
pcvar(sdev = car.pca$sdev)
# Use the pcvar output to annotate the axes of a PC-space
car.pcvar <- pcvar(car.pca)
dim1 <- 1
dim2 <- 2
plot(x = car.pca$x[,dim1], y = car.pca$x[,dim2],
xlab = paste0(names(car.pcvar[dim1])," (",car.pcvar[dim1],")"),
ylab = paste0(names(car.pcvar[dim2])," (",car.pcvar[dim2],")"),
col = as.factor(strtrim(rownames(mtcars),3))
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.