plsr: PCR and PLSR Models

View source: R/plsr.R

plsrR Documentation

PCR and PLSR Models

Description

Regression on PCA or PLS latent variables (scores).

Functions pcr and plsr assume PCA and PLS algorithms providing orthogonal 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


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

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

Arguments

Xr

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

Yr

A n x q matrix or data frame, or a vector of length n, of reference (= training) responses.

Xu

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

Yu

A m x q matrix or data frame, or a vector of length m, of the true responses for Xu. Default to NULL.

ncomp

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

algo

For plsr, a function (algorithm) implementing a PLS on the training data set. Default to NULL (pls_kernel is used). For pcr, a function (algorithm) implementing a PCA. Default to NULL: if n < p, pca_eigenk is used; in the other case, pca_eigen is used.

...

Optionnal arguments to pass in the function defined in algo.

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


n <- 10
p <- 6
set.seed(1)
X <- matrix(rnorm(n * p, mean = 10), ncol = p)
y1 <- 100 * rnorm(n)
y2 <- 100 * rnorm(n)
Y <- cbind(y1, y2)
set.seed(NULL)

Xr <- X[1:8, ] ; Yr <- Y[1:8, ] 
Xu <- X[9:10, ] ; Yu <- Y[9:10, ] 

ncomp <- 3
fm <- plsr(Xr, Yr, Xu, Yu, ncomp = ncomp)
#fm <- pcr(Xr, Yr, Xu, Yu, ncomp = ncomp)
names(fm)
fm$y
fm$fit
fm$r
names(fm)

plsr(Xr, Yr, Xu, ncomp = ncomp)[c("y", "fit", "r")]

plsr(Xr, Yr[, 1], Xu, Yu[, 1], ncomp = ncomp)[c("y", "fit", "r")]

####### MSE

ncomp <- 3
fm <- plsr(Xr, Yr, Xu, Yu, ncomp = ncomp)
mse(fm, ~ ncomp, nam = "y1")
mse(fm, ~ ncomp, nam = "y2")
mse(fm, ~ ncomp)

z <- mse(fm, ~ ncomp)
z
z[z$msep == min(z$msep), ]
plotmse(z)

####### B-COEFFICIENTS FOR THE MODEL WITH ncomp COMPONENTS

bcoef(fm, ncomp = 2)

####### Weighted PLSR

# fictive weights
plsr(Xr, Yr, Xu, Yu, ncomp = ncomp, weights = 1:nrow(Xr))


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