昨天跟着fantuanxiaot师兄—— [原创]基于R语言的核回归(Kernal Regression)与最近邻回归(NNBR) 的帖子学习了以下,觉得核回归的方式比较原来的必须服从某些假定的回归方法好很多,在网上找了找相关的内容,把找到的内容,分享给坛友~
[size=17.3333339691162px] 核方法常见的有Nadaraya-Watson核估计与Gasser-Muller核估计方法。
核密度估计的原理其实是很简单的。在我们对某一事物的概率分布的情况下。如果某一个数在观察中出现了,我们可以认为这个数的概率密度很大,和这个数比较近的数的概率密度也会比较大,而那些离这个数远的数的概率密度会比较小。
基于这种想法,针对观察中的第一个数,我们都可以f(x-xi)去拟合我们想象中的那个远小近大概率密度。当然其实也可以用其他对称的函数。针对每一个观察中出现的数拟合出多个概率密度分布函数之后,取平均。如果某些数是比较重要,某些数反之,则可以取加权平均。
NW核估计形式为:
GM核估计形式为:
- # 定义x.y.bw(窗宽)
- x <- seq(-1, 1, length = 40)
- y <- 5 * x * cos(5 * pi * x)
- h <- 0.055
- # 核估计法一:NW method核估计
- fx.hat <- function(z, h) {
- dnorm((z - x)/h)/h
- }
- NWSMOOTH <- function(h, y, x) {
- n <- length(y)
- s.hat <- rep(0, n)
- for (i in 1:n) {
- a <- fx.hat(x[i], h)
- s.hat[i] <- sum(y * a/sum(a))
- }
- return(s.hat)
- }
- NWsmooth.val <- NWSMOOTH(h, y, x)
- plot(x, y, xlab = "Predictor", ylab = "Response", col = 1)
- f <- function(x) 5 * x * cos(5 * pi * x)
- curve(f, -1, 1, ylim = c(-15.5, 15.5), lty = 1, add = T, col = 1)
- lines(x, NWsmooth.val, lty = 2, col = 2)
- A <- data.frame(x = seq(-1, 1, length = 1000))
- model.linear <- lm(y ~ poly(x, 9))
- lines(seq(-1, 1, length = 1000), predict(model.linear, A), lty = 3, col = 3)
- letters <- c("NW method", "orignal model", "9 order poly-reg")
- legend("bottomright", legend = letters, lty = c(2, 1, 3), col = c(2, 1, 3),
- cex = 0.5)
- # 核估计法二:GMSMOOTH估计
- GMSMOOTH <- function(y, x, h) {
- n <- length(y)
- s <- c(-Inf, 0.5 * (x[-n] + x[-1]), Inf)
- s.hat <- rep(0, n)
- for (i in 1:n) {
- fx.hat <- function(z, h, x) {
- dnorm((x - z)/h)/h
- }
- a<- y * integrate(fx.hat, s, s[i + 1], h =
- h, x = x)$value
- s.hat <- sum(a)
- }return(s.hat)
- }
- GMsmooth.val <- GMSMOOTH(y, x, h)
- plot(x, y, xlab = "Predictor", ylab = "Response", col =1)
- f <- function(x) 5 * x * cos(5 * pi * x)
- curve(f, -1, 1, ylim = c(-15.5, 15.5), lty = 1, add = T,col = 1)
- lines(x, GMsmooth.val, lty = 2, col = 2)
- A <- data.frame(x = seq(-1, 1, length = 1000))
- model.linear <- lm(y ~ poly(x, 9))
- lines(seq(-1, 1, length = 1000), predict(model.linear,A), lty = 3, col = 3)
- letters <- c("GM method", "orignal model", "9 order polyreg")
- legend("bottomright", legend = letters, lty = c(2, 1, 3),col = c(2, 1, 3),cex = 0.5)
本帖隐藏的内容
- # 两估计方法对比
- fx.hat <- function(z, h) {
- dnorm((z - x)/h)/h
- }
- NWSMOOTH <- 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)
- }
- NWsmooth.val <- NWSMOOTH(h, y, x)
- GMSMOOTH <- function(y, x, h) {
- n <- length(y)
- s <- c(-Inf, 0.5 * (x[-n] + x[-1]), Inf)
- s.hat <- rep(0, n)
- for (i in 1:n) {
- fx.hat <- function(z, h, x) {
- dnorm((x - z)/h)/h
- }
- a <- y * integrate(fx.hat, s, s[i + 1], h = h, x = x)$value
- s.hat <- sum(a)
- }
- return(s.hat)
- }
- GMsmooth.val <- GMSMOOTH(y, x, h)
- plot(x, y, xlab = "Predictor", ylab = "Response", col = 1)
- f <- function(x) 5 * x * cos(5 * pi * x)
- curve(f, -1, 1, ylim = c(-15.5, 15.5), lty = 1, add = T, col = 1)
- lines(x, NWsmooth.val, lty = 2, col = 2)
- lines(x, GMsmooth.val, lty = 3, col = 3)
- letters <- c("orignal model", "NW method", "GM method")
- legend("bottomright", legend = letters, lty = 1:3, col = 1:3, cex = 0.5)
从图中可以看到NW估计的方差似乎小些,事实也确实如此,GM估计的渐进方差约为NW估计的1.5倍。但是GM估计解决了一些计算的难题。
学习于:yujunbeta老师博客,http://blog.csdn.net/yujunbeta/article/details/26058533
小亮老师博客,http://blog.sina.com.cn/s/blog_62b37bfe0101homb.html