R 如何获取特定组件的变量(PCA)

R 如何获取特定组件的变量(PCA),r,pca,R,Pca,我想从特定组件(comp2)中获取变量: R中是否有任何命令可用于执行此操作?您可以使用prcomp和索引选择特定的PC组件: library(fastDummies) table <- data.frame(NOM = c("Evian", "Montagne des Pyrenees", "Cristaline-St-Cyr", "Fiee des L

我想从特定组件(comp2)中获取变量:


R中是否有任何命令可用于执行此操作?

您可以使用prcomp和索引选择特定的PC组件:

library(fastDummies)
table <- data.frame(NOM = c("Evian", "Montagne des Pyrenees", "Cristaline-St-Cyr", 
                                "Fiee des Lois", "Volcania", "Saint Diery"), 
                        ACRO = c("EVIAN", "MTPYR", "CRIST", "FIEE", "VOLCA", "STDIE"), 
                        PAYS = c("F", "F","F", "F", "F", "F"), 
                        TYPE = c("M", "S", "S", "S", "S", "M"), 
                        PG = c("P", "P", "P", "P", "P", "G"), 
                        CA = c(78, 48, 71, 89, 4.1, 85), 
                        MG = c(24, 11, 5.5, 31, 1.7, 80), 
                        `NA` = c(5,34, 11.2, 17, 2.7, 385), 
                        K = c(1, 1, 3.2, 2, 0.9, 65), 
                        SUL = c(10,16, 5, 47, 1.1, 25), 
                        NO3 = c(3.8, 4, 1, 0, 0.8, 1.9), 
                        HCO3 = c(357,183, 250, 360, 25.8, 1350), 
                        CL = c(4.5, 50, 20, 28, 0.9,285), 
                        MOY = c(60.41, 43.38, 45.86, 71.75, 4.75, 284.61),
                    stringsAsFactors = TRUE)

table_conv <- dummy_cols(table,
                         select_columns = c("NOM","ACRO","PAYS","TYPE","PG"),
                         remove_selected_columns = TRUE)

pca <- prcomp(as.matrix(table_conv))

pca$x[,"PC4"]

您可以使用prcomp和索引选择特定的PC组件:

library(fastDummies)
table <- data.frame(NOM = c("Evian", "Montagne des Pyrenees", "Cristaline-St-Cyr", 
                                "Fiee des Lois", "Volcania", "Saint Diery"), 
                        ACRO = c("EVIAN", "MTPYR", "CRIST", "FIEE", "VOLCA", "STDIE"), 
                        PAYS = c("F", "F","F", "F", "F", "F"), 
                        TYPE = c("M", "S", "S", "S", "S", "M"), 
                        PG = c("P", "P", "P", "P", "P", "G"), 
                        CA = c(78, 48, 71, 89, 4.1, 85), 
                        MG = c(24, 11, 5.5, 31, 1.7, 80), 
                        `NA` = c(5,34, 11.2, 17, 2.7, 385), 
                        K = c(1, 1, 3.2, 2, 0.9, 65), 
                        SUL = c(10,16, 5, 47, 1.1, 25), 
                        NO3 = c(3.8, 4, 1, 0, 0.8, 1.9), 
                        HCO3 = c(357,183, 250, 360, 25.8, 1350), 
                        CL = c(4.5, 50, 20, 28, 0.9,285), 
                        MOY = c(60.41, 43.38, 45.86, 71.75, 4.75, 284.61),
                    stringsAsFactors = TRUE)

table_conv <- dummy_cols(table,
                         select_columns = c("NOM","ACRO","PAYS","TYPE","PG"),
                         remove_selected_columns = TRUE)

pca <- prcomp(as.matrix(table_conv))

pca$x[,"PC4"]

使用
factoextra
,您可以使用
get\u pca\u var
获得所有组件:

library(FactoMineR)
library(factoextra)

pca <- FactoMineR::PCA(iris[,1:4])

pca.var <- factoextra::get_pca_var(pca)

pca.var$coord

                  Dim.1      Dim.2       Dim.3       Dim.4
Sepal.Length  0.8901688 0.36082989 -0.27565767 -0.03760602
Sepal.Width  -0.4601427 0.88271627  0.09361987  0.01777631
Petal.Length  0.9915552 0.02341519  0.05444699  0.11534978
Petal.Width   0.9649790 0.06399985  0.24298265 -0.07535950

使用
factoextra
,您可以使用
get\u pca\u var
获得所有组件:

library(FactoMineR)
library(factoextra)

pca <- FactoMineR::PCA(iris[,1:4])

pca.var <- factoextra::get_pca_var(pca)

pca.var$coord

                  Dim.1      Dim.2       Dim.3       Dim.4
Sepal.Length  0.8901688 0.36082989 -0.27565767 -0.03760602
Sepal.Width  -0.4601427 0.88271627  0.09361987  0.01777631
Petal.Length  0.9915552 0.02341519  0.05444699  0.11534978
Petal.Width   0.9649790 0.06399985  0.24298265 -0.07535950

您正在使用哪个PCA包?@Waldi感谢您的评论,我使用FactoMineR和FactoExtra关于
factorextra::get\u PCA\u var()
您正在使用哪个PCA包?@Waldi感谢您的评论,我使用FactoMineR和FactoExtra关于
factorextra::get\u PCA\u var()
谢谢,但是为什么上面的答案中提到的get_pca_var会给我一个不同的结果mmm,我想这是因为使用了iris数据集。我使用了你之前发布的数据集。是的,我使用了我的数据而不是虹膜,它给了我一个不同的结果,事实上,问题是:第二个分量反对的变量是什么?我做了一个编辑,因为你想要第二个分量的特征向量。请您编辑您的问题,并对照我提出的数据集查看它,好吗?如果没有,我们可以使用经典的Iris数据集。谢谢,但是为什么上面的答案中提到的get_pca_var会给我一个不同的结果mmm,我想这是因为使用了Iris数据集。我使用了你之前发布的数据集。是的,我使用了我的数据而不是虹膜,它给了我一个不同的结果,事实上,问题是:第二个分量反对的变量是什么?我做了一个编辑,因为你想要第二个分量的特征向量。请您编辑您的问题,并对照我提出的数据集查看它,好吗?如果没有,我们可以使用经典的Iris数据集。
pca.var$coord[,"Dim.2"]
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
  0.36082989   0.88271627   0.02341519   0.06399985