\Header{isoreg}{Isotonic / Monotone Regression}
\keyword{regression}{isoreg}
\keyword{smooth}{isoreg}
\begin{Description}\relax
Compute the isotonic (monotonely increasing nonparametric) least
squares regression which is piecewise constant.\end{Description}
\begin{Usage}
\begin{verbatim}
isoreg(x, y = NULL)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x, y}] in \code{isoreg}, coordinate vectors of the regression
points.  Alternatively a single \dQuote{plotting} structure can be
specified: see \code{\Link{xy.coords}}.

\item[\code{...}] potentially further arguments passed to methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The algorithm determines the convex minorant \eqn{m(x)}{} of the
\emph{cumulative} data (i.e., \code{cumsum(y)}) which is piecewise
linear and the result is \eqn{m'(x)}{}, a step function with level
changes at locations where the convex \eqn{m(x)}{} touches the
cumulative data polygon and changes slope.\\
\code{\Link{as.stepfun}()} in \pkg{stepfun}
returns a \code{\Link{stepfun}} object
which can be more parsimonious.\end{Details}
\begin{Value}
\code{isoreg()} returns an object of class \code{isoreg} which is
basically a list with components
\begin{ldescription}
\item[\code{x}] original (constructed) abscissa values \code{x}.
\item[\code{y}] corresponding y values.
\item[\code{yf}] fitted values corresponding to \emph{ordered} x values.
\item[\code{yc}] cumulative y values corresponding to \emph{ordered} x values.
\item[\code{iKnots}] integer vector giving indices where the fitted curve jumps,
i.e., where the convex minorant has kinks.
\item[\code{isOrd}] logical indicating if original x values were ordered
increasingly already.
\item[\code{ord}] \code{if(!isOrd)}: integer permutation \code{\Link{order}(x)} of
\emph{original} \code{x}.
\item[\code{call}] the \code{\Link{call}} to \code{isoreg()} used.
\end{ldescription}
\end{Value}
\begin{Note}\relax
The code should be improved to accept \emph{weights} additionally and
solve the corresponding weighted least squares problem.\\
\dQuote{Patches are welcome!}\end{Note}
\begin{References}\relax
Barlow, R. E., Bartholomew, D. J., Bremner, J. M., and Brunk, H. D. (1972)
\emph{Statistical inference under order restrictions}; Wiley, London.

Robertson, T., Wright,F. T. and Dykstra, R. L. (1988)
\emph{Order Restricted Statistical Inference}; Wiley, New York.\end{References}
\begin{SeeAlso}\relax
the plotting method \code{\Link{plot.isoreg}} with more examples;
\code{\Link{isoMDS}()} from the \pkg{MASS} package internally
uses isotonic regression.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
(ir <- isoreg(c(1,0,4,3,3,5,4,2,0)))
plot(ir, plot.type = "row")

(ir3 <- isoreg(y3 <- c(1,0,4,3,3,5,4,2, 3)))# last "3", not "0"
library(stepfun)
(fi3 <- as.stepfun(ir3))
(ir4 <- isoreg(1:10, y4 <- c(5, 9, 1:2, 5:8, 3, 8)))
cat("R^2 =", formatC(sum(residuals(ir4)^2) / (9*var(y4)), dig=2),"\n")
\end{ExampleCode}
\end{Examples}

