R中的多元非参数核回归

时间:2019-03-07 06:07:46

标签: r kernel regression

我正在寻找手动方法/方法来计算具有超过1个回归变量的非参数内核回归(例如3)。我考虑使用npregpro R软件包,但是我想发现算法的工作原理。从理论上讲,它是 here解释的,但是如何在R中执行呢?到目前为止,我已经尝试过,分别计算所有独立变量,最后将所有变量相加来计算yhat。

yhat<-fk1+fk2+fk3+fk4+fk5

其中“ fk1 = m(x1),fk2 =(mx2)”等等。但是结果与使用上述软件包计算得出的结果不同。

任何人都可以使用R代码分享或指导我吗?

Rcode

    y<-c(676,516,1052,868,1008,436,544,680,640,492,984,1400,1276,1736,1004,396,352,328,392,236,392,268,252,236,340,2436,2216,2096,1660,2272,824,1196,1960,2080,1764,412,416,504,492,636,1756,1232,1400,1520,1560)
 x1<-c(33,35,32,30,33,33,36,30,38,30,30,37,33,36,30,30,27,29,34,36,30,28,31,31,35,29,35,35,30,30,26,29,25,26,26,25,26,26,27,27,24,27,26,28,28)
 x2<-c(5.00,4.75,4.20,4.40,5.55,5.05,4.25,4.45,4.75,4.60,4.10,3.45,3.45,4.10,3.50,3.25,3.35,3.20,3.35,3.30,3.25,3.25,3.20,3.20,3.35,7.10,7.35,7.45,7.45,7.40,4.85,4.60,5.20,4.75,5.20,4.55,3.95,3.70,3.75,4.15,5.60,5.35,5.50,5.50,5.40)
 x3<-c(1441.67,1299.19,1154.27,1045.15,521.62,1273.02,1346.35,1253.88,1242.65,1281.95,553.69,494.74,525.97,571.14,408.64,646.65,514.03,350.73,496.29,580.92,535.82,490.34,552.39,661.32,672.15,528.65,563.13,497.96,458.38,498.25,936.26,894.79,941.36,1038.79,898.05,989.87,951.28,939.83,925.42,954.11,720.72,782.09,773.30,829.26,856.96)
 x4<-c(35185.5,28170.4,26455.0,25072.9,31664.2,25491.7,20877.3,25621.3,27587.3,26511.7,7886.5,14596.0,9826.8,11978.4,10368.6,17307.4,12822.0,8582.6,12369.5,14731.9,15060.6,11056.3,8118.9,13009.5,15003.7,10225.0,8024.2,10393.0,8711.6,10239.6,20436.0,12519.9,18979.0,22986.1,11704.5,17721.0,16485.2,17101.3,17849.0,16949.6,11344.6,14752.4,13649.8,14533.0,16892.2)
 x5<-c(16.4524,13.9852,15.3276,17.3128,22.3312,12.2778,17.8225,14.3516,13.6826,11.7566,9.8820,16.6752,12.3730,9.4058,14.9302,31.2865,30.1652,28.5901,19.8795,18.5056,22.1344,28.6101,23.1908,24.6917,22.6758,0.3729,0.2703,0.3205,0.2648,0.2105,18.9875,20.9687,23.9841,19.9727,21.3864,23.7063,30.5589,26.8415,27.7292,21.5699,19.6531,20.3295,19.5880,20.1328,19.2420)
x<-data.frame(x1,x2,x3,x4,x5)
 n<-length(y)

require(bbemkr)
Baysian<-nrr(x, logband = TRUE)
 h1<-Baysian[1]
 h2<-abs(Baysian[2])
 h3<-Baysian[3]
 h4<-Baysian[4]
 h5<-Baysian[5]

res <- matrix(0, length(x1), ncol = 8)
for(i in 1: length(x1)) {
k1 <- (1/(sqrt(2*pi)))*exp((-1/2)*((x1-x1[i])/h1)^2)
k2 <- (1/(sqrt(2*pi)))*exp((-1/2)*((x2-x2[i])/h2)^2)
k3 <- (1/(sqrt(2*pi)))*exp((-1/2)*((x3-x3[i])/h3)^2)
k4 <- (1/(sqrt(2*pi)))*exp((-1/2)*((x4-x4[i])/h4)^2)
k5 <- (1/(sqrt(2*pi)))*exp((-1/2)*((x5-x5[i])/h5)^2)

fk1 <- (k1*y)/sum(k1)
fk2 <- (k2*y)/sum(k2)
fk3 <- (k3*y)/sum(k3)
fk4 <- (k4*y)/sum(k4)
fk5 <- (k5*y)/sum(k5)

y.hat1 <- sum(fk1)
y.hat2 <- sum(fk2)
y.hat3 <- sum(fk3)
y.hat4 <- sum(fk4)
y.hat5 <- sum(fk5)

yhat<-y.hat1+y.hat2+y.hat3+y.hat4+y.hat5
Error <- (y-yhat)

res[i,1] =y.hat1
res[i,2] =y.hat2
res[i,3] =y.hat3
res[i,4] =y.hat4
res[i,5] =y.hat5
res[i,6] =yhat
res[i,7] =Error[i] 
res[i,8] = (Error[i])^2
}
print(res)
MSE<-(sum(res[,3]))/n
MSE
#####By Pkg###
require(np)
nw<- npreg(x,y,bws=abs(Baysian),bandwidth.compute=FALSE,ckertype="gaussian")
yhat<-fitted(nw)
ressii<-(sum(residuals(nw)^2))/n
ressii

问:为什么两者都无法产生相同的结果?

Manual:1011.385
by Package: 5.382793e-15

我怎么了?

0 个答案:

没有答案