因子分析R - 不同的结果?

时间:2017-10-31 05:03:41

标签: r psych factor-analysis

我使用psych包对R中的数据集进行因子分析。直到大约1个月前,它已吐出相同的产量,但最近,它有所不同。

当我尝试在旧版本的mental包上运行它时,它也会产生不同的输出。我无法诊断这个问题,并试图获得原始输出。我不知道它是如何成为编码问题的,因为结果是在过去产生的 - 我现在只是努力获得相同的输出..

以下是代码的精简版。

下载心理套餐:

# check if package "psych" is installed. if not, remind the user to install
if(("psych" %in% rownames(installed.packages())) == FALSE){
  stop("Please install package 'psych' by running 'install.package('psych')'")
}
library(psych)  # this package is needed for factor analysis

运行FA:

n_factor=3


# the variables defined below are used to record the iterative process
LIST_min_in_max_loading_vector <- NULL
LIST_drop_variable <- NULL

min_in_max_loading_vector=0
flag=1
while(TRUE){
  cat("The ",flag," Step is done. \n")
  fa_result<- fa(dat,nfactors=n_factor,rotate = "varimax", cor='poly')
  max_loading_in_each_row <- sapply(1:dim(fa_result$loadings)[1],function(j) max(abs(fa_result$loadings[j,])))
  variable_names=row.names(fa_result$loadings)

  min_in_max_loading_vector <- min(max_loading_in_each_row)

  # Please note that here we have a cut-off value 0.5. 
  # This means that the minimum of the absolute values of all the loadings must be bigger than 0.5
  # it's also the stop condition of our iterative algorithm
  if(min_in_max_loading_vector>0.5){
    break
  }
  min_variable <- variable_names[which(max_loading_in_each_row==min_in_max_loading_vector)]

  cat("The minimum of the maximum absolute loadings is:",min_in_max_loading_vector,"\n")
  drop_index <- which(row.names(fa_result$loadings)==min_variable) 
  cat(min_variable," is droped in this round.\n\n")
  dat <- dat[,-drop_index]

  #record the process of dropping
  LIST_min_in_max_loading_vector[flag]=min_in_max_loading_vector
  LIST_drop_variable[flag] <- min_variable

  print(fa_result$loadings)

  flag=flag+1
}

任何人都可以解决这个问题吗?

0 个答案:

没有答案