R第一主成分

时间:2015-05-28 13:50:43

标签: r static rstudio pca

我在道琼斯的10个股票数据上做了一个PCA,现在我试图通过使用我的PCA的第一个主要成分从股票数据中提取“股票指数”因素,但我不知道怎么做。

library(FactoMineR);
str <- "Exxon Mobil;Intel;McDonald's;Microsoft;Nike;Procter And Gamble;The Travelers Companies;Verizon Communications;Visa;Wal-Mart Stores
84,46;30,81;96,29;40,72;99,55;82,32;107,11;48,92;65,18;80,71
85;31,27;97,44;40,66;100,33;81,94;108,13;48,63;65,41;82,25
85,63;31,46;97,88;40,96;100,89;82,72;109,64;49,12;65,66;82,53
83,58;32;96,96;40,97;99,88;82,31;107,13;48,56;65,54;81,35
84,32;30,08;97,64;41,21;99,33;82,15;106,83;48,42;65,59;81,89
84,86;29,89;98,14;41,46;98,99;83,01;107,61;48,73;65,73;81,32
84,52;30,79;99,36;42,9;100,65;83,92;109,23;49,41;67,1;83,05
85,43;31,2;98,62;42,86;101,46;84,86;109,62;49,64;67,08;83,31
84,54;31,31;97,05;42,88;101,98;84,74;109,73;49,56;67,41;83,24
84,41;30,74;95,98;42,29;98,32;83,38;109,11;49,3;66,81;81,52
86,07;30,89;97;42,5;97,51;83,75;109,52;49,54;267,67001;82,53
84,08;30,59;96,17;41,7;96,54;82,85;108,75;48,95;264,5;82,62
84,76;30,83;97,15;41,56;96,44;83,56;108,93;49,27;269,01999;83,29"

Actions <- read.table(text=str, dec="," , header=TRUE, sep=";")

Actions.PCA<-PCA(Actions)
summary(Actions.PCA)

1 个答案:

答案 0 :(得分:3)

不知道如何使用FactoMineR包来做,但我知道如何使用内置的R函数prcomp来完成它。

解析数据

str <- "Exxon Mobil;Intel;McDonalds;Microsoft;Nike;Procter And Gamble;The Travelers Companies;Verizon Communications;Visa;Wal-Mart Stores
84,46;30,81;96,29;40,72;99,55;82,32;107,11;48,92;65,18;80,71
85;31,27;97,44;40,66;100,33;81,94;108,13;48,63;65,41;82,25
85,63;31,46;97,88;40,96;100,89;82,72;109,64;49,12;65,66;82,53
83,58;32;96,96;40,97;99,88;82,31;107,13;48,56;65,54;81,35
84,32;30,08;97,64;41,21;99,33;82,15;106,83;48,42;65,59;81,89
84,86;29,89;98,14;41,46;98,99;83,01;107,61;48,73;65,73;81,32
84,52;30,79;99,36;42,9;100,65;83,92;109,23;49,41;67,1;83,05
85,43;31,2;98,62;42,86;101,46;84,86;109,62;49,64;67,08;83,31
84,54;31,31;97,05;42,88;101,98;84,74;109,73;49,56;67,41;83,24
84,41;30,74;95,98;42,29;98,32;83,38;109,11;49,3;66,81;81,52
86,07;30,89;97;42,5;97,51;83,75;109,52;49,54;267,67001;82,53
84,08;30,59;96,17;41,7;96,54;82,85;108,75;48,95;264,5;82,62
84,76;30,83;97,15;41,56;96,44;83,56;108,93;49,27;269,01999;83,29"

Actions <- read.table(str, header=TRUE, dec=",", sep=";")

制作PCA

pca <- prcomp(Actions)

获取第一个组件

pca$x[,1]

更新

我认为真正的问题是你的文件使用逗号作为小数分隔符而不是点。首先将其作为文本读取,而不将其解析为数据框(如read.csv2所做)。然后转换逗号,然后运行PCA。

Actions <- read.table("actions.csv", header=TRUE, dec=",", sep=";")
pca <- prcomp(Actions)