4

I'm trying to preform PCA (principal component analysis) using TidyModels. I have created a recipe but I don't know how can I change the default rotation used in `step_pca() method (such as changing it to say Varimax rotation). any ideas?

this is my recipe:

pembqol_rec <- recipe(~., data = df) %>% 
  update_role(id, name, new_role = "id") %>%
  step_naomit(all_predictors()) %>% 
  step_normalize(all_predictors()) %>% 
  step_pca(all_predictors(), id = "pca") %>% 
  prep()

1 Answers1

2

The step_pca() function uses stats::prcomp() under the hood, which I don't believe supports that, but you can get out the loadings using tidy() and the type = "coef" argument and then apply a rotation yourself. See this Cross Validated answer for more info.

library(recipes)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> 
#> Attaching package: 'recipes'
#> The following object is masked from 'package:stats':
#> 
#>     step

rec <- recipe( ~ ., data = USArrests) %>%
  step_normalize(all_numeric()) %>%
  step_pca(all_numeric(), num_comp = 3)

prep(rec) %>% tidy(number = 2, type = "coef")
#> # A tibble: 16 × 4
#>    terms      value component id       
#>    <chr>      <dbl> <chr>     <chr>    
#>  1 Murder   -0.536  PC1       pca_L9R48
#>  2 Assault  -0.583  PC1       pca_L9R48
#>  3 UrbanPop -0.278  PC1       pca_L9R48
#>  4 Rape     -0.543  PC1       pca_L9R48
#>  5 Murder    0.418  PC2       pca_L9R48
#>  6 Assault   0.188  PC2       pca_L9R48
#>  7 UrbanPop -0.873  PC2       pca_L9R48
#>  8 Rape     -0.167  PC2       pca_L9R48
#>  9 Murder   -0.341  PC3       pca_L9R48
#> 10 Assault  -0.268  PC3       pca_L9R48
#> 11 UrbanPop -0.378  PC3       pca_L9R48
#> 12 Rape      0.818  PC3       pca_L9R48
#> 13 Murder    0.649  PC4       pca_L9R48
#> 14 Assault  -0.743  PC4       pca_L9R48
#> 15 UrbanPop  0.134  PC4       pca_L9R48
#> 16 Rape      0.0890 PC4       pca_L9R48

Created on 2022-01-10 by the reprex package (v2.0.1)

Julia Silge
  • 10,848
  • 2
  • 40
  • 48