Learn R Programming

broom (version 0.4.3)

prcomp_tidiers: Tidying methods for principal components analysis via prcomp

Description

These tidiers operate on the results of a principal components analysis computed using prcomp. The tidy method returns a data frame with either the eigenvectors representing each row or each column.

Usage

# S3 method for prcomp
tidy(x, matrix = "u", ...)

# S3 method for prcomp augment(x, data = NULL, newdata, ...)

Arguments

x

an object of class "prcomp" resulting from a call to prcomp

matrix

character; Indicates which sets of eigenvectors are returned in tidy form. "v", "rotation", or "variables" will return information about each variable, while "u", "x", or "samples" (default) returns the loadings for each original row. "d" or "pcs" returns information about each principal component.

...

Extra arguments, not used

data

the original data on which principal components analysis was performed. This cannot be recovered from x. If newdata is supplied, data is ignored. If both data and newdata are missing, only the fitted locations on the principal components are returned.

newdata

data frame; new observations for which locations on principal components are sought.

Value

All tidying methods return a data.frame without rownames, whose structure depends on the method chosen.

If matrix is "u", "samples", or "x", the tidy method returns

row

The sample labels (rownames) of the data set on which PCA was performed

PC

An integer vector indicating the principal component

value

The value of the eigenvector (axis score) on the indicated principal component

If matrix is "v", "variables", or "rotation", the tidy method returns

row

The variable labels (colnames) of the data set on which PCA was performed

PC

An integer vector indicating the principal component

value

The value of the eigenvector (axis score) on the indicated principal component

If matrix is "d" or "pcs", the tidy method returns

PC

An integer vector indicating the principal component

std.dev

Standard deviation explained by this PC

percent

Percentage of variation explained

cumulative

Cumulative percentage of variation explained

The augment.prcomp method returns a data frame containing fitted locations on the principal components for the observed data plus either the original data or the new data if supplied via data or newdata respectively.

See Also

prcomp, svd_tidiers

Examples

Run this code
# NOT RUN {
pc <- prcomp(USArrests, scale = TRUE)

# information about rotation
head(tidy(pc))

# information about samples (states)
head(tidy(pc, "samples"))

# information about PCs
tidy(pc, "pcs")

# state map
library(dplyr)
library(ggplot2)

pc %>%
  tidy(matrix = "samples") %>%
  mutate(region = tolower(row)) %>%
  inner_join(map_data("state"), by = "region") %>%
  ggplot(aes(long, lat, group = group, fill = value)) +
  geom_polygon() +
  facet_wrap(~ PC) +
  theme_void() +
  ggtitle("Principal components of arrest data")

au <- augment(pc, data = USArrests)
head(au)

ggplot(au, aes(.fittedPC1, .fittedPC2)) +
  geom_point() +
  geom_text(aes(label = .rownames), vjust = 1, hjust = 1)

# }

Run the code above in your browser using DataLab