% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stats_fit_pca.R
\name{pca}
\alias{pca}
\title{Principal Component Analysis (PCA)}
\usage{
pca(X, scaling, ncomp, method = "nipals")
}
\arguments{
\item{X}{Numeric matrix (rows = samples, columns = variables).}

\item{scaling}{A \code{ScalingStrategy} object defining model-internal
centering and scaling (e.g. \code{uv_scaling(center = TRUE)}).}

\item{ncomp}{Integer. Number of principal components to compute.}

\item{method}{Character. PCA backend. Use \code{"nipals"} for the
internal implementation or any method supported by
\code{pcaMethods::pca()}.}
}
\value{
An object of class \code{m8_model} with \code{engine = "pca"}.
The object contains:
\itemize{
\item \code{fit$t}: Score matrix (samples × components)
\item \code{fit$p}: Loading matrix (components × variables)
\item \code{ctrl}: Model control information (variance explained, scaling settings)
\item \code{provenance}: Attributes inherited from the input matrix
}
}
\description{
Fits an unsupervised Principal Component Analysis (PCA) model
to a spectral data matrix. Model-internal centering and scaling
are controlled via a \code{ScalingStrategy} object and do not
modify the input matrix.

The default backend uses an internal NIPALS implementation.
Alternatively, PCA algorithms from the \pkg{pcaMethods} package
(e.g. \code{"ppca"}, \code{"svd"}, \code{"robustPca"}) can be used.
}
\details{
PCA decomposes \eqn{X} into orthogonal score and loading matrices:
\deqn{X \approx T P}
where:
\itemize{
\item \eqn{T} contains the principal component scores
\item \eqn{P} contains the loadings
}

The number of components is fixed by \code{ncomp}.
Unlike supervised models, PCA does not use cross-validation
or stopping rules.

Scaling and centering are applied internally during model fitting.
The original input matrix is not modified.
}
\examples{
data(covid)

uv <- uv_scaling(center=TRUE)
model <- pca(X=covid$X, scaling=uv, ncomp=2)

show(model)
summary(model)

Tx <- scores(model)
Px <- loadings(model)

t2 <- hotellingsT2(Tx)
ell <-ellipse2d(t2)

# scores plot
plot(Tx, asp = 1,
  col = as.factor(covid$an$type),
  xlim = range(c(Tx[1,], ell$x)),
  ylim = range(c(Tx[2,], ell$y))
 )
lines(ell$x, ell$y, col = "grey", lty=2)
}
\seealso{
\code{\link{uv_scaling}}

Other modelling: 
\code{\link{opls}()},
\code{\link{pls}()}
}
\concept{modelling}
