Kernel regression
- 2 min read
- 6 chapters
Related on Lably
Through Statistics, Random variable and Python (programming language)
In any nonparametric regression, the conditional expectation of a variable relative to a variable may be written:
where is an unknown function.
Nadaraya–Watson kernel regression
Nadaraya and Watson, both in 1964, proposed to estimate as a locally weighted average, using a kernel as a weighting function. The Nadaraya–Watson estimator is:
where is a kernel with a bandwidth such that is of order at least 1, that is .
Derivation
Starting with the definition of conditional expectation,
we estimate the joint distributions f(x,y) and f(x) using kernel density estimation with a kernel K:
We get:
which is the Nadaraya–Watson estimator.
Sign in to keep reading
5 more chapters are free with a Lably account.
Priestley–Chao kernel estimator
where is the bandwidth (or smoothing parameter).
Gasser–Müller kernel estimator
where
Example
This example is based upon Canadian cross-section wage data consisting of a random sample taken from the 1971 Canadian Census Public Use Tapes for male individuals having common education (grade 13). There are 205 observations in total.
The figure to the right shows the estimated regression function using a second order Gaussian kernel along with asymptotic variability bounds.
Script for example
The following commands of the R programming language use the npreg() function to deliver optimal smoothing and to create the figure given above. These commands can be entered at the command prompt via cut and paste.
<span id="mwZg"></span><span class="nf" id="mwZw">install.packages</span><span class="p" id="mwaA">(</span><span class="s" id="mwaQ">"np"</span><span class="p" id="mwag">)</span> <span class="nf" id="mwaw">library</span><span class="p" id="mwbA">(</span><span class="n" id="mwbQ">np</span><span class="p" id="mwbg">)</span><span class="w" id="mwbw"> </span><span class="c1" id="mwcA"># non parametric library</span> <span class="nf" id="mwcQ">data</span><span class="p" id="mwcg">(</span><span class="n" id="mwcw">cps71</span><span class="p" id="mwdA">)</span> <span class="nf" id="mwdQ">attach</span><span class="p" id="mwdg">(</span><span class="n" id="mwdw">cps71</span><span class="p" id="mweA">)</span> <span class="n" id="mweQ">m</span><span class="w" id="mweg"> </span><span class="o" id="mwew"><-</span><span class="w" id="mwfA"> </span><span class="nf" id="mwfQ">npreg</span><span class="p" id="mwfg">(</span><span class="n" id="mwfw">logwage</span><span class="o" id="mwgA">~</span><span class="n" id="mwgQ">age</span><span class="p" id="mwgg">)</span> <span class="nf" id="mwgw">plot</span><span class="p" id="mwhA">(</span><span class="n" id="mwhQ">m</span><span class="p" id="mwhg">,</span><span class="w" id="mwhw"> </span><span class="n" id="mwiA">plot.errors.method</span><span class="o" id="mwiQ">=</span><span class="s" id="mwig">"asymptotic"</span><span class="p" id="mwiw">,</span> <span class="w" id="mwjA"> </span><span class="n" id="mwjQ">plot.errors.style</span><span class="o" id="mwjg">=</span><span class="s" id="mwjw">"band"</span><span class="p" id="mwkA">,</span> <span class="w" id="mwkQ"> </span><span class="n" id="mwkg">ylim</span><span class="o" id="mwkw">=</span><span class="nf" id="mwlA">c</span><span class="p" id="mwlQ">(</span><span class="m" id="mwlg">11</span><span class="p" id="mwlw">,</span><span class="w" id="mwmA"> </span><span class="m" id="mwmQ">15.2</span><span class="p" id="mwmg">))</span> <span class="nf" id="mwmw">points</span><span class="p" id="mwnA">(</span><span class="n" id="mwnQ">age</span><span class="p" id="mwng">,</span><span class="w" id="mwnw"> </span><span class="n" id="mwoA">logwage</span><span class="p" id="mwoQ">,</span><span class="w" id="mwog"> </span><span class="n" id="mwow">cex</span><span class="o" id="mwpA">=</span><span class="n" id="mwpQ">.</span><span class="m" id="mwpg">25</span><span class="p" id="mwpw">)</span> <span class="nf" id="mwqA">detach</span><span class="p" id="mwqQ">(</span><span class="n" id="mwqg">cps71</span><span class="p" id="mwqw">)</span>
Related
According to David Salsburg, the algorithms used in kernel regression were independently developed and used in fuzzy systems: "Coming up with almost exactly the same computer algorithm, fuzzy systems and kernel density-based regressions appear to have been developed completely independently of one another."
Statistical implementation
- GNU Octave mathematical program package
- Julia: KernelEstimator.jl
- MATLAB: A free MATLAB toolbox with implementation of kernel regression, kernel density estimation, kernel estimation of hazard function and many others is available on these pages (this toolbox is a part of the book ).
- Python: the
KernelRegclass for mixed data types in thestatsmodels.nonparametricsub-package (includes other kernel density related classes), the package kernel_regression as an extension of scikit-learn (inefficient memory-wise, useful only for small datasets) - R: the function
npregof the np package can perform kernel regression. - Stata: npregress, kernreg2

