Date: 2025-09-13

# Selecting the number of components

### bootPLS core functions

#### Frédéric Bertrand and Myriam Maumy-Bertrand

#### 2025-09-13

# Overview

This vignette is a short tutorial on the use of the core functions of the `bootPLS` package.

These functions can also be used to reproduce the simulations and the figures of the book chapter Magnanensi et al. (2016) and of the two articles https://doi.org/10.1007/978-3-319-40643-5_18, Magnanensi et al. (2017) https://doi.org/10.1007/s11222-016-9651-4 and Magnanensi et al. (2021) https://doi.org/10.3389/fams.2021.693126.

# Pine real dataset: pls and spls regressions

## Loading and displaying dataset

Load and display the pinewood worm dataset.

    library(bootPLS)
    library(plsRglm)
    data(pine, package = "plsRglm")
    Xpine<-pine[,1:10]
    ypine<-log(pine[,11])

    pairs(pine)

Michel Tenenhaus’ reported in his book, La régression PLS (1998) Technip, Paris, that most of the expert biologists claimed that this dataset features two latent variables, which is tantamount to the PLS model having two components.

## PLS LOO and CV

Leave one out CV (`K=nrow(pine)`) one time (`NK=1`).

    bbb <- plsRglm::cv.plsR(log(x11)~.,data=pine,nt=6,K=nrow(pine),NK=1,verbose=FALSE)
    plsRglm::cvtable(summary(bbb))

Set up 6-fold CV (`K=6`), 100 times (`NK=100`), and use `random=TRUE` to randomly create folds for repeated CV.

    bbb2 <- plsRglm::cv.plsR(log(x11)~.,data=pine,nt=6,K=6,NK=100,verbose=FALSE)

Display the results of the cross-validation.

    plsRglm::cvtable(summary(bbb2))

The \(Q^2\) criterion is recommended in that PLSR setting without missing data. A model with 1 component is selected by the cross-validation as displayed by the following figure. Hence the \(Q^2\) criterion (1 component) does not agree with the experts (2 components).

    plot(plsRglm::cvtable(summary(bbb2)),type="CVQ2")

As for the CV Press criterion it is unable to point out a unique number of components.

    plot(plsRglm::cvtable(summary(bbb2)),type="CVPress")

## PLS (Y,T) Bootstrap

The package features our bootstrap based algorithm to select the number of components in plsR regression. It is implemented with the `nbcomp.bootplsR` function.

    set.seed(4619)
    nbcomp.bootplsR(Y=ypine,X=Xpine,R =500)

The `verbose=FALSE` option suppresses messages output during the algorithm, which is useful to replicate the bootstrap technique. To set up parallel computing, you can use the `parallel` and the `ncpus` options.

    set.seed(4619)
    res_boot_rep <- replicate(20,nbcomp.bootplsR(Y=ypine,X=Xpine,R =500,verbose =FALSE,parallel = "multicore",ncpus = 2))

It is easy to display the results with the `barplot` function.

    barplot(table(res_boot_rep))

A model with two components should be selected using our bootstrap based algorithm to select the number of components. Hence the number of component selected with our algorithm agrees with what was stated by the experts.

## sPLS (Y,T) Bootstrap

The package also features our bootstrap based algorithm to select, for a given \(\eta\) value, the number of components in spls regression. It is implemented with the `nbcomp.bootspls` function.

    nbcomp.bootspls(x=Xpine,y=ypine,eta=.5)

A `doParallel` and `foreach` based parallel computing version of the algorithm is implemented as the `nbcomp.bootspls.para` function.

    nbcomp.bootspls.para(x=Xpine,y=ypine,eta=.5)
    nbcomp.bootspls.para(x=Xpine,y=ypine,eta=c(.2,.5))

## Bootstrap (Y,X) for the coefficients with number of components updated for each resampling

Pinewood worm data reloaded.

    library(bootPLS)
    library(plsRglm)
    data(pine, package = "plsRglm")
    Xpine<-pine[,1:10]
    ypine<-log(pine[,11])
    datasetpine <- cbind(ypine,Xpine)

    coefs.plsR.adapt.ncomp(datasetpine,sample(1:nrow(datasetpine)))

Replicate the results to get the bootstrap distributions of the selected number of components and the coefficients.

    replicate(20,coefs.plsR.adapt.ncomp(datasetpine,sample(1:nrow(datasetpine))))

Parallel computing support with the `ncpus` and `parallel="multicore"` options.

    coefs.plsR.adapt.ncomp(datasetpine,sample(1:nrow(datasetpine)),ncpus=2,parallel="multicore")

    replicate(20,coefs.plsR.adapt.ncomp(datasetpine,sample(1:nrow(datasetpine)),ncpus=2,parallel="multicore"))

# Aze real dataset: binary logistic plsRglm and sgpls regressions

## PLSGLR (Y,T) Bootstrap

Loading the data and creating the data frames.

    data(aze_compl)
    Xaze_compl<-aze_compl[,2:34]
    yaze_compl<-aze_compl$y
    dataset <- cbind(y=yaze_compl,Xaze_compl)

Fitting a logistic PLS regression model with 10 components. You have to use the family option when fitting the plsRglm.

    modplsglm <- plsRglm(y~.,data=dataset,10,modele="pls-glm-family",family="binomial")

Perform the bootstrap based algorithm with the `nbcomp.bootplsRglm` function. By default 250 resamplings are carried out.

    set.seed(4619)
    aze_compl.bootYT <- suppressWarnings(nbcomp.bootplsRglm(modplsglm))

Plotting the bootstrap distributions of the coefficients of the components.

    plsRglm::boxplots.bootpls(aze_compl.bootYT)

Computing the bootstrap based confidence intervals of the coefficients of the components.

    plsRglm::confints.bootpls(aze_compl.bootYT)

Computing the bootstrap based confidence intervals of the coefficients of the components.

    suppressWarnings(plsRglm::plots.confints.bootpls(plsRglm::confints.bootpls(aze_compl.bootYT)))

## sparse PLSGLR (Y,T) Bootstrap

The package also features our bootstrap based algorithm to select, for a given \(\eta\) value, the number of components in sgpls regression. It is implemented in the `nbcomp.bootsgpls`.

    set.seed(4619)
    data(prostate, package="spls")
    nbcomp.bootsgpls((prostate$x)[,1:30], prostate$y, R=250, eta=0.2, typeBCa = FALSE)

A `doParallel` and `foreach` based parallel computing version of the algorithm is implemented as the `nbcomp.bootspls.para` function.

    nbcomp.bootsgpls.para((prostate$x)[,1:30], prostate$y, R=250, eta=c(.2,.5), maxnt=10, typeBCa = FALSE)

    rm(list=c("Xpine","ypine","bbb","bbb2","aze_compl","Xaze_compl","yaze_compl","aze_compl.bootYT","dataset","modplsglm","pine","datasetpine","prostate","res_boot_rep"))