繁体   English   中英

从因子分析的负载中获取数据框(心理中的 fa 函数)

[英]Getting data frame from loadings of factor analysis (fa function in psych)

我有一个数据框,如下所示:

x <- c(1, 2, 1, 2)
y <- c(1, 2, 3, 4)
z <- c(4, 3, 2, 1)
df <- data.frame(x, y, z)

我正在使用psych包中的fa函数进行因子分析:

fit <- fa(df, nfactors = 2)
fit$loadings

这将产生以下输出:

Loadings:
  MR1    MR2   
x  0.448       
y  0.999       
z -0.999       

                 MR1   MR2
SS loadings    2.195 0.000
Proportion Var 0.732 0.000
Cumulative Var 0.732 0.732

我想将带有 MR1 和 MR2 的表保存为数据框。 有谁知道如何做到这一点? 谢谢你。

x <- c(1, 2, 1, 2)
y <- c(1, 2, 3, 4)
z <- c(4, 3, 2, 1)
df <- data.frame(x, y, z)

fit <- psych::fa(df, nfactors = 2)

x <- fit$loadings

通过stats:::print.loadings

Lambda <- unclass(x)
p <- nrow(Lambda)
factors <- ncol(Lambda)

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))
}

tibble::rownames_to_column(as.data.frame(varex), "x")
##                x       MR1          MR2
## 1    SS loadings 2.1954555 3.000000e-30
## 2 Proportion Var 0.7318185 1.000000e-30
## 3 Cumulative Var 0.7318185 7.318185e-01

而且,对于第一个表:

cutoff <- 0.1 # (the default for the `print.loadings()` function)
Lambda <- unclass(x)
p <- nrow(Lambda)
fx <- setNames(Lambda, NULL)
fx[abs(Lambda) < cutoff] <- NA_real_
fx <- as.data.frame(fx)
rownames(fx) <- NULL
fx
##          MR1 MR2
## 1  0.4476761  NA
## 2  0.9987596  NA
## 3 -0.9987596  NA

我相信简短的回答应该是:

as.data.frame(unclass(fit$loadings))

给予:

         MR1         MR2
x  0.4502976  0.10314193
y  0.9984784 -0.02325767
z -0.9984784  0.02325767

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM