xfit: Matrix fitting from scores and loadings matrices and SSR...

View source: R/xfit.R

xfitR Documentation

Matrix fitting from scores and loadings matrices and SSR calculation

Description

Function xfit calculates the estimate of a matrix X (say Xfit) from given multivariate (e.g. PCA or PLS) scores and loadings of dimension a.

Function xssr calculates the sum of the squared X-residuals SSR[i] = sum(E[i]^2) (i = 0, ..., a), where E[i] = X - Xfit[i], and X[0] the centered matrix X, The returned SSR is a vector of length a + 1.

Usage


xfit(T, P, xmeans = rep(0, dim(P)[1]))

xssr(X, T, P, xmeans = rep(0, dim(P)[1]))

Arguments

X

Only for xssr. The n x p matrix or data frame X for which SSR are calculated.

T

A n x a scores matrix, or data.frame, calculated from a n x p matrix X.

P

A p x a loadings matrix, or data.frame, that will be applied to T.

xmeans

An eventual centering vector (length p) used for X before calculating T.

Value

For xfit:

Fitted matrix X.

For xssr:

SSR vector of length a + 1 (SSR[i] i = 0, ..., a).

Examples


data(datcass)

Xr <- datcass$Xr
Xu <- datcass$Xu

headm(Xr)
headm(Xu)

ncomp <- 2
#ncomp <- 10
fm <- pca(Xr, Xu, ncomp = ncomp)

##################### Approximation of Xr

Xr.fit <- xfit(fm$Tr, fm$P, fm$xmeans)
headm(Xr.fit)

s <- sample(1:nrow(Xr), size = 1)
plotsp(Xr[s, , drop = FALSE], col = "red", zeroes = TRUE)
plotsp(Xr.fit[s, , drop = FALSE], col = "blue", add = TRUE, lty = 2)

##################### Approximation of Xu

Xu.fit <- xfit(fm$Tu, fm$P, fm$xmeans)
headm(Xu.fit)

s <- sample(1:nrow(Xu), size = 1)
plotsp(Xu[s, , drop = FALSE], col = "red", zeroes = TRUE)
plotsp(Xu.fit[s, , drop = FALSE], col = "blue", add = TRUE, lty = 2)

##################### SSR

xssr(Xr, fm$Tr, fm$P, fm$xmeans)

xssr(Xu, fm$Tu, fm$P, fm$xmeans)


mlesnoff/rnirs documentation built on April 24, 2023, 4:17 a.m.