[R] What does ksmooth() do that NadarayaWatsonkernel() does not?

Jesper Hybel Pedersen jeshyb at dtu.dk
Mon Feb 6 09:29:16 CET 2017



What does the function ksmooth() to correct bias? Reading articles gives me the formula:

\frac{$B-t(B^n_{i=1}K_h(x-x_i)y_i}{$B-t(B^n_{j=1}K_h(x-x_j)}

For the Nadaray-Watson estimator. The same formula is stated in the documentation for the function:

NadarayaWatsonkernel(x, y, h, gridpoint)

>From the bbemkr-package (see ?NadarayaWatsonkernel). However this is not what the ksmooth()
function does as shown by the incompatibility of the followingly methods of calculation:


if (!require(bbemkr)) {install.packages("bbemkr")}

N = 100
x = rnorm(N)
y = 2 * x + rnorm(N)


# Manual implementation of Nadaray-Watson
y_fitted = rep(0,N)
w = y_fitted
lambda = 1
for (h in 1:N)
                           {
                                                      for (i in 1:N)
                                                                                  {
                                                                                                             w[i] = dnorm((x[i]-x[h]),sd=lambda)
                                                              y_fitted[h] = y_fitted[h] + y[i] * w[i]
                                                                                  }
                                                      y_fitted[h] = y_fitted[h]/sum(w)
                           }

nw.bbemkr = NadarayaWatsonkernel(x, y, lambda, x)
ks.stats = ksmooth(x,y,kernel="normal",bandwidth=lambda,x.points=x)

plot( x, y)
points( x , y_fitted , col="blue")  # From the manual implementation
points( nw.bbemkr$gridpoint+rnorm(N,sd=0.02), nw.bbemkr$mh,col="green") # From package bbemkr
points( ks.stats$x,ks.stats$y ,col="red")


The manual implementation is the same as the bbemkr package function NadarayaWatsonkernel()
But these are not the same as ksmooth() $B!D(B but still ksmooth() seems to result in better fit so I would like
To know what it does to achieve this better fit?


Best regards
Jesper Hybel Pedersen

	[[alternative HTML version deleted]]



More information about the R-help mailing list