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
|
|
n_factors, n_obs |
int
|
|
loglik, aic, bic |
float
|
Maximum likelihood only. The log likelihood is |
lr_independence |
dict
|
|
lr_factors |
dict or None
|
Maximum likelihood only: the test of |
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 |
n_iter |
int
|
Iterations taken; 0 for |
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'
|
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 |
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 ¶
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: |
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 ¶
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.