编辑因子分析 (R) 的输出
Edit Output from Factor Analysis (R)
我正在从事一个涉及执行因素分析的项目。这是一段示例代码:
fit <- factanal(mtcars, 2, rotation="varimax")
print(fit, digits=4, cutoff=.3, sort=TRUE)
这段代码的输出太长了。我只想提取负载输出的底部部分,其中包含每个因子的方差和之后的假设检验。
如果我尝试 fit$loadings,我仍然会得到太多的输出并且我在底部失去了假设检验。有谁知道拉出输出的这些特定部分的方法吗?
谢谢!
打印方法的一种可怕的黑客攻击,但它分两步工作...
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
这是一个不同的 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
我正在从事一个涉及执行因素分析的项目。这是一段示例代码:
fit <- factanal(mtcars, 2, rotation="varimax")
print(fit, digits=4, cutoff=.3, sort=TRUE)
这段代码的输出太长了。我只想提取负载输出的底部部分,其中包含每个因子的方差和之后的假设检验。
如果我尝试 fit$loadings,我仍然会得到太多的输出并且我在底部失去了假设检验。有谁知道拉出输出的这些特定部分的方法吗?
谢谢!
打印方法的一种可怕的黑客攻击,但它分两步工作...
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
这是一个不同的 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