% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/krnn.R
\name{krnn_fit}
\alias{krnn_fit}
\alias{print.krnn_fit}
\title{k-Ranked Nearest Neighbour Classifier for Functional Data}
\usage{
krnn_fit(
  X_train,
  y_train,
  X_test = NULL,
  k = 5L,
  grid = NULL,
  type = c("tukey", "simplicial"),
  sd_train = NULL
)

\method{print}{krnn_fit}(x, ...)
}
\arguments{
\item{X_train}{Numeric matrix (\eqn{n_{\mathrm{train}} \times M}) of
training curves (rows = curves, columns = time points).}

\item{y_train}{Integer vector of class labels (0/1) of length
\eqn{n_{\mathrm{train}}}.}

\item{X_test}{Numeric matrix of test curves. If \code{NULL} (default) the
training set is used for in-sample evaluation.}

\item{k}{Positive integer: half-neighbourhood size. The total number of
neighbours per observation is \eqn{2k}. Use \code{\link{krnn_cv}} to
select k.}

\item{grid}{Numeric vector of length \eqn{M} giving observation times.
Defaults to equally spaced points on \eqn{[0, 1]}.}

\item{type}{Depth variant passed to \code{\link{fm_depth}}: \code{"tukey"}
(default) or \code{"simplicial"}.}

\item{sd_train}{Optional pre-computed \code{"fd_signed_depth"} object for
the training set (avoids recomputation when called repeatedly).}

\item{x}{A \code{"krnn_fit"} object.}

\item{...}{Ignored.}
}
\value{
An object of class \code{"krnn_fit"} with components:
\describe{
\item{predicted}{Integer vector of predicted class labels.}
\item{prob}{Numeric vector of estimated probabilities
\eqn{\hat{P}(y=0|\mathrm{sdp})}.}
\item{sd_train}{The \code{"fd_signed_depth"} object for the training
set.}
\item{y_train}{The training labels.}
\item{k}{The value of k used.}
}

Invisibly returns \code{x}.
}
\description{
Fits the k-RNN classifier of Perez Ruiz (2020). Curves are ranked by their
signed depth and each new observation is assigned to the majority class
among its \eqn{2k} nearest ranked neighbours (k above and k below).
}
\examples{
set.seed(7)
M <- 100; n <- 80
t <- seq(0, 1, length.out = M)
X0 <- t(replicate(n / 2, sin(2 * pi * t) + rnorm(M, sd = 0.4)))
X1 <- t(replicate(n / 2, cos(2 * pi * t) + rnorm(M, sd = 0.4)))
X  <- rbind(X0, X1)
y  <- rep(0:1, each = n / 2)
idx_tr <- sample(n, 60)
fit <- krnn_fit(X[idx_tr, ], y[idx_tr], X[-idx_tr, ], k = 5)
print(fit)

}
\references{
Perez Ruiz, D. A. (2020). \emph{Supervised Classification for Functional
Data}. PhD thesis, University of Manchester. Section 3.2.
}
\seealso{
\code{\link{krnn_cv}}, \code{\link{predict.krnn_fit}}
}
