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 Continous Response Data" in the S-PLUS Guide to Statistical and Mathematical Analysis.


SEE ALSO:
density , loess , smooth , supsmu .

EXAMPLES:
x <- rnorm(100) ; eps <- rnorm(100,0,.1)
y <- sin(x) + eps
plot(x,y)
# get a density estimate for x, and add it to the plot.
ks1 <- ksmooth(x,ker="parzen",bandwidth=1,n.points=50)
lines(ks1)
# Get a regression estimate for y vs. x.
ks2 <- ksmooth(x,y,"normal",ban=.5,n=50)
lines(ks2,lty=2)