Reputation: 1989
I am trying to perform pca with R.
I have the following data matrix:
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
with 15 variables and 5 samples.
I tried the following code (which uses the transpose of my data matrix):
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)
which returns:
> 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
I then tried with the original data matrix (not transposed):
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)
In both cases, I have 5 principal components that explain 100% of the variability. However, since I have 15 variables, shouldn't 100% of variability be explained by 15 variables?
Upvotes: 0
Views: 956
Reputation: 10223
The number of principal components can never exceed the number of samples. Perhaps too simply put, since you only have 5 samples you only need 5 variables to explain the variability.
Upvotes: 2