I was curious about using loadings and scores for obtaining the X matrix back. While doing computations on matrices I observed that even though, according to the vignette, the plsda output should contain ‘defl.matrix’ in the final object this is empty, the output doesn’t contain defl.matrix. I use now the newest version 6.24.0. Would it be possible to fix it?
We will put it on our to-do list (but this will likely only be fixed in 2024).
In the meantime, here is the code for PLSDA. This is the code that will output the deflation matrices for each dimension / component:
## Use SRBCT data + PLSDA, get the deflated matrices
library(mixOmics)
## Set-up, following http://mixomics.org/case-studies/splsda-srbct-case-study/
set.seed(5249)
data(srbct) # extract the small round bull cell tumour data
X <- srbct$gene # use the gene expression data as the X matrix
Y <- srbct$class # use the class data as the Y matrix
srbct.splsda <- splsda(X, Y, ncomp = 10, all.outputs = T) # set ncomp to 10 for performance assessment later
# Problem 1: all.outputs option does not make a difference, T or F, output is the same
# list of length 22
# See names(srbct.splsda)
# Problem 2: srbct.splsda$Y is not a centered and scaled indicator matrix as the help file says
## Deflation: regression mode in NIPALS (same as pls::oscorespls.fit) ----------------------
ncomp <- srbct.splsda$ncomp
score <- srbct.splsda$variates
loading <- srbct.splsda$loadings
# Centered and scaled (by default) X and Y
Xdfl <- srbct.splsda$X
Ydfl <- srbct.splsda$Y |> unmap() |>
`dimnames<-`(list(rownames(Xdfl), levels(srbct.splsda$Y)))
# Main loop for deflation
dfl <- vector("list", ncomp)
for(comp in 1:ncomp){
t <- score$X[,comp,drop=F]
u <- score$Y[,comp,drop=F]
c <- crossprod(Xdfl, t)/sum(t^2)
d <- crossprod(Ydfl, t)/sum(t^2)
Xdfl <- Xdfl - tcrossprod(t, c)
Ydfl <- Ydfl - tcrossprod(t, d)
dfl[[comp]] <- list(
X = Xdfl,
Y = Ydfl
)
}
## Where to find mixOmics source code for deflation
# mixOmics::splsda
# mixOmics:::internal_wrapper.mint
# mixOmics:::internal_mint.block # by default initialise using svd (mixOmics:::initialisation_by_svd)
# mixOmics:::sparse.mint.block_iteration
# mixOmics:::defl.select