Nonparametric estimation of regression functions and their derivatives with kernel regression estimators and automatically adapted local plug-in bandwidth function.
lokerns(x, …)# S3 method for default
lokerns(x, y=NULL, deriv = 0, n.out=300, x.out=NULL, x.inOut = TRUE,
korder = deriv + 2, hetero=FALSE, is.rand=TRUE,
inputb= is.numeric(bandwidth) && bandwidth > 0,
m1 = 400, xl=NULL, xu=NULL,
s=NULL, sig=NULL, bandwidth=NULL, trace.lev = 0, …)
# S3 method for formula
lokerns(formula, data, subset, na.action, …)
vector of design points, not necessarily ordered.
vector of observations of the same length as x
.
order of derivative of the regression function to be estimated. Only deriv=0,1,2 are allowed for automatic smoothing, whereas deriv=0,1,2,3,4 is possible when smoothing with an input bandwidth array. The default value is deriv=0.
number of output design points where the function has to
be estimated; default is n.out=300
.
vector of output design points where the function has to be estimated. The default is an equidistant grid of n.out points from min(x) to max(x).
logical or character string indicating if x.out
should contain the input x
values.
Note that this argument did not exist, equivalently to being
FALSE
, up to lokern version 1.0-9
.
In order for residuals()
or fitted()
methods to be applicable, it must be TRUE
or a character
string specifying one of the methods
s of
seqXtend
(package sfsmisc). The default,
TRUE
corresponds to method "aim"
.
nonnegative integer giving the kernel order \(k\); it defaults to
korder = deriv+2
or \(k = \nu + 2\) where \(k - \nu\)
must be even. The maximal possible values are for automatic
smoothing, \(k \le 4\), whereas for smoothing with input
bandwidth array, \(k \le 6\).
logical: if TRUE, heteroscedastic error variables are assumed for variance estimation, if FALSE the variance estimation is optimized for homoscedasticity. Default value is hetero=FALSE.
logical: if TRUE
(default), random x are assumed and the
s-array of the convolution estimator is computed as smoothed
quantile estimators in order to adapt this variability. If FALSE,
the s-array is choosen as mid-point sequences as the classical
Gasser-Mueller estimator, this will be better for equidistant and
fixed design.
logical: if true, a local input bandwidth array is used; if
FALSE
(by default when bandwidth
is not specified), a
data-adaptive local plug-in bandwidths array is calculated and used.
integer, the number of grid points for integral approximation when estimating the plug-in bandwidth. The default, 400, may be increased if a very large number of observations are available.
numeric (scalars), the lower and upper bounds for integral
approximation and variance estimation when estimating the plug-in
bandwidth. By default (when xl
and xu
are not specified),
the 87% middle part of \([xmin,xmax]\) is used.
s-array of the convolution kernel estimator. If it is not given by input
it is calculated as midpoint-sequence of the ordered design points for
is.rand=FALSE
or as quantiles estimators of the design density
for is.rand=TRUE
.
variance of the error variables. If it is not given by
input or if hetero=TRUE
it is calculated by a
nonparametric variance estimator.
local bandwidth array for kernel regression estimation. If it is
not given by input or if inputb=FALSE
a data-adaptive local
plug-in bandwidth array is used instead.
integer indicating how much the internal (Fortran
level) computations should be “traced”, i.e., be reported.
The default, 0
, does not print anything.
a formula
of the form y ~ pred
,
specifying the response variable y
and predictor variable
pred
which must be in data
.
an optional matrix or data frame (or similar: see
model.frame
) containing the variables in the
formula formula
. By default the variables are taken from
environment(formula)
.
an optional vector specifying a subset of observations to be used.
a function which indicates what should happen when
the data contain NA
s. Defaults to
getOption("na.action")
.
for the formula
method: Optional arguments all
passed to lokerns.default()
.
an object of class(es) c("lokerns", "KernS")
, which is
a list including used parameters and estimator, containing among others
vector of ordered design points.
vector of observations ordered with respect to x.
local bandwidth array which was used for kernel regression estimation.
vector of ordered output design points.
vector of estimated regression function or its derivative
(at x.out
).
variance estimation which was used for calculating the plug-in bandwidths if hetero=TRUE (default) and either inputb=FALSE (default) or is.rand=TRUE (default).
derivative of the regression function which was estimated.
order of the kernel function which was used.
lower bound for integral approximation and variance estimation.
upper bound for integral approximation and variance estimation.
vector of midpoint values used for the convolution kernel regression estimator.
This function calls an efficient and fast algorithm for automatically adaptive nonparametric regression estimation with a kernel method.
Roughly spoken, the method performs a local averaging of the observations when estimating the regression function. Analogously, one can estimate derivatives of small order of the regression function. Crucial for the kernel regression estimation used here is the choice the local bandwidth array. Too small bandwidths will lead to a wiggly curve, too large ones will smooth away important details. The function lokerns calculates an estimator of the regression function or derivatives of the regression function with an automatically chosen local plugin bandwidth function. It is also possible to use a local bandwidth array which are specified by the user.
Main ideas of the plugin method are to estimate the optimal bandwidths by estimating the asymptotically optimal mean squared error optimal bandwidths. Therefore, one has to estimate the variance for homoscedastic error variables and a functional of a smooth variance function for heteroscedastic error variables, respectively. Also, one has to estimate an integral functional of the squared \(k\)-th derivative of the regression function (\(k=\code{korder}\)) for the global bandwidth and the squared \(k\)-th derivative itself for the local bandwidths.
Some more details are in glkerns
.
All the references in glkerns
.
glkerns
for global bandwidth computation.
plot.KernS
documents all the methods for "KernS"
classed objects.
# NOT RUN {
data(cars)
lofit <- lokerns(dist ~ speed, data=cars)
lofit # print() method
# }
# NOT RUN {
if(require("sfsmisc")) {
TA.plot(lofit)
} else { plot(residuals(lofit) ~ fitted(lofit)); abline(h = 0, lty=2) }
qqnorm(residuals(lofit), ylab = "residuals(lofit)")
## nice simple plot of data + smooth
plot(lofit)
(sb <- summary(lofit$bandwidth))
op <- par(fg = "gray90", tcl = -0.2, mgp = c(3,.5,0))
plot(lofit$band, ylim=c(0,3*sb["Max."]), type="h",#col="gray90",
ann = FALSE, axes = FALSE)
boxplot(lofit$bandwidth, add = TRUE, at = 304, boxwex = 8,
col = "gray90",border="gray", pars = list(axes = FALSE))
axis(4, at = c(0,pretty(sb)), col.axis = "gray")
par(op)
par(new=TRUE)
plot(dist ~ speed, data = cars,
main = "Local Plug-In Bandwidth Vector")
lines(lofit, col=4, lwd=2)
mtext(paste("bandwidth in [",
paste(format(sb[c(1,6)], dig = 3),collapse=","),
"]; Median b.w.=",formatC(sb["Median"])))
# }
Run the code above in your browser using DataLab