我相信你是追求的scores
。根据 for 的帮助?princomp
,生成的对象是一个包含元素的列表scores
:
分数
如果 score = TRUE,则提供的数据在主成分上的得分。仅当提供了 x 并且 covmat 是协方差列表时也提供了,这些才是非空的。对于公式方法,napredict() 用于处理 na.action 省略的值的处理。
让我们设置一个示例(基于示例中的示例?princomp
):
summary(pc.cr <- princomp(USArrests, cor = TRUE))
Importance of components:
Comp.1 Comp.2 Comp.3 Comp.4
Standard deviation 1.5748783 0.9948694 0.5971291 0.41644938
Proportion of Variance 0.6200604 0.2474413 0.0891408 0.04335752
Cumulative Proportion 0.6200604 0.8675017 0.9566425 1.00000000
您可以使用以下方法调查结果对象str()
:
str(pc.cr)
List of 7
$ sdev : Named num [1:4] 1.575 0.995 0.597 0.416
..- attr(*, "names")= chr [1:4] "Comp.1" "Comp.2" "Comp.3" "Comp.4"
$ loadings: loadings [1:4, 1:4] -0.536 -0.583 -0.278 -0.543 0.418 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
.. ..$ : chr [1:4] "Comp.1" "Comp.2" "Comp.3" "Comp.4"
$ center : Named num [1:4] 7.79 170.76 65.54 21.23
..- attr(*, "names")= chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
$ scale : Named num [1:4] 4.31 82.5 14.33 9.27
..- attr(*, "names")= chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
$ n.obs : int 50
$ scores : num [1:50, 1:4] -0.986 -1.95 -1.763 0.141 -2.524 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ...
.. ..$ : chr [1:4] "Comp.1" "Comp.2" "Comp.3" "Comp.4"
$ call : language princomp(x = USArrests, cor = TRUE)
- attr(*, "class")= chr "princomp"
现在提取分数:
head(pc.cr$scores)
Comp.1 Comp.2 Comp.3 Comp.4
Alabama -0.9855659 1.1333924 -0.44426879 0.156267145
Alaska -1.9501378 1.0732133 2.04000333 -0.438583440
Arizona -1.7631635 -0.7459568 0.05478082 -0.834652924
Arkansas 0.1414203 1.1197968 0.11457369 -0.182810896
California -2.5239801 -1.5429340 0.59855680 -0.341996478
Colorado -1.5145629 -0.9875551 1.09500699 0.001464887