我正在尝试用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个变量,不应该用15个变量来解释100%的变异性吗?
答案 0 :(得分:2)
主要组件的数量永远不会超过样本数量。也许太简单了,因为你只有5个样本,你只需要5个变量来解释变异性。