Learn R Programming

broom (version 0.4.0)

svd_tidiers: Tidying methods for singular value decomposition

Description

These methods tidy the U, D, and V matrices returned by the svd function into a tidy format. Because svd returns a list without a class, this function has to be called by tidy.list when it recognizes a list as an SVD object.

Usage

tidy_svd(x, matrix = "u", ...)

Arguments

x
list containing d, u, v components, returned from svd
matrix
which of the u, d or v matrix to tidy
...
Extra arguments (not used)

Value

  • An SVD object contains a decomposition into u, d, and v matrices, such that u %*% diag(d) %*% t(v) gives the original matrix. This tidier gives a choice of which matrix to tidy.

    When matrix = "u", each observation represents one pair of row and principal component, with variables:

  • rowNumber of the row in the original data being described
  • PCPrincipal component
  • loadingLoading of this principal component for this row
  • When matrix = "d", each observation represents one principal component, with variables:
  • PCPrincipal component
  • dValue in the d vector
  • percentPercent of variance explained by this PC, which is proportional to $d^2$
  • When matrix = "v", each observation represents a pair of a principal component and a column of the original matrix, with variables:
  • columnColumn of original matrix described
  • PCPrincipal component
  • valueValue of this PC for this column

See Also

svd, tidy.list

Examples

Run this code
mat <- as.matrix(iris[, 1:4])
s <- svd(mat)

tidy_u <- tidy(s, matrix = "u")
head(tidy_u)

tidy_d <- tidy(s, matrix = "d")
tidy_d

tidy_v <- tidy(s, matrix = "v")
head(tidy_v)

library(ggplot2)
library(dplyr)

ggplot(tidy_d, aes(PC, percent)) +
    geom_point() +
    ylab("% of variance explained")

tidy_u %>%
    mutate(Species = iris$Species[row]) %>%
    ggplot(aes(Species, loading)) +
    geom_boxplot() +
    facet_wrap(~ PC, scale = "free_y")

Run the code above in your browser using DataLab