我對包含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. 限制輸出列
- 3. R編程brew vs. Sweave--抑制輸出和brew限制
- 4. 限制grep的輸出
- 5. 編織R爲PDF時的限制信息輸出
- 6. k3_1的輸出限制在-3.1445e + 24
- 7. 在R中抑制輸出
- 8. 數據表 - 限制輸出
- 9. PM2日誌輸出限制
- 10. lxml/xpath - 限制輸出
- 11. 限制輸出來自Application.Inputbox
- 12. 如何限制JPQ輸出?
- 13. 限制Argparse幫助輸出
- 14. 抑制一個命令的輸出R
- 15. oozie - 輸出數據超出限制[2048]
- 16. 限制Solr中MoreLikeThis的輸出
- 17. 限制所有Linux命令的輸出
- 18. 'adb shell dumpsys alarm`的限制輸出
- 19. 是否有輸出的字符限制
- 20. js-grid輸出高度限制問題 - 無法控制輸出的溢出
- 21. 編程中的R - 輸出R中控制檯比腳本
- 22. 限制foreach輸出4結果
- 23. Concrete5分頁 - 限制輸出頁數
- 24. Python - 熊貓輸出限制列
- 25. Webpack:限制輸出行長度
- 26. 如何在NSLog中限制NSArray輸出
- 27. 批量失敗輸出長度限制
- 28. 限制Python輸出文件大小
- 29. 如何限制SPSS輸出小數?
- 30. codeigniter數據庫如何限制輸出
你能提供一個小的可重複的例子? – 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