% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/model.R
\name{model}
\alias{model}
\alias{predict.resemble_model}
\title{Global spectral calibration model}
\usage{
model(Xr, Yr, fit_method, control = model_control(), verbose = TRUE)

\method{predict}{resemble_model}(object, newdata, ncomp = NULL, ...)
}
\arguments{
\item{Xr}{A numeric matrix of predictor variables, with observations in rows
and variables in columns. Typically spectral data.}

\item{Yr}{A numeric matrix with one column containing the response variable
values corresponding to the observations in \code{Xr}.}

\item{fit_method}{An object created by \code{\link{fit_pls}} or
\code{\link{fit_gpr}} specifying the regression method and its parameters,
including centring and scaling options.
\code{fit_wapls()} is not supported for global models because it requires
target observations to compute weights.}

\item{control}{A list created by \code{model_control()} specifying the
cross-validation settings. The default is \code{model_control()}.}

\item{verbose}{Logical indicating whether progress information should be
printed. Default is \code{TRUE}.}

\item{object}{A \code{resemble_model} object returned by \code{\link{model}}.}

\item{newdata}{A numeric matrix of new observations with the same number of
columns as the training data.}

\item{ncomp}{For PLS models, the number of components to use for prediction.
The default is the number used during fitting. Ignored for GPR models. For
prediction, this can be a vector of integers representing the predictions
coming from models with the requested components.}

\item{...}{Additional arguments, currently unused.}
}
\value{
For \code{model()}, an object of class \code{resemble_model} containing:
\itemize{
\item \code{fit_method}: Fitting method object used.
\item \code{control}: Model control object used.
\item \code{model}: Fitted model object.
\item \code{cv_results}: Cross-validation results, if
\code{validation_type = "lgo"}.
\item \code{n_obs}: Number of observations used.
\item \code{n_vars}: Number of predictor variables.
}

For \code{predict.resemble_model()}, a numeric matrix of predictions. For
PLS models, columns correspond to different numbers of components.
}
\description{
\loadmathjax

Fits a global calibration model for spectral data using partial least
squares (PLS) or Gaussian process regression (GPR). Unlike
\code{\link{mbl}}, which builds local models for each prediction,
\code{model()} fits a single global model to the reference data.
}
\details{
\code{model()} provides a straightforward interface for fitting global
calibration models, in contrast to the local modelling approach implemented
in \code{\link{mbl}}. This is useful when:
\itemize{
\item{the relationship between spectra and the response is approximately
linear across the full dataset}
\item{a single portable model is needed for deployment}
\item{computational efficiency is prioritised over predictive performance
in heterogeneous datasets}
}

\subsection{Cross-validation}{
When \code{validation_type = "lgo"}, stratified random sampling is used
to create training-validation splits that preserve the distribution of the
response variable. Cross-validation results include RMSE, R², and
standardised RMSE for each component in PLS models, or overall in GPR
models.
}

\subsection{PLS models}{
When \code{fit_pls()} is used, the function fits a PLS model with the
specified number of components. If cross-validation is enabled, the optimal
number of components is selected based on the minimum RMSE.
}

\subsection{GPR models}{
When \code{fit_gpr()} is used, the function fits a Gaussian process
regression model with a dot-product covariance function. The noise variance
parameter controls regularisation.
}
}
\examples{
\donttest{
library(prospectr)
data(NIRsoil)

# Preprocess spectra
Xr <- savitzkyGolay(NIRsoil$spc, m = 1, p = 2, w = 11)
Yr <- NIRsoil$CEC

# Remove missing values
ok <- !is.na(Yr)
Xr <- Xr[ok, ]
Yr <- as.matrix(Yr[ok])

# Fit a PLS model with 10 components and cross-validation
# Scaling is controlled via fit_pls()
pls_mod <- model(
  Xr = Xr,
  Yr = Yr,
  fit_method = fit_pls(ncomp = 10, scale = FALSE),
  control = model_control(validation_type = "lgo", number = 10)
)

# View cross-validation results
pls_mod$cv_results

# Fit a GPR model (centring/scaling controlled via fit_gpr())
gpr_mod <- model(
  Xr = Xr,
  Yr = Yr,
  fit_method = fit_gpr(noise_variance = 0.001, scale = TRUE),
  control = model_control(validation_type = "lgo")
)
}

}
\references{
de Jong, S. (1993). SIMPLS: An alternative approach to partial least
squares regression. \emph{Chemometrics and Intelligent Laboratory Systems},
18(3), 251--263.

Rasmussen, C. E., & Williams, C. K. I. (2006). \emph{Gaussian Processes for
Machine Learning}. MIT Press.

Shenk, J. S., & Westerhaus, M. O. (1991). Population structuring of near
infrared spectra and modified partial least squares regression.
\emph{Crop Science}, 31(6), 1548--1555.
}
\seealso{
\code{\link{fit_pls}} and \code{\link{fit_gpr}} for specifying the fitting
method;
\code{\link{model_control}} for controlling aspects of the modelling
process;
\code{\link{mbl}} for memory-based (local) learning.
}
\author{
\href{https://orcid.org/0000-0002-5369-5120}{Leonardo Ramirez-Lopez}
}
