\HeaderA{loessFit}{Fast Simple Loess}{loessFit}
\keyword{models}{loessFit}
\begin{Description}\relax
A fast version of locally weighted regression when there is only one x-variable and only the fitted values and residuals are required.
\end{Description}
\begin{Usage}
\begin{verbatim}
loessFit(y, x, weights=NULL, span=0.3, bin=0.01/(2-is.null(weights)), iterations=4)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{y}] numeric vector of response values.  Missing values are allowed.
\item[\code{x}] numeric vector of predictor values  Missing values are allowed.
\item[\code{weights}] numeric vector of non-negative weights.  Missing values are allowed.
\item[\code{span}] numeric parameter between 0 and 1 specifying proportion of data to be used in the local regression moving window.
Larger numbers give smoother fits.
\item[\code{bin}] numeric value between 0 and 1 giving the proportion of the data which can be grouped in a single bin when doing local regression fit.
\code{bin=0} forces an exact local regression fit with no interpolation.
\item[\code{iterations}] number of iterations of loess fit
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
This function is a low-level equivalent to \code{lowess} in the stats package if \code{weights} is null and to \code{loess} otherwise.
It is intended to give a streamlined common interface to the two functions for use in \code{\LinkA{normalizeWithinArrays}{normalizeWithinArrays}}.
Note that, while \code{lowess} has fewer features than \code{loess}, it is faster, uses less memory and uses a more accurate interpolation scheme than \code{loess}, so it is desirable to use \code{lowess} whenever the extra features of \code{loess} are not required.

There are a couple of key differences between \code{loessFit} and \code{loess} when \code{weights} is non-NULL.
One difference is that \code{loessFit} returns a linear regression fit if there are insufficient observations to estimate the loess curve, whereas \code{loess} will return an error.

The arguments \code{span}, \code{cell} and \code{iterations} here have the same meaning as in \code{loess}.
\code{span} is equivalent to the argument \code{f} of \code{lowess} and \code{iterations} is equivalent to \code{iter+1}.
The parameter \code{bin} is intended to give a simple uniform interface to the \code{delta} argument of \code{lowess} and the \code{cell} argument of \code{loess}.
\code{bin} translates to \code{delta=bin*diff(range(x))} in a call to \code{lowess} or to \code{cell=bin/span} in a call to \code{loess}.

Unlike \code{lowess}, \code{loessFit} returns values in original rather than sorted order.
Also unlike \code{lowess}, \code{loessFit} allows missing values, the treatment being analogous to \code{na.exclude}.
\end{Details}
\begin{Value}
A list with components
\begin{ldescription}
\item[\code{fitted}] numeric vector of same length as \code{y} giving the loess fit
\item[\code{residuals}] numeric vector of same length as \code{x} giving residuals from the fit
\end{ldescription}
\end{Value}
\begin{Author}\relax
Gordon Smyth
\end{Author}
\begin{SeeAlso}\relax
An overview of LIMMA functions for normalization is given in \LinkA{05.Normalization}{05.Normalization}.

See also \code{\LinkA{lowess}{lowess}} and \code{\LinkA{loess}{loess}} in the stats package.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
y <- rnorm(1000)
x <- rnorm(1000)
w <- rep(1,1000)
# The following are equivalent apart from execution time
# and interpolation inaccuracies
system.time(fit <- loessFit(y,x)$fitted)
system.time(fit <- loessFit(y,x,w)$fitted)
system.time(fit <- fitted(loess(y~x,weights=w,span=0.3,family="symmetric",iterations=4)))
# The same but with sorted x-values
system.time(fit <- lowess(x,y,f=0.3)$y)
\end{ExampleCode}
\end{Examples}


