Skip to content

statspai.multivariate

multivariate

Multivariate descriptive methods: principal components and factor analysis.

FactorResult

Bases: ResultProtocolMixin

Outcome of :func:factor.

Attributes:

Name Type Description
loadings DataFrame

Factor loadings (pattern matrix), variables in rows, retained factors in columns; unrotated.

uniqueness Series

One minus the communality of each variable: the share of its variance that the retained factors do not reproduce.

eigenvalues DataFrame

eigenvalue, difference, proportion and cumulative. For maximum likelihood the eigenvalues are the sums of squared loadings of the retained factors.

n_factors, n_obs int
loglik, aic, bic float

Maximum likelihood only. The log likelihood is -n / 2 times the minimised discrepancy between the fitted and the sample correlation matrix, which is zero for a perfect fit.

lr_independence dict

chi2, df, pvalue of the likelihood-ratio test that the variables are uncorrelated.

lr_factors dict or None

Maximum likelihood only: the test of k factors against an unrestricted correlation matrix (Bartlett-corrected).

heywood bool

True when a uniqueness is at its lower bound (an improper solution): 0.005 for maximum likelihood, below zero otherwise.

converged bool

Whether the iterations of 'ipf' (the communalities) or 'ml' (the optimiser) stopped at their tolerance. Always True for 'pf' and 'pcf', which do not iterate.

n_iter int

Iterations taken; 0 for 'pf' and 'pcf'.

Examples:

>>> import numpy as np, pandas as pd
>>> import statspai as sp
>>> rng = np.random.default_rng(0)
>>> f = rng.normal(size=400)
>>> df = pd.DataFrame({f"x{j}": f + rng.normal(size=400) for j in range(4)})
>>> res = sp.factor(df, method="pcf", n_factors=1)
>>> list(res.loadings.columns)
['Factor1']
>>> res.scores(df).shape
(400, 1)

rotate

rotate(method: str = 'varimax', *, normalize: bool = False, power: float = 3.0, tol: float = 1e-12, maxiter: int = 5000) -> 'FactorResult'

Rotate the retained factors.

Parameters:

Name Type Description Default
method ('varimax', 'promax')

'varimax' is the orthogonal rotation that maximises the variance of the squared loadings within each factor. 'promax' starts from varimax and lets the factors correlate: the target is the varimax loadings raised to power with their signs kept.

'varimax'
normalize bool

Kaiser normalisation: each variable's loadings are scaled to unit length before the rotation and scaled back after it.

False
power float

Promax power.

3.0

Returns:

Type Description
FactorResult

A copy with rotated loadings, rotation_matrix (loadings = unrotated @ rotation_matrix for varimax; for promax the matrix that rotates the factors, whose inverse transpose rotates the loadings), factor_correlation (the identity for varimax) and variance (the sum of squared correlations of the variables with each rotated factor). Uniquenesses and the tests do not change with a rotation. Factors are ordered by the variance they account for and signed so that their loadings sum to a positive number, which is what Stata's rotate prints.

Examples:

>>> import numpy as np, pandas as pd
>>> import statspai as sp
>>> rng = np.random.default_rng(0)
>>> f, g = rng.normal(size=(2, 500))
>>> df = pd.DataFrame({f"a{j}": f + rng.normal(size=500) for j in range(3)})
>>> for j in range(3):
...     df[f"b{j}"] = g + rng.normal(size=500)
>>> rot = sp.factor(df, method="pcf", n_factors=2).rotate("varimax")
>>> bool((rot.loadings.abs().max(axis=1) > 0.6).all())
True

scores

scores(data: DataFrame) -> DataFrame

Regression-method factor scores, z R^{-1} S, with z the variables standardised by the estimation sample's mean and standard deviation and S the correlations of the variables with the factors (the loadings, times the factor correlation after an oblique rotation).

PCAResult

Bases: ResultProtocolMixin

Outcome of :func:pca.

Attributes:

Name Type Description
eigenvalues DataFrame

One row per component: eigenvalue, difference to the next, proportion of total variance and cumulative proportion.

loadings DataFrame

The eigenvectors, variables in rows and retained components in columns. Each has unit length.

unexplained Series

Share of each variable's variance not reproduced by the retained components (zero when all are kept).

n_obs int

Complete rows used.

matrix {'correlation', 'covariance'}

What was decomposed.

Examples:

>>> import numpy as np, pandas as pd
>>> import statspai as sp
>>> rng = np.random.default_rng(0)
>>> f = rng.normal(size=300)
>>> df = pd.DataFrame({f"x{j}": f + rng.normal(size=300) for j in range(3)})
>>> res = sp.pca(df, n_components=1)
>>> res.loadings.shape
(3, 1)
>>> bool((res.unexplained > 0).all())
True

scores

scores(data: DataFrame) -> DataFrame

Component scores: the centred (and, for the correlation matrix, standardised) variables times the eigenvectors. The mean and standard deviation are those of the estimation sample. A row with a missing variable gets missing scores.

plot

plot(ax: Any = None) -> Any

Scree plot: eigenvalues against component number.