scores: Calculate Scores and Distances for Multi-Source PCA

View source: R/SRPCAMS_helpers.R

scoresR Documentation

Calculate Scores and Distances for Multi-Source PCA

Description

Computes principal component scores, score distances (SD), and orthogonal distances (OD) for observations grouped into multiple sources using the multi-source PCA model. The function supports either an 'ssMRCD' object for robust local centering and scaling or manually provided group-wise means ('mu') and covariances ('Sigma').

Usage

scores(PC, ssMRCD = NULL, X = NULL, groups = NULL, mu = NULL, Sigma = NULL)

Arguments

PC

A 3D array representing the principal component loading matrices for each group (dimensions: variables × components × groups).

ssMRCD

An optional 'ssMRCD' object used to robustly center and scale 'X'. If 'NULL', then 'X', 'groups', 'mu' and 'Sigma', must be provided.

X

An optional matrix of observations (rows are samples, columns are variables), required if 'ssMRCD' is not provided.

groups

An optional numeric vector specifying group/source membership for each observation in 'X', required if 'ssMRCD' is not provided.

mu

Optional list of group-wise means, required if 'ssMRCD' is not provided.

Sigma

Optional list of group-wise covariance matrices, required if 'ssMRCD' is not provided.

Value

A list with the following components:

scores

Matrix of principal component scores for each observation.

SD

Numeric vector of score distances, i.e., Mahalanobis distances in the PCA space.

OD

Numeric vector of orthogonal distances (reconstruction error orthogonal to PCA space).

X_centered

Locally centered input data.

See Also

ssMRCD, scale.ssMRCD, msPCA

Examples

# create data set
x1 = matrix(runif(200), ncol = 2)
x2 = matrix(rnorm(200), ncol = 2)
x = list(x1, x2)

# create weighting matrix
W = matrix(c(0, 1, 1, 0), ncol = 2)

# calculate ssMRCD
loccovs = ssMRCD(x, weights = W, lambda = 0.5)

# calculate PCA
pca = msPCA(eta = 1, gamma = 0.5,COVS = loccovs$MRCDcov)

# calculate scores
scores_all = scores(PC = pca$PC, ssMRCD = loccovs)
str(scores_all)

scores_all = scores(PC = pca$PC,
                    X = rbind(x1, x2),
                    groups = rep(c(1,2), each = 100),
                    mu = loccovs$MRCDmu,
                    Sigma = loccovs$MRCDcov)
str(scores_all)

ssMRCD documentation built on Nov. 5, 2025, 7:44 p.m.