R 语言 因子分析

#因子分析
options(digits=2)
covariances <- ability.cov$cov
#将协方差矩阵转化为相关系数矩阵
correlations <- cov2cor(covariances)
#第一步:判断需提取的公共因子数
library(psych)
covariances <- ability.cov$cov
correlations <- cov2cor(covariances)
fa.parallel(correlations, n.obs=112, fa="both", n.iter=100,
              main="Scree plots with parallel analysis")

fa(correlations, nfactors=2, rotate="none", fm="pa")
#因子旋转(正交旋转:使用正交旋转将人为地强制两个因子不相关)
fa.varimax <- fa(correlations, nfactors=2, rotate="varimax", fm="pa")
fa.varimax
#因子旋转(正交旋转:使用斜交旋转将人为地强制两个因子不相关)
fa.promax <- fa(correlations, nfactors=1, rotate="promax", fm="pa")
fa.promax

#因子结构矩阵
fsm <- function(oblique) {
  if (class(oblique)[2]=="fa" & is.null(oblique$Phi)) {
    warning("Object doesn't look like oblique EFA")
  } else {
    P <- unclass(oblique$loading)
    F <- P %*% oblique$Phi
    colnames(F) <- c("PA1", "PA2")
    return(F)
  }
}
fsm(fa.varimax)
现在你可以看到变量与因子间的相关系数。将它们与正交旋转所得因子载荷阵相比,你会发
现该载荷阵列的噪音比较大,这是因为之前你允许潜在因子相关。虽然斜交方法更为复杂,但模
型将更符合真实数据。
使用factor.plot()或fa.diagram()函数,你可以绘制正交或者斜交结果的图形。来看

以下代码:

factor.plot(fa.promax, labels=rownames(fa.promax$loadings))
相比PCA,EFA并不那么关注计算因子得分。在fa()函数中添加score = TRUE选项(原始
数据可得时)便可很轻松地获得因子得分。另外还可以得到得分系数(标准化的回归权重),它
在返回对象的weights元素中。
对于ability.cov数据集,通过二因子斜交旋转法便可获得用来计算因子得分的权重:
fa.promax$weights
[,1] [,2]
general 0.080 0.210
picture 0.021 0.090
blocks 0.044 0.695
maze 0.027 0.035
reading 0.739 0.044
vocab 0.176 0.039
与可精确计算的主成分得分不同,因子得分只是估计得到的。它的估计方法有多种,fa()

函数使用的是回归方法


猜你喜欢

转载自blog.csdn.net/zpp15603669517/article/details/79806615