plsda: PCDA and PLSDA

View source: R/plsda.R

plsdaR Documentation

PCDA and PLSDA

Description

Discrimination (DA) on PCA or PLS latent variables.

- Function plsda fits PLSDA models, with various DA methods.

The class membership y (unidimensional variable) for the reference (= training) observations is firstly transformed to a table Ydummy containing nclas dummy variables (with function dummy), where nclas is the number of classes in y. Then, a PLS2 is implemented on the X-data and the dummy table Ydummy (with function pls), returning PLS2 scores finally used by the DA method.

The PLS2 algorithm is defined in argument algo.

The DA method is defined in argument da. The default DA method is dalm. Other methods presently avalaible are dadis, daglm, daprob and knnda. Any other ad'hoc DA methods can be developed by the user. Note that the DA method dasdod can not be used in plsda: in this last function, the number of PLS components (that corresponds to the input data for the DA) varies from 1 to ncomp; dasdod computes PCAs from the input data (here the PLS components) which cannot be implemented when the number of PLS comoonents is equal to 1 or 2.

- Function plsdalm is a faster equivalent of plsda(..., da = dalm, ...).

- Functions pcda and pcdalm does the same (and with the same syntax) as plsda but on principal component (instead of PLS) scores.

Matrix X is centered before the analyses, but X is not column-wise scaled (there is no argument scale available). If a scaling is needed, the user has to scale X before using the functions.

Row observations can eventually be weighted (using argument weights).

Usage


pcda(Xr, Yr, Xu, Yu = NULL, ncomp, algo = NULL, da = dalm, ...)

pcdalm(Xr, Yr, Xu, Yu = NULL, ncomp, algo = NULL, ...)

plsda(Xr, Yr, Xu, Yu = NULL, ncomp, algo = NULL, da = dalm, ...)

plsdalm(Xr, Yr, Xu, Yu = NULL, ncomp, algo = NULL,  ...)

Arguments

Xr

A n x p matrix or data frame of reference (= training) observations.

Yr

A vector of length n, or a n x 1 matrix, of reference (= training) responses (class membership).

Xu

A m x p matrix or data frame of new (= test) observations to be predicted.

Yu

A vector of length m, or a m x 1 matrix, of the true response (class membership). Default to NULL.

ncomp

The maximal number of PLS scores (= components = latent variables) to consider.

algo

For plsda, a function (algorithm) implementing a PLS. Default to NULL (pls_kernel is used). For pcda, a function (algorithm) implementing a PCA. Default to NULL: if n < p, pca_eigenk is used; in the other case, pca_eigen is used.

da

A function defining the discriminant method used for the predictions. Default to dalm.

...

Optionnal arguments to pass in functions defined in algo and da.

Value

A list of outputs (see examples), such as:

y

Responses for the test data.

fit

Predictions for the test data.

r

Residuals for the test data.

Examples


data(datforages)

Xr <- datforages$Xr
yr <- datforages$yr

Xu <- datforages$Xu
yu <- datforages$yu

Xr <- detrend(Xr)
Xu <- detrend(Xu)

headm(Xr)
headm(Xu)

table(yr)
table(yu)

######## PLS-DALM

ncomp <- 20
fm <- plsda(Xr, yr, Xu, yu, ncomp = ncomp)
names(fm)
headm(fm$y)
headm(fm$fit)
headm(fm$r)

z <- err(fm, ~ ncomp)
z[z$errp == min(z$errp), ]
plotmse(z, nam = "errp")

## Same with plsdalm (faster)

ncomp <- 20
fm <- plsdalm(Xr, yr, Xu, yu, ncomp = ncomp)
z <- err(fm, ~ ncomp)
z[z$errp == min(z$errp), ]
plotmse(z, nam = "errp")

######## PLS-LDA

ncomp <- 20
fm <- plsda(Xr, yr, Xu, yu, ncomp, da = daprob)

z <- err(fm, ~ ncomp)
z[z$errp == min(z$errp), ]
plotmse(z, nam = "errp")

######## PLS-QDA

ncomp <- 20
fm <- plsda(Xr, yr, Xu, yu, ncomp, da = daprob, 
  lda = FALSE)

z <- err(fm, ~ ncomp)
z[z$errp == min(z$errp), ]
plotmse(z, nam = "errp")

######## PLS-KNNDA

ncomp <- 15
fm <- plsda(Xr, yr, Xu, yu, ncomp, da = knnda, 
  diss = "mahalanobis", k = 3)
  
z <- err(fm, ~ ncomp + k)
z[z$errp == min(z$errp), ]
plotmse(z, nam = "errp", group = z$k)


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