factor-analyzer

A Python module to perform exploratory & confirmatory factor analyses.

https://github.com/educationaltestingservice/factor_analyzer

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.3%) to scientific vocabulary

Keywords

cfa efa factor-analysis python
Last synced: 6 months ago · JSON representation

Repository

A Python module to perform exploratory & confirmatory factor analyses.

Basic Info
  • Host: GitHub
  • Owner: EducationalTestingService
  • License: gpl-2.0
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 4.89 MB
Statistics
  • Stars: 258
  • Watchers: 18
  • Forks: 70
  • Open Issues: 16
  • Releases: 7
Topics
cfa efa factor-analysis python
Created about 8 years ago · Last pushed about 1 year ago
Metadata Files
Readme License

README.rst

FactorAnalyzer
--------------

.. image:: https://gitlab.com/EducationalTestingService/factor_analyzer/badges/main/pipeline.svg
   :target: https://gitlab.com/EducationalTestingService/factor_analyzer/-/pipelines
   :alt: Build status

.. image:: https://codecov.io/gh/EducationalTestingService/factor_analyzer/branch/main/graph/badge.svg
   :target: https://codecov.io/gh/EducationalTestingService/factor_analyzer
   :alt: Code coverage

.. image:: https://anaconda.org/ets/factor_analyzer/badges/version.svg
   :target: https://anaconda.org/ets/factor_analyzer/
   :alt: Conda version

.. image:: https://img.shields.io/pypi/v/factor_analyzer
   :target: https://pypi.org/project/factor-analyzer/
   :alt: PyPI version

.. image:: https://img.shields.io/readthedocs/factor_analyzer/latest.svg
   :target: https://factor-analyzer.readthedocs.io/
   :alt: Docs

.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white
   :target: https://github.com/pre-commit/pre-commit
   :alt: Pre-commit checks


This is a Python module to perform exploratory and factor analysis (EFA), with several
optional rotations. It also includes a class to perform confirmatory factor
analysis (CFA), with certain pre-defined constraints. In exploratory factor analysis,
factor extraction can be performed using a variety of estimation techniques. The
``factor_analyzer`` package allows users to perform EFA using either (1) a minimum
residual (MINRES) solution, (2) a maximum likelihood (ML) solution, or (3) a principal
factor solution. However, CFA can only be performed using an ML solution.

Both the EFA and CFA classes within this package are fully compatible with `scikit-learn`.
Portions of this code are ported from the excellent R library `psych`, and the `sem`
package provided inspiration for the CFA class.

Please see the `official documentation `__ for additional details.


Description
-----------

Exploratory factor analysis (EFA) is a statistical technique used to
identify latent relationships among sets of observed variables in a
dataset. In particular, EFA seeks to model a large set of observed
variables as linear combinations of some smaller set of unobserved,
latent factors. The matrix of weights, or factor loadings, generated
from an EFA model describes the underlying relationships between each
variable and the latent factors.

Confirmatory factor analysis (CFA), a closely associated technique, is
used to test an a priori hypothesis about latent relationships among sets
of observed variables. In CFA, the researcher specifies the expected pattern
of factor loadings (and possibly other constraints), and fits a model according
to this specification.

Typically, a number of factors (K) in an EFA or CFA model is selected
such that it is substantially smaller than the number of variables. The
factor analysis model can be estimated using a variety of standard
estimation methods, including but not limited MINRES or ML.

Factor loadings are similar to standardized regression coefficients, and
variables with higher loadings on a particular factor can be interpreted
as explaining a larger proportion of the variation in that factor. In the
case of EFA, factor loading matrices are usually rotated after the factor
analysis model is estimated in order to produce a simpler, more interpretable
structure to identify which variables are loading on a particular factor.

Two common types of rotations are:

1. The **varimax** rotation, which rotates the factor loading matrix so
   as to maximize the sum of the variance of squared loadings, while
   preserving the orthogonality of the loading matrix.

2. The **promax** rotation, a method for oblique rotation, which builds
   upon the varimax rotation, but ultimately allows factors to become
   correlated.

This package includes a ``factor_analyzer`` module with a stand-alone
``FactorAnalyzer`` class. The class includes ``fit()`` and ``transform()``
methods that enable users to perform factor analysis and score new data
using the fitted factor model. Users can also perform optional rotations
on a factor loading matrix using the ``Rotator`` class.

