Редактировать выходные данные факторного анализа (R)

Я работаю над проектом, который включает в себя проведение факторного анализа. Вот кусок примера кода:

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

Вывод для этого кода слишком длинный. Я хочу только извлечь нижнюю часть выходных данных о нагрузках с дисперсиями по факторам и проверкой гипотезы после нее.

Если я попытаюсь использовать fit$loadings, я все равно получу слишком много вывода и потеряю проверку гипотезы внизу. Кто-нибудь знает способ вытащить эти конкретные части вывода?

Благодарю вас!


person user2813606    schedule 27.06.2020    source источник
comment
Вам нужно значение pvalue fit$PVAL   -  person akrun    schedule 28.06.2020
comment
Может быть, вы можете проверить здесь   -  person akrun    schedule 28.06.2020


Ответы (2)


Это другой хак, но он выполняет свою работу:

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 
person dcarlson    schedule 27.06.2020

Какой-то ужасный взлом методов печати, но он работает в два этапа...

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
person Chuck P    schedule 27.06.2020