The R Project SVN R

Rev

Rev 87163 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/stats/man/kernel.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
88563 hornik 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27497 ripley 6
\name{kernel}
56186 murdoch 7
\alias{kernel}
27497 ripley 8
\alias{bandwidth.kernel}
9
\alias{df.kernel}
10
\alias{is.tskernel}
34932 maechler 11
\alias{plot.tskernel}
27497 ripley 12
\title{Smoothing Kernel Objects}
13
\description{
14
  The \code{"tskernel"} class is designed to represent discrete
15
  symmetric normalized smoothing kernels.  These kernels can be used to
16
  smooth vectors, matrices, or time series objects.
34932 maechler 17
 
18
  There are \code{\link{print}}, \code{\link{plot}} and \code{\link{[}}
19
  methods for these kernel objects.
27497 ripley 20
}
21
\usage{
53718 ripley 22
kernel(coef, m = 2, r, name)
27497 ripley 23
 
24
df.kernel(k)
25
bandwidth.kernel(k)
26
is.tskernel(k)
50416 ripley 27
 
34932 maechler 28
\method{plot}{tskernel}(x, type = "h", xlab = "k", ylab = "W[k]",
29
     main = attr(x,"name"), \dots)
27497 ripley 30
}
31
\arguments{
32
  \item{coef}{the upper half of the smoothing kernel coefficients
34932 maechler 33
    (including coefficient zero) \emph{or} the name of a kernel
27497 ripley 34
    (currently \code{"daniell"}, \code{"dirichlet"}, \code{"fejer"} or
74363 maechler 35
    \code{"modified.daniell"}).}
53722 ripley 36
  \item{m}{the kernel dimension(s) if \code{coef} is a name.  When \code{m}
34932 maechler 37
    has length larger than one, it means the convolution of
38
    kernels of dimension \code{m[j]}, for \code{j in 1:length(m)}.
85976 hornik 39
    Currently this is supported only for the named \code{"*daniell"} kernels.}
34932 maechler 40
  \item{name}{the name the kernel will be called.}
87163 maechler 41
  \item{r}{the kernel order for a \I{Dirichlet} or \I{Fejer} kernel.}
53722 ripley 42
  \item{k, x}{a \code{"tskernel"} object.}
34932 maechler 43
  \item{type, xlab, ylab, main, \dots}{arguments passed to
44
    \code{\link{plot.default}}.}
27497 ripley 45
}
46
\details{
47
  \code{kernel} is used to construct a general kernel or named specific
85977 hornik 48
  kernels.  The modified \I{Daniell} kernel halves the end coefficients.
34932 maechler 49
 
50
  The \code{\link{[}} method allows natural indexing of kernel objects
51
  with indices in \code{(-m) : m}.  The normalization is such that for
52
  \code{k <- kernel(*)}, \code{sum(k[ -k$m : k$m ])} is one.
53
 
42961 ripley 54
  \code{df.kernel} returns the \sQuote{equivalent degrees of freedom} of
85953 hornik 55
  a smoothing kernel as defined in
88563 hornik 56
  \bibcitet{|R:Brockwell+Davis:1991|page 362},
85953 hornik 57
  and \code{bandwidth.kernel} returns the equivalent bandwidth as
88563 hornik 58
  defined in \bibcitet{|R:Bloomfield:1976|page 201},
85953 hornik 59
  with a continuity correction.
27497 ripley 60
}
61
\value{
34932 maechler 62
  \code{kernel()} returns an object of class \code{"tskernel"} which is
63
  basically a list with the two components \code{coef} and the kernel
64
  dimension \code{m}.  An additional attribute is \code{"name"}.
27497 ripley 65
}
66
\author{A. Trapletti; modifications by B.D. Ripley}
67
\seealso{
68
    \code{\link{kernapply}}
69
}
70
\references{
88563 hornik 71
  \bibshow{*}
27497 ripley 72
}
73
\examples{
41508 ripley 74
require(graphics)
75
 
34932 maechler 76
## Demonstrate a simple trading strategy for the
77
## financial time series German stock index DAX.
78
x <- EuStockMarkets[,1]
27497 ripley 79
k1 <- kernel("daniell", 50)  # a long moving average
80
k2 <- kernel("daniell", 10)  # and a short one
34932 maechler 81
plot(k1)
27497 ripley 82
plot(k2)
83
x1 <- kernapply(x, k1)
84
x2 <- kernapply(x, k2)
85
plot(x)
86
lines(x1, col = "red")    # go long if the short crosses the long upwards
87
lines(x2, col = "green")  # and go short otherwise
88
 
34932 maechler 89
## More interesting kernels
61160 ripley 90
kd <- kernel("daniell", c(3, 3))
34932 maechler 91
kd # note the unusual indexing
92
kd[-2:2]
61160 ripley 93
plot(kernel("fejer", 100, r = 6))
34932 maechler 94
plot(kernel("modified.daniell", c(7,5,3)))
95
 
42819 murdoch 96
# Reproduce example 10.4.3 from Brockwell and Davis (1991)
61160 ripley 97
spectrum(sunspot.year, kernel = kernel("daniell", c(11,7,3)), log = "no")
27497 ripley 98
}
99
\keyword{ts}