Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirmatory Factor Analysis in Python

Is there a package to perform Confirmatory Factor Analysis in python? I have found a few that can perform Exploratory Factor Analysis in python (scikitlearn, factor_analyzer etc), but I am yet to find a package that does CFA .

like image 587
johnnyw Avatar asked Jun 09 '26 20:06

johnnyw


1 Answers

python 3.7.3 in Spyder (Anaconda Navigator)

factor_analyzer does CFA as well:

import necessary libraries

import pandas as pd
from factor_analyzer import FactorAnalyzer

importing sample data

df= pd.read_csv("test.csv")

Confirmatory factor analysis

from factor_analyzer import (ConfirmatoryFactorAnalyzer, ModelSpecificationParser)    

model_dict = {"F1": ["V1", "V2", "V3", "V4"], "F2": ["V5", "V6", "V7", "V8"]}

model_spec = ModelSpecificationParser.parse_model_specification_from_dict(df, model_dict)

cfa = ConfirmatoryFactorAnalyzer(model_spec, disp=False) 

cfa.fit(df.values) 

cfa.loadings_ 
  • V1 to V8 refer to the name of columns in your data frame that you'd like to allocate to each factor (F1 and F2). You need to replace V1 to V8 with appropriate column names based on your data set and hypothesis you're testing.
like image 158
Azadeh Feizpour Avatar answered Jun 11 '26 09:06

Azadeh Feizpour