Powered by prcomp. When creating the nested_data, the data should be scaled (i.e, trans = scale) if all variables are not in the same unit.

nested_prcomp(.data, data_column = .data$data, ...)

Arguments

.data

A data frame with a list column of data frames, possibly created using nested_data.

data_column

An expression that evalulates to the data object within each row of .data

...

Passed to prcomp.

Value

.data with additional columns 'model', 'loadings', 'variance' and 'scores'

Examples

library(dplyr, warn.conflicts = FALSE)

nested_pca <- alta_lake_geochem %>%
  nested_data(
    qualifiers = c(depth, zone),
    key = param,
    value = value,
    trans = scale
  ) %>%
  nested_prcomp()

# get variance info
nested_pca %>% unnested_data(variance)
#> # A tibble: 6 × 6
#>   component component_text standard_deviation variance variance_propor…¹ varia…²
#>       <int> <chr>                       <dbl>    <dbl>             <dbl>   <dbl>
#> 1         1 PC1                         2.15    4.61             0.768     0.768
#> 2         2 PC2                         0.884   0.781            0.130     0.899
#> 3         3 PC3                         0.603   0.364            0.0607    0.959
#> 4         4 PC4                         0.381   0.145            0.0242    0.984
#> 5         5 PC5                         0.276   0.0761           0.0127    0.996
#> 6         6 PC6                         0.151   0.0228           0.00380   1    
#> # … with abbreviated variable names ¹​variance_proportion,
#> #   ²​variance_proportion_cumulative

# get loadings info
nested_pca %>% unnested_data(loadings)
#> # A tibble: 6 × 7
#>   variable    PC1     PC2    PC3    PC4     PC5     PC6
#>   <chr>     <dbl>   <dbl>  <dbl>  <dbl>   <dbl>   <dbl>
#> 1 C        -0.380 -0.540  -0.460  0.162 -0.567   0.0718
#> 2 C/N       0.401 -0.451   0.207 -0.735 -0.223  -0.0408
#> 3 Cu       -0.387 -0.340   0.760  0.173  0.0609  0.352 
#> 4 Ti        0.439  0.0783  0.356  0.483 -0.565  -0.350 
#> 5 d13C      0.458  0.0890 -0.144  0.145 -0.126   0.851 
#> 6 d15N     -0.377  0.613   0.144 -0.386 -0.539   0.149 

# scores, requalified
nested_pca %>% unnested_data(c(qualifiers, scores))
#> # A tibble: 32 × 9
#>    depth zone   row_number    PC1      PC2    PC3     PC4     PC5     PC6
#>    <dbl> <chr>       <int>  <dbl>    <dbl>  <dbl>   <dbl>   <dbl>   <dbl>
#>  1  0.25 Zone 3          1 -3.41   1.05    -0.517 -1.14   -0.261  -0.363 
#>  2  0.75 Zone 3          2 -3.71  -0.00229 -0.280 -0.954   0.156   0.0447
#>  3  1.25 Zone 3          3 -3.75  -0.980    0.833  0.0304  0.187   0.0475
#>  4  1.75 Zone 3          4 -3.89  -0.925    1.12   0.0765 -0.0739  0.202 
#>  5  2.5  Zone 3          5 -3.46  -1.19     0.967  0.270  -0.0480  0.0348
#>  6  3.5  Zone 3          6 -2.88  -0.835    0.394  0.151   0.0405 -0.179 
#>  7  4.5  Zone 2          7 -1.49  -0.293   -0.144  0.248  -0.0819 -0.0934
#>  8  5.5  Zone 2          8 -0.223  0.0818  -0.218  0.575   0.342  -0.237 
#>  9  6.5  Zone 2          9 -0.331  0.573   -0.181  0.366   0.354  -0.0660
#> 10  7.5  Zone 2         10 -0.638  0.543   -0.904  0.145   0.239   0.150 
#> # … with 22 more rows