plot_top_loadings: Plot the largest PCA component loadings from a recipe or...

Description Usage Arguments Value Examples

View source: R/plot_top_loadings.R

Description

A plot of the n largest component loadings is produced.

Usage

1
2
3
4
5
6
7
plot_top_loadings(x, ...)

## S3 method for class 'recipe'
plot_top_loadings(x, ..., n = 4, id = NULL, type = "pca")

## S3 method for class 'workflow'
plot_top_loadings(x, ..., n = 4, id = NULL, type = "pca")

Arguments

x

A prepped recipe or fitted workflow that uses a recipe. The recipe must have used at least one recipes::step_pca() (or recipes::step_pls()).

...

An optional series of conditional statements used to filter the PCA data before plotting. See Details below.

n

The number of columns to plot (per component).

id

A single numeric or character value that is used to pick the step with the PCA results. If a single recipes::step_pca() (or recipes::step_pls()) was used, this argument is ignored. Note: if used, id must be named.

type

A character value ("pca" or "pls") for the type of step to use.

Value

A ggplot object.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
library(recipes)
library(parsnip)
library(workflows)
library(ggplot2)

data("cells", package = "modeldata")

theme_set(theme_minimal())

## -----------------------------------------------------------------------------

cell_pca <-
  recipe(class ~ ., data = cells %>% dplyr::select(-case)) %>%
  step_center(all_predictors()) %>%
  step_scale(all_predictors()) %>%
  step_pca(all_predictors())

# or when used in a workflow
lr_workflow <-
  workflow() %>%
  add_model(logistic_reg() %>% set_engine("glm")) %>%
  add_recipe(cell_pca)

## -----------------------------------------------------------------------------

cell_pca <- prep(cell_pca)

# What were the top 10 channel 1 columns in the first two components?
plot_top_loadings(cell_pca, grepl("ch_1", terms) & component_number <= 2, n = 10)

## -----------------------------------------------------------------------------

lr_workflow <- lr_workflow %>% fit(data = cells)

plot_top_loadings(lr_workflow, component_number <= 4)

tidymodels/learntidymodels documentation built on Dec. 14, 2021, 5:12 p.m.