\HeaderA{kde2d}{Two-Dimensional Kernel Density Estimation}{kde2d}
\keyword{dplot}{kde2d}
\begin{Description}\relax
Two-dimensional kernel density estimation with an axis-aligned
bivariate normal kernel, evaluated on a square grid.
\end{Description}
\begin{Usage}
\begin{verbatim}
kde2d(x, y, h, n = 25, lims = c(range(x), range(y)))
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] x coordinate of data

\item[\code{y}] y coordinate of data

\item[\code{h}] vector of bandwidths for x and y directions. Defaults to
normal reference bandwidth (see \code{\LinkA{bandwidth.nrd}{bandwidth.nrd}}).

\item[\code{n}] Number of grid points in each direction.

\item[\code{lims}] The limits of the rectangle covered by the grid as \code{c(xl, xu, yl, yu)}.

\end{ldescription}
\end{Arguments}
\begin{Value}
A list of three components.

\begin{ldescription}
\item[\code{x, y}] The x and y coordinates of the grid points, vectors of length \code{n}.

\item[\code{z}] An \code{n}  x  \code{n} matrix of the evaluated density.

\end{ldescription}
\end{Value}
\begin{References}\relax
Venables, W. N. and Ripley, B. D. (2002)
\emph{Modern Applied Statistics with S.} Fourth edition.  Springer.
\end{References}
\begin{Examples}
\begin{ExampleCode}
attach(geyser)
plot(duration, waiting, xlim = c(0.5,6), ylim = c(40,100))
f1 <- kde2d(duration, waiting, n = 50, lims = c(0.5, 6, 40, 100))
image(f1, zlim = c(0, 0.05))
f2 <- kde2d(duration, waiting, n = 50, lims = c(0.5, 6, 40, 100),
            h = c(width.SJ(duration), width.SJ(waiting)) )
image(f2, zlim = c(0, 0.05))
persp(f2, phi = 30, theta = 20, d = 5)

plot(duration[-272], duration[-1], xlim = c(0.5, 6),
     ylim = c(1, 6),xlab = "previous duration", ylab = "duration")
f1 <- kde2d(duration[-272], duration[-1],
            h = rep(1.5, 2), n = 50, lims = c(0.5, 6, 0.5, 6))
contour(f1, xlab = "previous duration",
        ylab = "duration", levels  =  c(0.05, 0.1, 0.2, 0.4) )
f1 <- kde2d(duration[-272], duration[-1],
            h = rep(0.6, 2), n = 50, lims = c(0.5, 6, 0.5, 6))
contour(f1, xlab = "previous duration",
        ylab = "duration", levels  =  c(0.05, 0.1, 0.2, 0.4) )
f1 <- kde2d(duration[-272], duration[-1],
            h = rep(0.4, 2), n = 50, lims = c(0.5, 6, 0.5, 6))
contour(f1, xlab = "previous duration",
        ylab = "duration", levels  =  c(0.05, 0.1, 0.2, 0.4) )
detach("geyser")
\end{ExampleCode}
\end{Examples}

