核估计的话,常见的有Nadaraya-Watson核估计与Gasser-Muller核估计方法,也就是很多教材里谈到的NW核估计与GM核估计,这里我们还是不谈核的选择,将一切的核估计都默认用Gauss核处理。
NW核估计形式为:[ hat f_h(x)=frac{sum_{i=1}^n K_h(x_i-x)y_i}{sum_{i=1}^n K_h(x_i-x)} ]
GM核估计形式为:[ hat f_h(x)=sum_{i=1}^n y_i int_{s_{i-1}}^{s_i} K_h(u-x)du ]
式中( s_i=(x_i+x_{i+1})/2,x_0=-infty,x_{n+1}=infty )
x <- seq(-1, 1, length = 20)
y <- 5 * x * cos(5 * pi * x)
h <- 0.088
fx.hat <- function(z, h) {
dnorm((z - x)/h)/h
}
KSMOOTH <- function(h, y, x) {
n <- length(y)
s.hat <- rep(0, n)
for (i in 1:n) {
a <- fx.hat(x, h)
s.hat <- sum(y * a/sum(a))
}
return(s.hat)
}
ksmooth.val <- KSMOOTH(h, y, x)
plot(x, y, xlab = "Predictor", ylab = "Response")
f <- function(x) 5 * x * cos(5 * pi * x)
curve(f, -1, 1, ylim = c(-15.5, 15.5), lty = 2, add = T)
lines(x, ksmooth.val, type = "l")