pcvar: Proportional Variance from Principal Components

pcvarR Documentation

Proportional Variance from Principal Components

Description

The function returns the proportion of variance explained by each axis in a principal component analysis (PCA).

Usage

pcvar(
  x = NULL,
  sdev = NULL,
  dimensions = "all",
  rounding = 4,
  as.percent = TRUE
)

Arguments

x

An object of class prcomp.

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.

Details

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.

Source

Dave Angelini david.r.angelini@gmail.com [aut, cre]

Examples

# 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))
)

aphanotus/borealis documentation built on Nov. 4, 2022, 8:44 p.m.