% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/error.R
\name{error.krr}
\alias{error.krr}
\alias{error}
\title{Compute Model Error for Kernel Ridge Regression Models}
\usage{
error(x, ...)

\method{error}{krr}(x, data_new = NULL, ...)
}
\arguments{
\item{x}{An object of class \code{"krr"}, typically returned by
\code{\link{fastkrr}}.}

\item{...}{Additional arguments (ignored).}

\item{data_new}{An optional data frame containing new predictor variables
and the observed response variable. The response column must have the
same name as the response variable used to fit the model. If
\code{NULL}, the training MSE is computed. Otherwise, the prediction MSE (PMSE)
is computed using predictions for \code{data_new}.}
}
\value{
A numeric value giving the mean squared error (MSE). If \code{data_new = NULL},
  this returns the training MSE based on the fitted values. If \code{data_new} is provided,
  it returns the prediction mean squared error (PMSE) evaluated on the new dataset.
}
\description{
Computes the model error for kernel ridge regression (\code{"krr"} object).
Returns the mean squared error (MSE) between the observed responses
and the fitted values stored in the object.
}
\details{
This method computes the mean squared error defined as:
\deqn{\text{MSE} = \frac{1}{n} \sum_{i = 1} (y_i - \hat{y}_i)^2}
Depending on the presence of \code{data_new}, the components are defined as follows:
\itemize{
  \item If \code{data_new = NULL}, \eqn{y} is the observed response vector and
    \eqn{\hat{y}} is the fitted values vector, both stored within the \code{"krr"} object.
  \item If \code{data_new} is provided, \eqn{y} is the observed response extracted from
    \code{data_new} and \eqn{\hat{y}} is the predicted values vector generated by
    applying \code{\link{predict.krr}} to the new predictor variables.
}
}
\examples{
# Data setting
set.seed(1)
lambda = 1e-4
d = 1
n = 50
rho = 1
X = matrix(runif(n*d, 0, 1), nrow = n, ncol = d)
y = sin(2 * pi * rowMeans(X)^3) + rnorm(n, mean = 0, sd = 0.1)

data = data.frame(X, y = y)

model = fastkrr(data = data, response = "y", kernel = "gaussian",
                 opt = "exact", lambda = lambda)

# MSE
error(model)

new_n = 50
new_x = matrix(runif(new_n*d, 0, 1), nrow = new_n, ncol = d)
new_y = as.vector(sin(2*pi*rowMeans(new_x)^3) + rnorm(new_n, 0, 0.1))
new_data = data.frame(new_x, y = new_y)

# PMSE
error(model, data_new = new_data)

}
\seealso{
\code{\link{summary.krr}}, \code{\link{plot.krr}}, \code{\link{predict.krr}}
}
