| svd_tidiers {broom} | R Documentation |
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.
tidy_svd(x, matrix = "u", ...)
x |
list containing d, u, v components, returned from |
matrix |
which of the u, d or v matrix to tidy |
... |
Extra arguments (not used) |
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:
row |
Number of the row in the original data being described |
PC |
Principal component |
loading |
Loading of this principal component for this row |
When matrix = "d", each observation represents one principal component,
with variables:
PC |
Principal component |
d |
Value in the |
percent |
Percent 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:
column |
Column of original matrix described |
PC |
Principal component |
value |
Value of this PC for this column |
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")