鉴于我的输入3D数据位于文件“myFile.txt”中,按
组织435 43 23
234 23 453
212 45 2345
...
我想在它上面执行PCA,只提取第一个 两个主要部分。什么是最简单的方法 为达到这个?我有R和mdp处理,但我不是 确定我需要执行的命令集 使任何这些有用。
我会欣赏建设性意见,而不是无知 downvote。毕竟,目的是帮助我找到解决方案......
答案 0 :(得分:3)
在基础R中,推荐的函数是prcomp()
。阅读其帮助文件?prcomp
。
一个例子是:
mod <- prcomp(USArrests, scale = TRUE)
mod
是类"prcomp"
的对象,特征向量矩阵(主要组件,原始/未缩放)在组件x
> str(mod)
List of 5
$ sdev : num [1:4] 1.575 0.995 0.597 0.416
$ rotation: num [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] "PC1" "PC2" "PC3" "PC4"
$ 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.36 83.34 14.47 9.37
..- attr(*, "names")= chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
$ x : num [1:50, 1:4] -0.976 -1.931 -1.745 0.14 -2.499 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ...
.. ..$ : chr [1:4] "PC1" "PC2" "PC3" "PC4"
- attr(*, "class")= chr "prcomp"
查看x
组件:
> head(mod$x)
PC1 PC2 PC3 PC4
Alabama -0.9756604 1.1220012 -0.43980366 0.154696581
Alaska -1.9305379 1.0624269 2.01950027 -0.434175454
Arizona -1.7454429 -0.7384595 0.05423025 -0.826264240
Arkansas 0.1399989 1.1085423 0.11342217 -0.180973554
California -2.4986128 -1.5274267 0.59254100 -0.338559240
Colorado -1.4993407 -0.9776297 1.08400162 0.001450164
提取组件1和2
> scrs <- mod$x[, 1:2]
> head(scrs)
PC1 PC2
Alabama -0.9756604 1.1220012
Alaska -1.9305379 1.0624269
Arizona -1.7454429 -0.7384595
Arkansas 0.1399989 1.1085423
California -2.4986128 -1.5274267
Colorado -1.4993407 -0.9776297
然后你可以绘制它们等等:
plot(scrs, asp = 1) ## asp = 1 gives equal scaling to x and y axes