\HeaderA{medpolish}{Median Polish of a Matrix}{medpolish}
\keyword{robust}{medpolish}
\begin{Description}\relax
Fits an additive model using Tukey's \emph{median polish} procedure.
\end{Description}
\begin{Usage}
\begin{verbatim}
medpolish(x, eps = 0.01, maxiter = 10, trace.iter = TRUE,
          na.rm = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] a numeric matrix.
\item[\code{eps}] real number greater than 0. A tolerance for convergence:
see \sQuote{Details}.
\item[\code{maxiter}] the maximum number of iterations
\item[\code{trace.iter}] logical. Should progress in convergence be reported?
\item[\code{na.rm}] logical. Should missing values be removed?
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The model fitted is additive (constant + rows + columns). The
algorithm works by alternately removing the row and column medians,
and continues until the proportional reduction in the sum
of absolute residuals is less than \code{eps}
or until there have been \code{maxiter} iterations.
The sum of absolute residuals is printed at
each iteration of the fitting process, if \code{trace.iter} is \code{TRUE}.
If \code{na.rm} is \code{FALSE} the presence of any \code{NA} value in
\code{x} will cause an error, otherwise \code{NA} values are ignored.

\code{medpolish} returns an object of class \code{medpolish} (see below).
There are printing and plotting methods for this
class, which are invoked via by the generics
\code{\LinkA{print}{print}} and \code{\LinkA{plot}{plot}}.
\end{Details}
\begin{Value}
An object of class \code{medpolish} with the following named components:
\begin{ldescription}
\item[\code{overall}] the fitted constant term.
\item[\code{row}] the fitted row effects.
\item[\code{col}] the fitted column effects.
\item[\code{residuals}] the residuals.
\item[\code{name}] the name of the dataset.
\end{ldescription}
\end{Value}
\begin{References}\relax
Tukey, J. W. (1977).
\emph{Exploratory Data Analysis},
Reading Massachusetts: Addison-Wesley.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{median}{median}}; \code{\LinkA{aov}{aov}} for a \emph{mean}
instead of \emph{median} decomposition.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
require(graphics)

## Deaths from sport parachuting;  from ABC of EDA, p.224:
deaths <-
    rbind(c(14,15,14),
          c( 7, 4, 7),
          c( 8, 2,10),
          c(15, 9,10),
          c( 0, 2, 0))
dimnames(deaths) <- list(c("1-24", "25-74", "75-199", "200++", "NA"),
                         paste(1973:1975))
deaths
(med.d <- medpolish(deaths))
plot(med.d)
## Check decomposition:
all(deaths ==
    med.d$overall + outer(med.d$row,med.d$col, "+") + med.d$residuals)
\end{ExampleCode}
\end{Examples}

