\HeaderA{kernel}{Smoothing Kernel Objects}{kernel}
\aliasA{bandwidth.kernel}{kernel}{bandwidth.kernel}
\aliasA{df.kernel}{kernel}{df.kernel}
\aliasA{is.tskernel}{kernel}{is.tskernel}
\aliasA{plot.tskernel}{kernel}{plot.tskernel}
\keyword{ts}{kernel}
\begin{Description}\relax
The \code{"tskernel"} class is designed to represent discrete
symmetric normalized smoothing kernels.  These kernels can be used to
smooth vectors, matrices, or time series objects.

There are \code{\LinkA{print}{print}}, \code{\LinkA{plot}{plot}} and \code{\LinkA{[}{[}}
methods for these kernel objects.
\end{Description}
\begin{Usage}
\begin{verbatim}
kernel(coef, m, r, name)

df.kernel(k)
bandwidth.kernel(k)
is.tskernel(k)
## S3 method for class 'tskernel':
plot(x, type = "h", xlab = "k", ylab = "W[k]",
     main = attr(x,"name"), ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{coef}] the upper half of the smoothing kernel coefficients
(including coefficient zero) \emph{or} the name of a kernel
(currently \code{"daniell"}, \code{"dirichlet"}, \code{"fejer"} or
\code{"modified.daniell"}.
\item[\code{m}] the kernel dimension(s).  When \code{m}
has length larger than one, it means the convolution of
kernels of dimension \code{m[j]}, for \code{j in 1:length(m)}.
Currently this is supported only for the named "*daniell" kernels.
\item[\code{name}] the name the kernel will be called.
\item[\code{r}] the kernel order for a Fejer kernel.
\item[\code{k,x}] a \code{"tskernel"} object.
\item[\code{type, xlab, ylab, main, ...}] arguments passed to
\code{\LinkA{plot.default}{plot.default}}.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{kernel} is used to construct a general kernel or named specific
kernels.  The modified Daniell kernel halves the end coefficients (as
used by S-PLUS).

The \code{\LinkA{[}{[}} method allows natural indexing of kernel objects
with indices in \code{(-m) : m}.  The normalization is such that for
\code{k <- kernel(*)}, \code{sum(k[ -k\$m : k\$m ])} is one.

\code{df.kernel} returns the \sQuote{equivalent degrees of freedom} of
a smoothing kernel as defined in Brockwell and Davis (1991), page
362, and \code{bandwidth.kernel} returns the equivalent bandwidth as
defined in Bloomfield (1976), p. 201, with a continuity correction.
\end{Details}
\begin{Value}
\code{kernel()} returns an object of class \code{"tskernel"} which is
basically a list with the two components \code{coef} and the kernel
dimension \code{m}.  An additional attribute is \code{"name"}.
\end{Value}
\begin{Author}\relax
A. Trapletti; modifications by B.D. Ripley
\end{Author}
\begin{References}\relax
Bloomfield, P. (1976)
\emph{Fourier Analysis of Time Series: An Introduction.}
Wiley.

Brockwell, P.J. and Davis, R.A. (1991)
\emph{Time Series: Theory and Methods.}
Second edition. Springer, pp. 350--365.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{kernapply}{kernapply}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
require(graphics)

## Demonstrate a simple trading strategy for the
## financial time series German stock index DAX.
x <- EuStockMarkets[,1]
k1 <- kernel("daniell", 50)  # a long moving average
k2 <- kernel("daniell", 10)  # and a short one
plot(k1)
plot(k2)
x1 <- kernapply(x, k1)
x2 <- kernapply(x, k2)
plot(x)
lines(x1, col = "red")    # go long if the short crosses the long upwards
lines(x2, col = "green")  # and go short otherwise

## More interesting kernels
kd <- kernel("daniell", c(3,3))
kd # note the unusual indexing
kd[-2:2]
plot(kernel("fejer", 100, r=6))
plot(kernel("modified.daniell", c(7,5,3)))

# Reproduce example 10.4.3 from Brockwell and Davis (1991)
spectrum(sunspot.year, kernel=kernel("daniell", c(11,7,3)), log="no")
\end{ExampleCode}
\end{Examples}

