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", ...)
list containing d, u, v components, returned from svd
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:
Number of the row in the original data being described
Principal component
Loading of this principal component for this row
When matrix = "d", each observation represents one principal component, with variables:
Principal component
Value in the d
vector
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 of original matrix described
Principal component
Value of this PC for this column
# NOT RUN {
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