我对包含1000多个变量的数据集运行主成分分析。我使用R Studio,当我运行总结以查看组件的累积方差时,我只能看到最后几百个组件。我如何限制总结仅显示前100个组件?R - summary.princomp的限制输出
回答
这是很容易修改print.summary.princomp
(你可以看到通过键入stats:::print.summary.princomp
原码)来做到这一点:
pcaPrint <- function (x, digits = 3, loadings = x$print.loadings, cutoff = x$cutoff,n, ...)
{
#Check for sensible value of n; default to full output
if (missing(n) || n > length(x$sdev) || n < 1){n <- length(x$sdev)}
vars <- x$sdev^2
vars <- vars/sum(vars)
cat("Importance of components:\n")
print(rbind(`Standard deviation` = x$sdev[1:n], `Proportion of Variance` = vars[1:n],
`Cumulative Proportion` = cumsum(vars)[1:n]))
if (loadings) {
cat("\nLoadings:\n")
cx <- format(round(x$loadings, digits = digits))
cx[abs(x$loadings) < cutoff] <- paste(rep(" ", nchar(cx[1,
1], type = "w")), collapse = "")
print(cx[,1:n], quote = FALSE, ...)
}
invisible(x)
}
pcaPrint(summary(princomp(USArrests, cor=TRUE),
loadings = TRUE, cutoff = 0.2), digits = 2,n = 2)
编辑要包括用于n
一个合理的值基本检查。现在我已经完成了这个任务,不知道是否值得把R Core作为一个永久的补充来提出。看起来很简单,并且可能有用。
非常感谢。正是我需要的。这使得数据挖掘应用程序变得更加容易。 – user1209675 2012-04-07 16:41:43
@joran:是的,这是一个值得提交给R-Core团队IMO的特性。 – digEmAll 2012-04-07 16:45:53
你可以把加载矩阵形式,你可以将矩阵保存到一个变量,然后子集(一个la matrix[,1:100]
)它看到第一个/中间/最后n。在这个例子中,我使用了head()。每列是一个主要组成部分。
head(
matrix(
prin$loadings,
ncol=length(dimnames(prin$loadings)[[2]]),
nrow=length(dimnames(prin$loadings)[[1]])
),
100)
我尝试这样做,它似乎是工作: L =负荷(首席) L [1:100]
- 1. R - 如何限制hmisc rcorr的输出?
- 2. Bash输出限制
- 3. 限制输出列
- 4. 限制grep的输出
- 5. 编织R为PDF时的限制信息输出
- 6. k3_1的输出限制在-3.1445e + 24
- 7. 在R中抑制输出
- 8. 如何限制JPQ输出?
- 9. 限制输出来自Application.Inputbox
- 10. PM2日志输出限制
- 11. for,如何限制输出?
- 12. 限制Argparse帮助输出
- 13. Mysql结果限制输出
- 14. 多个输出要限制
- 15. 限制GDB输出长度
- 16. 数据表 - 限制输出
- 17. lxml/xpath - 限制输出
- 18. 抑制一个命令的输出R
- 19. oozie - 输出数据超出限制[2048]
- 20. 限制二进制输出为8位
- 21. 在bash中输出*的长度限制?
- 22. 限制所有Linux命令的输出
- 23. Group By和限制mySql中的输出
- 24. MySQL的组由min()和限制输出
- 25. 'adb shell dumpsys alarm`的限制输出
- 26. 不同的规定 - 限制输出
- 27. 限制我的输出与阵列
- 28. 在ghci上限制hoogle的输出
- 29. 限制特定列sql的输出hana
- 30. PHP - 限制插件的输出
你能提供一个小的可重复的例子? – digEmAll 2012-04-07 15:30:31
@digemall并非如此,数据集非常庞大。我正在运行:prin < - princomp(train [c(2:1777)])summary(prin)当我这样做时,它显示了所有1776个主要组件的信息。我只需要前100名左右。 – user1209675 2012-04-07 16:07:41
是的,当然不是完整的代码。我的意思是一个小例子来理解你的步骤。无论如何@Joran得到了点;) – digEmAll 2012-04-07 16:44:08