Skip to contents

Extracts a per-component variance summary from a PCA fit. Accepts either an existing prcomp object or raw data, in which case prcomp is run first.

Usage

run_pca(data, center = TRUE, scale = TRUE, ...)

Arguments

data

A `prcomp` object, or a numeric matrix/data frame to fit PCA on.

center, scale

Passed to prcomp (as `center` and `scale.`) when `data` is raw data; ignored when `data` is already a `prcomp` object.

...

Further arguments passed to prcomp when `data` is raw data.

Value

A tibble with one row per component: `component`, `eigenvalue`, `difference`, `proportion`, `variance_pct`, `cum_variance_pct`.

Examples

run_pca(prcomp(mtcars[, 1:4], scale. = TRUE))
#> # A tibble: 4 × 6
#>   component eigenvalue difference proportion variance_pct cum_variance_pct
#>   <chr>          <dbl>      <dbl>      <dbl>        <dbl>            <dbl>
#> 1 PC1           3.50       3.26       0.876         87.6              87.6
#> 2 PC2           0.239      0.0742     0.0599         5.99             93.5
#> 3 PC3           0.165      0.0721     0.0413         4.13             97.7
#> 4 PC4           0.0931    NA          0.0233         2.33            100  

# Or fit the PCA in one step
run_pca(mtcars[, 1:4], scale = TRUE)
#> # A tibble: 4 × 6
#>   component eigenvalue difference proportion variance_pct cum_variance_pct
#>   <chr>          <dbl>      <dbl>      <dbl>        <dbl>            <dbl>
#> 1 PC1           3.50       3.26       0.876         87.6              87.6
#> 2 PC2           0.239      0.0742     0.0599         5.99             93.5
#> 3 PC3           0.165      0.0721     0.0413         4.13             97.7
#> 4 PC4           0.0931    NA          0.0233         2.33            100