'Factor Analysis using Python Factor_Analyzer
I am trying to perform factor analysis by using factor analyzer module by using the below codes:
for bartlett_sphericity
from factor_analyzer.factor_analyzer import calculate_bartlett_sphericity
chi_sqaure_value, p_value = calculate_bartlett_sphericity(fac)
chi_sqaure_value, p_value
for KMO
from factor_analyzer.factor_analyzer import calculate_kmo
kmo_all, kmo_model = calculate_kmo(fac)
kmo_model
after running the above codes, I got (inf,nan)
from bartlett_sphericity
and nan from KMO
. please advise how to fix this issue.
Solution 1:[1]
I literally just had this issue. It turned out that there were two problems that caused it. The first one was that the correlation matrix had nan's in it. This was because I had several columns that were all zeros, after I removed those, I still had the problem. It turned out that I had two columns that were perfectly correlated, i.e. had a correlation of 1 or -1. After I removed one of these (you don't need to remove BOTH perfectly correlated columns), both of the above worked, and I was trucking along with me FA.
Solution 2:[2]
The KMO and bartlett_sphericity won't work with NaN values in your dataset. In case you removed outliers from your data set, check if the ordinal columns (0 or 1) still have data. Sometimes, like in my case, they were all turned to NaN thus causing the error. As well you can drop the columns/rows with NaN data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kenney |
Solution 2 | Brian Onyango |