【问题标题】:Edit Output from Factor Analysis (R)编辑因子分析 (R) 的输出
【发布时间】:2020-10-18 07:03:56
【问题描述】:

我正在从事一个涉及执行因子分析的项目。这是一段示例代码:

fit <- factanal(mtcars, 2, rotation="varimax")
print(fit, digits=4, cutoff=.3, sort=TRUE)

此代码的输出太长了。我只想提取负载输出的底部以及每个因子的方差和之后的假设检验。

如果我尝试 fit$loadings,我仍然会得到太多输出,并且会丢失底部的假设检验。有谁知道如何提取输出的这些特定部分?

谢谢!

【问题讨论】:

  • 你需要pvalue fit$PVAL
  • 可能有,可以查看here

标签: r output dimensionality-reduction factor-analysis


【解决方案1】:

这是一个不同的 hack,但它完成了工作:

capture.output(print(fit, digits=4, cutoff=.3, sort=TRUE), file="temp.txt")
cat(readLines("temp.txt")[23:30], sep="\n")
#                Factor1 Factor2
# SS loadings     4.4938  4.3567
# Proportion Var  0.4085  0.3961
# Cumulative Var  0.4085  0.8046
# 
# Test of the hypothesis that 2 factors are sufficient.
# The chi square statistic is 68.57 on 34 degrees of freedom.
# The p-value is 0.000405 

【讨论】:

    【解决方案2】:

    一种对打印方法的可怕破解,但它分两步工作......

    fit <- factanal(mtcars, 2, rotation="varimax")
    
    yyy <- function (x, digits = 3, ...) 
    {
       if (!is.null(x$STATISTIC)) {
          factors <- x$factors
          cat("\nTest of the hypothesis that", factors, if (factors == 
                                                            1) 
             "factor is"
             else "factors are", "sufficient.\n")
          cat("The chi square statistic is", round(x$STATISTIC, 
                                                   2), "on", x$dof, if (x$dof == 1) 
                                                      "degree"
              else "degrees", "of freedom.\nThe p-value is", signif(x$PVAL, 
                                                                    3), "\n")
       }
       else {
          cat(paste("\nThe degrees of freedom for the model is", 
                    x$dof, "and the fit was", round(x$criteria["objective"], 
                                                    4), "\n"))
       }
       invisible(x)
    }
    
    
    xxx <- function (x, digits = 3L, cutoff = 0.1, sort = FALSE, ...) 
    {
       Lambda <- unclass(x)
       p <- nrow(Lambda)
       factors <- ncol(Lambda)
       if (sort) {
          mx <- max.col(abs(Lambda))
          ind <- cbind(1L:p, mx)
          mx[abs(Lambda[ind]) < 0.5] <- factors + 1
          Lambda <- Lambda[order(mx, 1L:p), ]
       }
    #   cat("\nLoadings:\n")
       fx <- setNames(format(round(Lambda, digits)), NULL)
       nc <- nchar(fx[1L], type = "c")
       fx[abs(Lambda) < cutoff] <- strrep(" ", nc)
    #   print(fx, quote = FALSE, ...)
       vx <- colSums(x^2)
       varex <- rbind(`SS loadings` = vx)
       if (is.null(attr(x, "covariance"))) {
          varex <- rbind(varex, `Proportion Var` = vx/p)
          if (factors > 1) 
             varex <- rbind(varex, `Cumulative Var` = cumsum(vx/p))
       }
       cat("\n")
       print(round(varex, digits))
       invisible(x)
    }
    
    xxx(fit$loadings)
    #> 
    #>                Factor1 Factor2
    #> SS loadings      4.494   4.357
    #> Proportion Var   0.409   0.396
    #> Cumulative Var   0.409   0.805
    yyy(fit)
    #> 
    #> Test of the hypothesis that 2 factors are sufficient.
    #> The chi square statistic is 68.57 on 34 degrees of freedom.
    #> The p-value is 0.000405
    

    【讨论】:

      猜你喜欢
      • 2010-12-21
      • 1970-01-01
      • 2020-09-24
      • 2015-09-05
      • 2019-01-27
      • 1970-01-01
      • 2019-04-13
      • 1970-01-01
      • 2017-04-24
      相关资源
      最近更新 更多