PCA,TidyModels;如何在 step_pca 中更改旋转方法?
PCA, TidyModels; how to change rotation method in step_pca?
我正在尝试使用 TidyModels 执行 PCA(主成分分析)。我已经创建了一个配方,但我不知道如何更改 `step_pca() 方法中使用的默认旋转(例如将其更改为 Varimax 旋转)。
有什么想法吗?
这是我的食谱:
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()
step_pca()
函数在幕后使用 stats::prcomp()
,我认为这不支持,但您可以查看 loadings using tidy()
and the type = "coef"
argument and then apply a rotation yourself. See this Cross Validated answer 了解更多信息。
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
由 reprex package (v2.0.1)
创建于 2022-01-10
我正在尝试使用 TidyModels 执行 PCA(主成分分析)。我已经创建了一个配方,但我不知道如何更改 `step_pca() 方法中使用的默认旋转(例如将其更改为 Varimax 旋转)。 有什么想法吗?
这是我的食谱:
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()
step_pca()
函数在幕后使用 stats::prcomp()
,我认为这不支持,但您可以查看 loadings using tidy()
and the type = "coef"
argument and then apply a rotation yourself. See this Cross Validated answer 了解更多信息。
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
由 reprex package (v2.0.1)
创建于 2022-01-10