【问题标题】:R - Limit output of summary.princompR - summary.princomp 的限制输出
【发布时间】:2012-04-07 15:13:37
【问题描述】:

我正在对包含 1000 多个变量的数据集进行主成分分析。我正在使用 R Studio,当我运行摘要以查看组件的累积方差时,我只能看到最后几百个组件。如何将摘要限制为仅显示前 100 个组件?

【问题讨论】:

  • 你能提供一个可重现的小例子吗?
  • @digemall 并非如此,数据集很大。我只是在运行: prin
  • 是的,当然不是完整的代码。我的意思是一个小例子来准确理解你的步骤。无论如何@joran 明白了;)

标签: r princomp


【解决方案1】:

我试过这个,它似乎工作: l = 载荷(prin) l[,1:100]

【讨论】:

    【解决方案2】:

    修改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 作为永久添加;看起来很简单,而且可能很有用。

    【讨论】:

    • 非常感谢。正是我需要的。这使得数据挖掘应用程序变得更加容易。
    • @joran:是的,这是一个值得提交给 R-Core 团队 IMO 的功能。
    【解决方案3】:

    您可以将负载以矩阵形式放置,您可以将矩阵保存到一个变量中,然后将其作为子集(例如matrix[,1:100])以查看第一个/中间/最后一个 n。在这个例子中,我使用了 head()。每列都是一个主成分。

    head(
      matrix(
        prin$loadings, 
          ncol=length(dimnames(prin$loadings)[[2]]),
          nrow=length(dimnames(prin$loadings)[[1]])
      ),
    100)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多