【发布时间】:2014-06-26 21:59:29
【问题描述】:
我正在尝试使用 R 执行 pca。
我有以下数据矩阵:
V2 V3 V4 V5 V6
2430 0 168 290 45 1715
552928 188 94 105 60 3374
55267 0 0 465 0 3040
27787 0 0 0 0 3380
938270 0 56 56 0 2039
249165 0 0 332 0 2548
31009 0 0 0 0 2690
314986 0 0 0 0 2897
5001 0 0 0 0 3453
28915 0 262 175 0 2452
5261 0 0 351 0 3114
74412 0 109 54 0 2565
16007 0 0 407 0 1730
6614 0 71 179 0 2403
419 0 0 0 0 2825
有 15 个变量和 5 个样本。
我尝试了以下代码(它使用了我的数据矩阵的转置):
fit <- prcomp(t(dt))
summary(fit) # print variance accounted for
loadings(fit) # pc loadings
plot(fit,type="lines") # scree plot
fit$scores # the principal components
biplot(fit)
返回:
> summary(fit) # print variance accounted for
Importance of components:
PC1 PC2 PC3 PC4 PC5
Standard deviation 4651.1348 298.09026 126.79032 41.03270 3.474e-13
Proportion of Variance 0.9951 0.00409 0.00074 0.00008 0.000e+00
Cumulative Proportion 0.9951 0.99918 0.99992 1.00000 1.000e+00
loadings(fit) # pc loadings
NULL
> plot(fit,type="lines") # scree plot
> fit$scores # the principal components
NULL
然后我尝试使用原始数据矩阵(未转置):
fit <- prcomp(dt)
summary(fit) # print variance accounted for
loadings(fit) # pc loadings
plot(fit,type="lines") # scree plot
fit$scores # the principal components
biplot(fit)
Importance of components:
PC1 PC2 PC3 PC4 PC5
Standard deviation 562.2600 156.13452 75.59006 43.63721 9.21936
Proportion of Variance 0.9079 0.07001 0.01641 0.00547 0.00024
Cumulative Proportion 0.9079 0.97788 0.99429 0.99976 1.00000
> loadings(fit) # pc loadings
NULL
> plot(fit,type="lines") # scree plot
> fit$scores # the principal components
NULL
> biplot(fit)
在这两种情况下,我都有 5 个主成分来解释 100% 的变异性。但是,既然我有 15 个变量,难道 100% 的可变性不应该用 15 个变量来解释吗?
【问题讨论】:
-
不,如果您在转置矩阵的每一列中只有 5 个观察值,那么只有 5 列就足以表达所有其他的。您只能拥有与线性独立列一样多的主成分。
-
我建议您在阅读有关该方法的基础知识时进行一些先前的努力。您会在cran 找到很多参考资料。一本关于多元方法的好书是Everit and Hothorn。
-
@PauloCardoso 感谢您的参考
标签: r machine-learning data-mining pca