The following rotation options are available in both ``FactorAnalyzer``
and ``Rotator``:

    (a) varimax (orthogonal rotation)
    (b) promax (oblique rotation)
    (c) oblimin (oblique rotation)
    (d) oblimax (orthogonal rotation)
    (e) quartimin (oblique rotation)
    (f) quartimax (orthogonal rotation)
    (g) equamax (orthogonal rotation)
    (h) geomin_obl (oblique rotation)
    (i) geomin_ort (orthogonal rotation)

In addition, the package includes a ``confirmatory_factor_analyzer``
module with a stand-alone ``ConfirmatoryFactorAnalyzer`` class. The
class includes ``fit()`` and ``transform()``  that enable users to perform
confirmatory factor analysis and score new data using the fitted model.
Performing CFA requires users to specify in advance a model specification
with the expected factor loading relationships. This can be done using
the ``ModelSpecificationParser`` class.

Note that the ``ConfirmatoryFactorAnalyzer`` class is very experimental at this point,
so use it with caution, especially if your data are highly non-normal.

Examples
--------

Exploratory factor analysis example.

.. code:: python

  In [1]: import pandas as pd
     ...: from factor_analyzer import FactorAnalyzer

  In [2]: df_features = pd.read_csv('tests/data/test02.csv')

  In [3]: fa = FactorAnalyzer(rotation=None)

  In [4]: fa.fit(df_features)
  Out[4]:
  FactorAnalyzer(bounds=(0.005, 1), impute='median', is_corr_matrix=False,
                 method='minres', n_factors=3, rotation=None, rotation_kwargs={},
                 use_smc=True)

  In [5]: fa.loadings_
  Out[5]:
  array([[-0.12991218,  0.16398151,  0.73823491],
         [ 0.03899558,  0.04658425,  0.01150343],
         [ 0.34874135,  0.61452341, -0.07255666],
         [ 0.45318006,  0.7192668 , -0.0754647 ],
         [ 0.36688794,  0.44377343, -0.01737066],
         [ 0.74141382, -0.15008235,  0.29977513],
         [ 0.741675  , -0.16123009, -0.20744497],
         [ 0.82910167, -0.20519428,  0.04930817],
         [ 0.76041819, -0.23768727, -0.12068582],
         [ 0.81533404, -0.12494695,  0.17639684]])

  In [6]: fa.get_communalities()
  Out[6]:
  array([0.5887579 , 0.00382308, 0.50452402, 0.72841182, 0.33184336,
         0.66208429, 0.61911037, 0.73194557, 0.64929612, 0.71149718])

Confirmatory factor analysis example.

.. code:: python

  In [1]: import pandas as pd

  In [2]: from factor_analyzer import (ConfirmatoryFactorAnalyzer,
     ...:                              ModelSpecificationParser)

  In [3]: df_features = pd.read_csv('tests/data/test11.csv')

  In [4]: model_dict = {"F1": ["V1", "V2", "V3", "V4"],
     ...:               "F2": ["V5", "V6", "V7", "V8"]}
  In [5]: model_spec = ModelSpecificationParser.parse_model_specification_from_dict(df_features,
     ...:                                                                           model_dict)

  In [6]: cfa = ConfirmatoryFactorAnalyzer(model_spec, disp=False)

  In [7]: cfa.fit(df_features.values)

  In [8]: cfa.loadings_
  Out[8]:
  array([[0.99131285, 0.        ],
         [0.46074919, 0.        ],
         [0.3502267 , 0.        ],
         [0.58331488, 0.        ],
         [0.        , 0.98621042],
         [0.        , 0.73389239],
         [0.        , 0.37602988],
         [0.        , 0.50049507]])

  In [9]: cfa.factor_varcovs_
  Out[9]:
  array([[1.        , 0.17385704],
         [0.17385704, 1.        ]])

  In [10]: cfa.transform(df_features.values)
  Out[10]:
  array([[-0.46852166, -1.08708035],
         [ 2.59025301,  1.20227783],
         [-0.47215977,  2.65697245],
         ...,
         [-1.5930886 , -0.91804114],
         [ 0.19430887,  0.88174818],
         [-0.27863554, -0.7695101 ]])

Requirements
------------

-  Python 3.8 or higher
-  ``numpy``
-  ``pandas``
-  ``scipy``
-  ``scikit-learn >= 1.6.0``

