% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/estimators.R
\name{werm_huber}
\alias{werm_huber}
\title{Fit a WERM changepoint model with Huber loss}
\usage{
werm_huber(
  X,
  y,
  num_chgpts,
  delta = 1L,
  search_method = "efficient",
  fit_intercept = TRUE,
  epsilon = 1.35,
  max_iter = 100L,
  tol = 1e-05,
  penalty = "none",
  alpha = 0
)
}
\arguments{
\item{X}{Numeric matrix of shape \verb{(n, p)}. Observations must be in order.}

\item{y}{Numeric vector of length \code{n}. Ordered responses.}

\item{num_chgpts}{Integer. Number of changepoints to detect.}

\item{delta}{Integer. Minimum gap between candidate changepoints during
search. Default \code{1}. Rule of thumb: \code{max(1L, as.integer(0.05 * nrow(X)))}.}

\item{search_method}{Character. \code{"efficient"} (default) or \code{"brute_force"}.}

\item{fit_intercept}{Logical. Whether each segment model includes an
intercept. Default \code{TRUE}.}

\item{epsilon}{Numeric. Huber transition parameter. Default \code{1.35}
(95 \% Gaussian efficiency).}

\item{max_iter}{Integer. Maximum L-BFGS-B iterations. Default \code{100}.}

\item{tol}{Numeric. Gradient-norm tolerance. Default \code{1e-5}.}

\item{penalty}{Character. \code{"none"} (default), \code{"l1"}, or \code{"l2"}.}

\item{alpha}{Numeric. Penalty strength. Default \code{0}.}
}
\value{
An object of class \code{c("werm_huber", "werm_fit")}.
Same elements as \code{\link[=werm_least_squares]{werm_least_squares()}}.
}
\description{
Like \code{\link[=werm_least_squares]{werm_least_squares()}} but uses the Huber loss, which is more robust
to outliers than squared loss.
}
\examples{
# Limit BLAS/OpenMP threads so example CPU time stays proportional to
# elapsed time on multicore CRAN machines.
Sys.setenv(
  OMP_NUM_THREADS = "1",
  OPENBLAS_NUM_THREADS = "1",
  MKL_NUM_THREADS = "1",
  BLAS_NUM_THREADS = "1"
)

if (nzchar(Sys.getenv("RETICULATE_PYTHON")) &&
    weightederm_examples_available("WERMHuber")) {
  set.seed(2)
  n <- 12L; p <- 1L; true_cp <- 6L
  X <- matrix(rnorm(n * p), n, p)
  y <- c(
    X[1:true_cp, , drop = FALSE] \%*\% 3,
    X[(true_cp + 1L):n, , drop = FALSE] \%*\% -3
  ) + rnorm(n, sd = 0.03)
  fit <- werm_huber(X, y, num_chgpts = 1L, delta = 1L,
                    fit_intercept = FALSE, max_iter = 5L, tol = 1e-3)
  fit$changepoints
}

}
