ParamPCA performs regression of parametrized PCA, which allows the PCA components themselves to depend upon some auxiliary information, such as time.
We refer to these parameterizing factors as metadata
and we perform PCA on data
.
The metadata factors can be continuous.
ParamPCA works by non-linear optimization to minimize the squared sum residual of the data after projecting each observation in data
on to the PCA hyperplane determined by that observations metadata
.
Parameterizations can be specified as patsy formulas (similar to R).
Formulas are single-sided (like group * age
not y ~ group * age
) since the left-hand-side is always the provided data
.
The following example shows using this method to perform a 'cosinor' style regression of PCA.
Cosinor regression is commonly used for identifying factors with 24 hour periods in circadian medicine and is performed by y ~ cos(t) + sin(t)
.
This does the ParamPCA equivalent of that.
import numpy as np
import pandas
from param_pca import ParamPCA
## EXAMPLE DATA
np.random.seed(0)
N = 500 # N independent samples
t = np.linspace(0,2 * np.pi, N) # time
scores = np.random.normal(size=N) # Simulate one component of common variation
data = np.concatenate([
[np.cos(t/2)**2 * scores*5 + scores * 10], # Component largest in this at t=0
[np.sin(t/2)**2 * scores*5 + scores * 10], # Component largest in this at t=pi (i.e., 12 hours)
np.random.normal(size=(1,N))*5,
np.random.normal(size=(1,N))*5,
np.random.normal(size=(100,N)),
], axis=0).T
metadata = pandas.DataFrame({"t": t})
## Perform the ParamPCA regression
result = ParamPCA(data, metadata, "np.cos(t) + np.sin(t)", 3, R = 10, verbose=True)
print(result.summary())
For
where
where t + s
means that
for some fixed matrices A, B, C corresponding and