Psychology. Confirmatory Factor Analysis (CFA) for Bifactor Model

Confirmatory Factor Analysis (CFA) for Bifactor Model

The following Python code demonstrates how to perform a confirmatory factor analysis (CFA) for a bifactor model with orthogonal factors using the semopy library. The model includes two specific latent factors: Optimism and Pessimism, along with a general latent factor, Dispositional. The dataset is expected to be in CSV format with 521 observations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Import necessary libraries
import pandas as pd
from semopy import Model, ModelMeans, fit, calc_stats

# Load the data (replace 'data.csv' with the actual path to your dataset)
data = pd.read_csv("data.csv")

# Define the bifactor model with orthogonal factors
# Using lavaan-style syntax
descriptor = """
# General latent factor
Dispositional =~ Q1 + Q4 + Q10 + Q3 + Q7 + Q9 + Q11 + Q12

# Specific latent factors
Optimism =~ Q1 + Q4 + Q10 + Q11
Pessimism =~ Q3 + Q7 + Q9 + Q12

# Orthogonality constraints
Optimism ~~ 0*Pessimism
Optimism ~~ 0*Dispositional
Pessimism ~~ 0*Dispositional
"""

# Create and fit the model
model = Model(descriptor)
res = model.fit(data)

# Calculate fit statistics
stats = calc_stats(model, data)

# Print results
print("Fit statistics:")
print(stats)

# Display factor loadings
print("\nFactor Loadings:")
print(model.inspect("mx"))

# Save results to CSV
results_df = pd.DataFrame(stats, index=[0])
results_df.to_csv("fit_statistics.csv", index=False)

Explanation

  1. Library: The semopy library is used for structural equation modeling in Python.
    1. Read paper semopy: A Python package for Structural Equation Modeling.G. Meshcheryakov, A. Igolkina. 2019. on arxiv.org
    2. Read paper semopy 2: A Structural Equation Modeling Package with Random Effects in Python. 2021. on arxiv.org
  2. Data Input: Replace data.csv with the path to your CSV file.
  3. Model Syntax: The descriptor variable defines the bifactor model, including general and specific factors. Orthogonality constraints are included.
  4. Fit Statistics: Fit indices such as χ², df, CFI, TLI, RMSEA, and SRMR are calculated and printed.

Relevant Research Articles

Russian

  1. Быстров В.П. - “Математическая статистика: теория и применение”
  2. Морозов М.В. - “Основы факторного анализа”

English

  1. Raykov & Marcoulides - “Introduction to Psychometric Theory”
  2. Keith F. Widaman - “Factor Analysis in Social Research”