# neurostatx.utils.factor

*module*

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L1-L1)

## neurostatx.utils.factor.MethodTypes

*class*

```python
class MethodTypes(StrEnum, Enum)
```

Bases: `StrEnum`, `Enum`

Factor extraction methods for exploratory factor analysis.

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L50-L55)

### neurostatx.utils.factor.MethodTypes.minres

*attribute* · *class attribute* · *instance attribute*

```python
minres = 'minres'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L53-L53)

### neurostatx.utils.factor.MethodTypes.ml

*attribute* · *class attribute* · *instance attribute*

```python
ml = 'ml'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L54-L54)

### neurostatx.utils.factor.MethodTypes.principal

*attribute* · *class attribute* · *instance attribute*

```python
principal = 'principal'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L55-L55)

## neurostatx.utils.factor.RotationTypes

*class*

```python
class RotationTypes(StrEnum, Enum)
```

Bases: `StrEnum`, `Enum`

Factor rotation methods for exploratory factor analysis.

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L38-L47)

### neurostatx.utils.factor.RotationTypes.equamax

*attribute* · *class attribute* · *instance attribute*

```python
equamax = 'equamax'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L47-L47)

### neurostatx.utils.factor.RotationTypes.oblimax

*attribute* · *class attribute* · *instance attribute*

```python
oblimax = 'oblimax'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L44-L44)

### neurostatx.utils.factor.RotationTypes.oblimin

*attribute* · *class attribute* · *instance attribute*

```python
oblimin = 'oblimin'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L42-L42)

### neurostatx.utils.factor.RotationTypes.promax

*attribute* · *class attribute* · *instance attribute*

```python
promax = 'promax'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L41-L41)

### neurostatx.utils.factor.RotationTypes.quartimax

*attribute* · *class attribute* · *instance attribute*

```python
quartimax = 'quartimax'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L46-L46)

### neurostatx.utils.factor.RotationTypes.quartimin

*attribute* · *class attribute* · *instance attribute*

```python
quartimin = 'quartimin'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L45-L45)

### neurostatx.utils.factor.RotationTypes.varimax

*attribute* · *class attribute* · *instance attribute*

```python
varimax = 'varimax'
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L43-L43)

## neurostatx.utils.factor.cfa

*function*

```python
def cfa(df, model)
```

Fit a confirmatory factor analysis model with semopy.

**Parameters**

- `df` (`pd.DataFrame`) — Observed indicators included in the CFA.
- `model` (`str`) — semopy model specification.

**Returns**

- `fitted` (`semopy.Model`) — Fitted CFA model.
- `scores` (`pd.DataFrame`) — Predicted factor scores.
- `stats` (`pd.DataFrame`) — Parameter estimates from ``inspect``.

**Examples**

```pycon
>>> import pandas as pd
>>> import numpy as np
>>> from neurostatx.utils.factor import cfa
>>> rng = np.random.RandomState(0)
>>> f = rng.randn(40)
>>> df = pd.DataFrame({"x1": f + 0.1 * rng.randn(40),
...                    "x2": f + 0.1 * rng.randn(40),
...                    "x3": f + 0.1 * rng.randn(40)})
>>> fitted, scores, stats = cfa(df, "F =~ x1 + x2 + x3")
>>> "F" in scores.columns
True
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L243-L286)

## neurostatx.utils.factor.efa

*function*

```python
def efa(df, method, rotation, nfactors=1)
```

Fit an exploratory factor analysis with factor_analyzer.

**Parameters**

- `df` (`pd.DataFrame`) — Variables to include in the EFA.
- `method` (`str`) — Extraction method: ``"minres"``, ``"ml"``, or ``"principal"``.
- `rotation` (`str`) — Rotation applied to the loadings, or None.
- `nfactors` (`int`) (default: `1`) — Number of factors to extract. Defaults to 1.

**Returns**

- `model` (`FactorAnalyzer`) — Fitted factor analyzer.
- `ev` (`np.array`) — Original eigenvalues.
- `v` (`np.array`) — Common-factor eigenvalues.
- `scores` (`np.array`) — Factor scores.
- `loadings` (`np.array`) — Factor loadings.
- `communalities` (`np.array`) — Communalities.

**Examples**

```pycon
>>> import pandas as pd
>>> import numpy as np
>>> from neurostatx.utils.factor import efa
>>> rng = np.random.RandomState(0)
>>> df = pd.DataFrame(rng.rand(40, 4), columns=list("abcd"))
>>> model, ev, v, scores, loadings, communalities = efa(
...     df, method="minres", rotation="varimax", nfactors=1
... )
>>> scores.shape[0]
40
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L186-L240)

## neurostatx.utils.factor.horn_parallel_analysis

*function*

```python
def horn_parallel_analysis(x, output_folder, method='minres', rotation=None, nfactors=1, niter=20)
```

Estimate the number of factors and components with Horn's method.

Compares observed eigenvalues to those from random data, following
psych's ``fa.parallel``.

**Parameters**

- `x` (`np.array`) — Variables to include in the analysis.
- `output_folder` (`str`) — Directory where ``horns_parallel_screeplot.png`` is written.
- `method` (`str`) (default: `'minres'`) — Extraction method: ``"minres"``, ``"ml"``, or ``"principal"``. Defaults to ``"minres"``.
- `rotation` (`str`) (default: `None`) — Rotation applied when fitting. Defaults to None.
- `nfactors` (`int`) (default: `1`) — Number of factors extracted while computing eigenvalues. Defaults to 1.
- `niter` (`int`) (default: `20`) — Number of random-data iterations. Defaults to 20.

**Returns**

- `suggfactors` (`int`) — Suggested number of factors.
- `suggcomponents` (`int`) — Suggested number of components.

**Examples**

```pycon
>>> import numpy as np
>>> from neurostatx.utils.factor import horn_parallel_analysis
>>> rng = np.random.RandomState(0)
>>> x = rng.rand(40, 5)
>>> suggfactors, suggcomponents = horn_parallel_analysis(
...     x, output_folder=".", niter=2
... )
```

[View source](https://github.com/gagnonanthony/NeuroStatX/blob/main/neurostatx/utils/factor.py#L75-L183)
