Densities or Regressions Using Kernel Smoothers

DESCRIPTION:

Estimates a probability density or performs scatterplot smoothing using kernel estimates.

USAGE:

ksmooth(x, y=NULL, kernel="box", bandwidth=0.5, range.x=range(x), 
        n.points=length(x), x.points=<<see below>>) 

REQUIRED ARGUMENTS:

x
vector of x data. Missing values are not accepted.

OPTIONAL ARGUMENTS:

y
vector of y data. This must be same length as x, and missing values are not accepted.
kernel
character string which determines the smoothing kernel. kernel can be:

"box" - a rectangular box (the default).

"triangle" - a triangle (a box convolved with itself).

"parzen" - the parzen function (a box convolved with a triangle).

"normal" - the gaussian density function.
bandwidth
the kernel bandwidth smoothing parameter. All kernels are scaled so the upper and lower quartiles of the kernel (viewed as a probability density) are +/- 0.25 when bandwidth is 1. Larger values of bandwidth make smoother estimates, smaller values of bandwidth make less smooth estimates.
range.x
vector containing the minimum and maximum values of x at which to compute the estimate. range.x can not be given if x.points is supplied.
n.points
number of points to smooth in the interval range.x. n.points can not be given if x.points is supplied.
x.points
vector specifying where the kernel estimate is computed. If not specified, this is set to seq(range.x[1], range.x[2], length=n.points) for density estimates and to x for regression estimates. Note that if x.points is supplied then neither range.x nor n.points can be supplied.

VALUE:

if y is specified, a kernel regression estimate of E[Y|X] is computed. If y is missing, a density estimate of x is computed.

a list containing the following components:
x
vector of sorted x values at which the kernel estimate was computed.
y
vector of smoothed estimates for either the density or the regression at the corresponding x.

REFERENCES:

Silverman, B. W. (1986). Density Estimation for Statistics and Data Analysis. Chapman and Hall, London.

Watson, G. S. (1966). Smooth regression analysis. Sankha, Ser. A 26, 359-378.

The chapter "Regression and Smoothing for Continuous Response Data" in the S-PLUS Guide to Statistics.

SEE ALSO:

, , , .

EXAMPLES:

# Make up some data:
x <- rnorm(100); eps <- rnorm(100, 0, .1) 
y <- sin(x) + eps 

# A kernel density estimate for x
ksmx <- ksmooth(x, ker="parzen", bandwidth=1, n.points=50)
plot(ksmx, type='l')
rugplot(x)  # Add x locations at bottom of plot

# A kernel regression estimate for y on x. 
ksmxy <- ksmooth(x, y, "normal", ban=.5, n=50) 
plot(x, y) 
lines(ksmxy)