【问题标题】:Confirmatory Factor Analysis in PythonPython中的验证性因子分析
【发布时间】:2019-06-18 05:25:10
【问题描述】:

是否有在 python 中执行验证性因子分析的包?我发现了一些可以在 python 中执行探索性因子分析(scikitlearn、factor_analyzer 等),但我还没有找到可以执行 CFA 的包。

【问题讨论】:

    标签: python scikit-learn statistics factor-analysis dimension-reduction


    【解决方案1】:

    Spyder 中的 python 3.7.3 (Anaconda Navigator)

    factor_analyzer 也做 CFA:

    导入必要的库

    import pandas as pd
    from factor_analyzer import FactorAnalyzer
    

    导入样本数据

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

    验证性因素分析

    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 到 V8 是指数据框中您希望分配给每个因子(F1 和 F2)的列的名称。您需要根据您的数据集和您正在测试的假设,将 V1 到 V8 替换为适当的列名。

    【讨论】:

    • 这在 python 3.7 中似乎不起作用。我已尝试更新 factor_analysisr 以允许这样做,但似乎存在一些不兼容问题。
    • 我在 Spyder 中使用过 python 3.7.3。
    • 我创建了一个新的虚拟环境,这解决了我的问题。感谢您的回答。
    • “factor_analyzer 也做 CFA”——是吗? CFA的目的不就是检验假设吗?这只是拟合模型。
    • CFA 代表验证性因素分析。此行从 factor_analyzer 导入 CFA 函数 from factor_analyzer import (ConfirmatoryFactorAnalyzer, ModelSpecificationParser) 拟合模型后,通过查看拟合指数和因子载荷(取决于您正在测试的内容),您可以调查您的假设。
    【解决方案2】:

    你可以试试包 psy (https://pypi.org/project/psy/)。我找不到它的文档,但我可以阅读用中文编写的 cmets。

    例子:

        import psy
    
        # items is a pandas data frame containing item data 
    
        # Specify how the items are mapped to the factors
        # In this case, all mapped to one factor
        item_factor_mapping = np.array([[1]] * items.shape[1])
    
        print(psy.cfa(items, item_factor_mapping))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-10
      • 2021-10-19
      • 2011-01-19
      • 2020-03-31
      • 1970-01-01
      • 2010-12-21
      • 2013-11-30
      • 2020-09-24
      相关资源
      最近更新 更多