Contributing
------------

Contributions to ``factor_analyzer`` are very welcome. Please file an issue
in the repository if you would like to contribute.

You can install the development requirements in a virtual environment with:

.. code-block:: bash

   python -m pip install -e .[dev]
   pre-commit install

Installation
------------

You can install this package via ``pip`` with:

``$ pip install factor_analyzer``

Alternatively, you can install via ``conda`` with:

``$ conda install -c ets factor_analyzer``

License
-------

GNU General Public License (>= 2)

Owner

  • Name: ETS
  • Login: EducationalTestingService
  • Kind: organization
  • Email: opensource@ets.org
  • Location: Princeton, NJ

Educational Testing Service

GitHub Events

Total
  • Issues event: 6
  • Watch event: 23
  • Issue comment event: 4
  • Push event: 1
  • Pull request review event: 1
  • Pull request event: 2
  • Fork event: 3
Last Year
  • Issues event: 6
  • Watch event: 23
  • Issue comment event: 4
  • Push event: 1
  • Pull request review event: 1
  • Pull request event: 2
  • Fork event: 3

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 241
  • Total Committers: 12
  • Avg Commits per committer: 20.083
  • Development Distribution Score (DDS): 0.299
Past Year
  • Commits: 1
  • Committers: 1
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
jbiggsets j****s@e****g 169
Nitin Madnani n****i@e****g 41
jbiggsets j****s@g****m 15
Artem Glebov a****v@g****m 4
Divyanshu Sharma d****c@n****n 3
Kees Mulder k****r@g****m 2
Justin Mahlik j****3@s****m 2
schnell3526 s****r@g****m 1
Takashi Ikeuchi 5****n 1
Sateesh Mannar s****t@s****m 1
Jan Grzegorzewski j****9@h****e 1
Juan Benitez j****8@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 71
  • Total pull requests: 52
  • Average time to close issues: 5 months
  • Average time to close pull requests: 22 days
  • Total issue authors: 53
  • Total pull request authors: 15
  • Average comments per issue: 2.27
  • Average comments per pull request: 1.12
  • Merged pull requests: 44
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 4
  • Pull requests: 2
  • Average time to close issues: 7 days
  • Average time to close pull requests: 6 days
  • Issue authors: 4
  • Pull request authors: 1
  • Average comments per issue: 0.75
  • Average comments per pull request: 0.0
  • Merged pull requests: 2
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • desilinguist (7)
  • jbiggsets (5)
  • FlorinAndrei (4)
  • sergemelnikov86 (3)
  • Fireblend (2)
  • gaardhus (2)
  • pjuergens (2)
  • nachomaiz (1)
  • Chandrak1907 (1)
  • dmnapolitano (1)
  • blessu (1)
  • kiwi0fruit (1)
  • tonytbui (1)
  • fredshu (1)
  • pinpointcoder (1)
Pull Request Authors
  • jbiggsets (20)
  • desilinguist (16)
  • ikeuchi-screen (2)
  • hotessy (2)
  • sjawhar (1)
  • Jehaumont (1)
  • keesmulder (1)
  • sateeshmannar (1)
  • szorowi1 (1)
  • Xylambda (1)
  • juanbenitezdev (1)
  • schnell3526 (1)
  • jmahlik (1)
  • aglebov (1)
  • janekg89 (1)
Top Labels
Issue Labels
enhancement (4) documentation (1) help wanted (1) testing (1) bug (1)
Pull Request Labels
enhancement (6) bug (5)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 59,667 last-month
  • Total docker downloads: 101,325
  • Total dependent packages: 8
  • Total dependent repositories: 46
  • Total versions: 10
  • Total maintainers: 2
pypi.org: factor-analyzer

A Factor Analysis tool written in Python

  • Versions: 10
  • Dependent Packages: 8
  • Dependent Repositories: 46
  • Downloads: 59,667 Last month
  • Docker Downloads: 101,325
Rankings
Docker downloads count: 1.1%
Downloads: 1.6%
Dependent packages count: 1.9%
Dependent repos count: 2.2%
Average: 2.8%
Stargazers count: 4.7%
Forks count: 5.3%
Maintainers (2)
Last synced: 6 months ago

Dependencies

requirements.txt pypi
  • numpy *
  • pandas *
  • scikit-learn *
  • scipy *