gpt4 book ai didi

r - 如何在 R 中使用 prcomp 将属性数量减少到 20 个?

转载 作者:行者123 更新时间:2023-12-04 02:22:48 26 4
gpt4 key购买 nike

我有一个名为 data 的 104 个属性数据集.我想使用 prcomp 将属性数量减少到 20 个R 中的函数。

我这样做了:

pr = prcomp(data)

但是 pr仅包含 prcomp 的实例类(class)。如何将原始数据集中的属性数量减少到 20 个?

最佳答案

首先,prcomp 做主成分分析。主成分分析会生成与变量一样多的成分。您正在寻找的是因子分析:

ff <- factanal(data,20)

?factanal
如果您只想保留前 20 个主成分作为新数据集,您可以轻松地从 predict() 函数中选择它们。甚至自己计算它们:
x <- prcomp(USArrests, scale = TRUE)

tt <- predict(x) # the standard way

# below the matrix way
tt2 <- scale(USArrests,x$center,x$scale) %*% x$rotation

# with only 3 components instead of 4
tt3 <- predict(x)[,1:3]
tt4 <- scale(USArrests,x$center,x$scale) %*% x$rotation[,1:3]

但请注意,将数据集减少到 20 个因子的因子分析与保留 PCA 的前 20 个主成分不同。

关于r - 如何在 R 中使用 prcomp 将属性数量减少到 20 个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3905958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com