R函数princomp失败但prcomp没有

时间:2018-03-27 18:58:16

标签: r function pca

当样本数小于变量时,

princomp失败,但prcomp没有。为什么呢?

Browse[1]> expr.mtx
      samp1 samp2 samp3 samp4 samp5
gene1     6    11    17    11    15
gene2    11    13     9    12     9
gene3    16     7     9    12     6
gene4     3     8    11     8    11
gene5     9     7    12     2    10
gene6     6     6     6     9    10
Browse[1]> princomp(t(expr.mtx))
Error in princomp.default(t(expr.mtx)) : 
  'princomp' can only be used with more units than variables
Browse[1]> prcomp(t(expr.mtx))
Standard deviations (1, .., p=5):
[1] 6.654047e+00 3.835829e+00 2.248479e+00 1.629236e+00 1.449188e-15

Rotation (n x k) = (6 x 5):
              PC1         PC2        PC3        PC4         PC5
gene1 -0.61577807 -0.01946833 -0.3858766  0.4130233 -0.44054693
gene2  0.16053156 -0.25542247  0.4062550  0.3157601 -0.63612151
gene3  0.53418947  0.28885564 -0.6968411  0.2623834 -0.09595675
gene4 -0.47430984 -0.18634512 -0.1897275  0.1319995  0.43994363
gene5 -0.27848488  0.85552275  0.1837702 -0.2181367 -0.20582437
gene6 -0.08464602 -0.29034305 -0.3615255 -0.7719163 -0.39515321
Browse[1]>

MWE:

set.seed(171)
expr.mtx <- matrix(
  data = rpois(n = 30, lambda = 10),
  ncol = 5,
  dimnames = list(paste0("gene", 1:6), paste0("samp", 1:5))
)
expr.mtx
princomp(t(expr.mtx))
prcomp(t(expr.mtx))

0 个答案:

没有答案