Debian Codegraph @

man/tidy_svd.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/list-svd.R
3 \name{tidy_svd}
4 \alias{tidy_svd}
5 \alias{svd_tidiers}
6 \title{Tidy a(n) svd object masquerading as list}
7 \usage{
8 tidy_svd(x, matrix = "u", ...)
9 }
10 \arguments{
11 \item{x}{A list with components \code{u}, \code{d}, \code{v} returned by \code{\link[base:svd]{base::svd()}}.}
12
13 \item{matrix}{Character specifying which component of the PCA should be
14 tidied.
15 \itemize{
16 \item \code{"u"}, \code{"samples"}, \code{"scores"}, or \code{"x"}: returns information about
17 the map from the original space into principle components space.
18 \item \code{"v"}, \code{"rotation"}, \code{"loadings"} or \code{"variables"}: returns information
19 about the map from principle components space back into the original
20 space.
21 \item \code{"d"}, \code{"eigenvalues"} or \code{"pcs"}: returns information about the
22 eigenvalues.
23 }}
24
25 \item{...}{Additional arguments. Not used. Needed to match generic
26 signature only. \strong{Cautionary note:} Misspelled arguments will be
27 absorbed in \code{...}, where they will be ignored. If the misspelled
28 argument has a default value, the default value will be used.
29 For example, if you pass \code{conf.lvel = 0.9}, all computation will
30 proceed using \code{conf.level = 0.95}. Two exceptions here are:
31 \itemize{
32 \item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if
33 it will be ignored.
34 \item \code{augment()} methods will warn when supplied a \code{newdata} argument if it
35 will be ignored.
36 }}
37 }
38 \value{
39 A \link[tibble:tibble]{tibble::tibble} with columns depending on the component of
40 PCA being tidied.
41
42 If \code{matrix} is \code{"u"}, \code{"samples"}, \code{"scores"}, or \code{"x"} each row in the
43 tidied output corresponds to the original data in PCA space. The columns
44 are:
45
46 \item{\code{row}}{ID of the original observation (i.e. rowname from original
47 data).}
48 \item{\code{PC}}{Integer indicating a principal component.}
49 \item{\code{value}}{The score of the observation for that particular principal
50 component. That is, the location of the observation in PCA space.}
51
52 If \code{matrix} is \code{"v"}, \code{"rotation"}, \code{"loadings"} or \code{"variables"}, each
53 row in the tidied output corresponds to information about the principle
54 components in the original space. The columns are:
55
56 \item{\code{row}}{The variable labels (colnames) of the data set on
57 which PCA was performed.}
58 \item{\code{PC}}{An integer vector indicating the principal component.}
59 \item{\code{value}}{The value of the eigenvector (axis score) on the
60 indicated principal component.}
61
62 If \code{matrix} is \code{"d"}, \code{"eigenvalues"} or \code{"pcs"}, the columns are:
63
64 \item{\code{PC}}{An integer vector indicating the principal component.}
65 \item{\code{std.dev}}{Standard deviation explained by this PC.}
66 \item{\code{percent}}{Fraction of variation explained by this component
67 (a numeric value between 0 and 1).}
68 \item{\code{cumulative}}{Cumulative fraction of variation explained by
69 principle components up to this component (a numeric value between 0 and
70 1).}
71 }
72 \description{
73 Broom tidies a number of lists that are effectively S3
74 objects without a class attribute. For example, \code{\link[stats:optim]{stats::optim()}},
75 \link[base:svd]{svd()} and \code{\link[interp:interp]{interp::interp()}} produce consistent output, but
76 because they do not have a class attribute, they cannot be handled by S3
77 dispatch.
78
79 These functions look at the elements of a list and determine if there is
80 an appropriate tidying method to apply to the list. Those tidiers are
81 implemented as functions of the form \verb{tidy_<function>} or
82 \verb{glance_<function>} and are not exported (but they are documented!).
83
84 If no appropriate tidying method is found, they throw an error.
85 }
86 \details{
87 See https://stats.stackexchange.com/questions/134282/relationship-between-svd-and-pca-how-to-use-svd-to-perform-pca
88 for information on how to interpret the various tidied matrices. Note
89 that SVD is only equivalent to PCA on centered data.
90 }
91 \examples{
92 \dontshow{if (rlang::is_installed(c("modeldata", "ggplot2"))) withAutoprint(\{ # examplesIf}
93
94 library(modeldata)
95 data(hpc_data)
96
97 mat <- scale(as.matrix(hpc_data[, 2:5]))
98 s <- svd(mat)
99
100 tidy_u <- tidy(s, matrix = "u")
101 tidy_u
102
103 tidy_d <- tidy(s, matrix = "d")
104 tidy_d
105
106 tidy_v <- tidy(s, matrix = "v")
107 tidy_v
108
109 library(ggplot2)
110 library(dplyr)
111
112 ggplot(tidy_d, aes(PC, percent)) +
113 geom_point() +
114 ylab("\% of variance explained")
115
116 tidy_u |>
117 mutate(class = hpc_data$class[row]) |>
118 ggplot(aes(class, value)) +
119 geom_boxplot() +
120 facet_wrap(~PC, scale = "free_y")
121 \dontshow{\}) # examplesIf}
122 }
123 \seealso{
124 \code{\link[base:svd]{base::svd()}}
125
126 Other svd tidiers:
127 \code{\link{augment.prcomp}()},
128 \code{\link{tidy.prcomp}()},
129 \code{\link{tidy_irlba}()}
130
131 Other list tidiers:
132 \code{\link{glance_optim}()},
133 \code{\link{list_tidiers}},
134 \code{\link{tidy_irlba}()},
135 \code{\link{tidy_optim}()},
136 \code{\link{tidy_xyz}()}
137 }
138 \concept{list tidiers}
139 \concept{svd tidiers}