\Header{locpoly}{Estimate Functions Using Local Polynomials}
\keyword{smooth}{locpoly}
\keyword{regression}{locpoly}
\begin{Description}\relax
Estimates a probability density function,  
regression function or their derivatives
using local polynomials. A fast binned implementation
over an equally-spaced grid is used.
\end{Description}
\begin{Usage}
\begin{verbatim}
locpoly(x, y, drv=0, degree=<<see below>>, kernel="normal", 
        bandwidth, gridsize=401, bwdisc=25, 
        range.x=<<see below>>,  binned=FALSE, truncate=TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] vector of x data.
Missing values are not accepted.

\item[\code{bandwidth}] the kernel bandwidth smoothing parameter.
It may be a single number or an array having
length \code{gridsize}, representing a bandwidth
that varies according to the location of
estimation.

\item[\code{y}] vector of y data.
This must be same length as \code{x}, and
missing values are not accepted.

\item[\code{drv}] order of derivative to be estimated.

\item[\code{degree}] degree of local polynomial used. Its value
must be greater than or equal to the value
of \code{drv}. The default value is of \code{degree} is
\code{drv} + 1.

\item[\code{kernel}] \code{"normal"} - the Gaussian density function.

\item[\code{gridsize}] number of equally-spaced grid points over which the 
function is to be estimated.

\item[\code{bwdisc}] number of logarithmically-equally-spaced bandwidths
on which \code{bandwidth} is discretised, to speed up
computation.

\item[\code{range.x}] vector containing the minimum and maximum values of \code{x} at which to
compute the estimate.

\item[\code{binned}] logical flag: if \code{TRUE}, then \code{x} and \code{y} are taken to be grid counts
rather than raw data. 

\item[\code{truncate}] logical flag: if \code{TRUE}, data with \code{x} values outside the range specified
by \code{range.x} are ignored.

\end{ldescription}
\end{Arguments}
\begin{Value}
if \code{y} is specified, a local polynomial regression estimate of 
E[Y|X] (or its derivative) is computed.
If \code{y} is missing, a local polynomial estimate of the density
of \code{x} (or its derivative) is computed.


a list containing the following components:

\begin{ldescription}
\item[\code{x}] vector of sorted x values at which the estimate was computed.

\item[\code{y}] vector of smoothed estimates for either the density or the regression
at the corresponding \code{x}.

\end{ldescription}
\end{Value}
\begin{Section}{Details}
Local polynomial fitting with a kernel weight is used to
estimate either a density, regression function or their
derivatives. In the case of density estimation, the 
data are binned and the local fitting procedure is applied to 
the bin counts. In either case, binned approximations over
an equally-spaced grid is used for fast computation. The
bandwidth may be either scalar or a vector of length
\code{gridsize}.
\end{Section}
\begin{References}\relax
Wand, M. P. and Jones, M. C. (1995).
\emph{Kernel Smoothing.}
Chapman and Hall, London.
\end{References}
\begin{SeeAlso}\relax
\code{\Link{bkde}}, \code{\Link{density}}, \code{\Link{dpill}},
\code{\Link{ksmooth}}, \code{\Link{loess}}, \code{\Link{smooth}},
\code{\Link{supsmu}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
data(geyser, package="MASS")
x <- geyser$duration
est <- locpoly(x,bandwidth=0.25)
plot(est,type="l")
# local linear density estimate
y <- geyser$waiting
plot(x,y)
fit <- locpoly(x,y,bandwidth=0.25)
lines(fit)
# local linear regression estimate
\end{ExampleCode}
\end{Examples}

