| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/stats-prcomp.R |
| 3 |
\name{tidy.prcomp} |
| 4 |
\alias{tidy.prcomp} |
| 5 |
\alias{prcomp_tidiers} |
| 6 |
\title{Tidy a(n) prcomp object} |
| 7 |
\usage{ |
| 8 |
\method{tidy}{prcomp}(x, matrix = "u", ...) |
| 9 |
} |
| 10 |
\arguments{ |
| 11 |
\item{x}{A \code{prcomp} object returned by \code{\link[stats:prcomp]{stats::prcomp()}}.} |
| 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 |
Tidy summarizes information about the components of a model. |
| 74 |
A model component might be a single term in a regression, a single |
| 75 |
hypothesis, a cluster, or a class. Exactly what tidy considers to be a |
| 76 |
model component varies across models but is usually self-evident. |
| 77 |
If a model has several distinct types of components, you will need to |
| 78 |
specify which components to return. |
| 79 |
} |
| 80 |
\details{ |
| 81 |
See https://stats.stackexchange.com/questions/134282/relationship-between-svd-and-pca-how-to-use-svd-to-perform-pca |
| 82 |
for information on how to interpret the various tidied matrices. Note |
| 83 |
that SVD is only equivalent to PCA on centered data. |
| 84 |
} |
| 85 |
\examples{ |
| 86 |
\dontshow{if (rlang::is_installed(c("maps", "ggplot2"))) withAutoprint(\{ # examplesIf} |
| 87 |
|
| 88 |
pc <- prcomp(USArrests, scale = TRUE) |
| 89 |
|
| 90 |
# information about rotation |
| 91 |
tidy(pc) |
| 92 |
|
| 93 |
# information about samples (states) |
| 94 |
tidy(pc, "samples") |
| 95 |
|
| 96 |
# information about PCs |
| 97 |
tidy(pc, "pcs") |
| 98 |
|
| 99 |
# state map |
| 100 |
library(dplyr) |
| 101 |
library(ggplot2) |
| 102 |
library(maps) |
| 103 |
|
| 104 |
pc |> |
| 105 |
tidy(matrix = "samples") |> |
| 106 |
mutate(region = tolower(row)) |> |
| 107 |
inner_join(map_data("state"), by = "region") |> |
| 108 |
ggplot(aes(long, lat, group = group, fill = value)) + |
| 109 |
geom_polygon() + |
| 110 |
facet_wrap(~PC) + |
| 111 |
theme_void() + |
| 112 |
ggtitle("Principal components of arrest data") |
| 113 |
|
| 114 |
au <- augment(pc, data = USArrests) |
| 115 |
|
| 116 |
au |
| 117 |
|
| 118 |
ggplot(au, aes(.fittedPC1, .fittedPC2)) + |
| 119 |
geom_point() + |
| 120 |
geom_text(aes(label = .rownames), vjust = 1, hjust = 1) |
| 121 |
\dontshow{\}) # examplesIf} |
| 122 |
} |
| 123 |
\seealso{ |
| 124 |
\code{\link[stats:prcomp]{stats::prcomp()}}, \link{svd_tidiers} |
| 125 |
|
| 126 |
Other svd tidiers: |
| 127 |
\code{\link{augment.prcomp}()}, |
| 128 |
\code{\link{tidy_irlba}()}, |
| 129 |
\code{\link{tidy_svd}()} |
| 130 |
} |
| 131 |
\concept{svd tidiers} |