简体   繁体   English

k- R 中的最近邻核回归

[英]k- Nearest Neighbor Kernel Regression in R

I am using "KernelKnn" package for k-NN kernel regression with single covariate.我正在使用“KernelKnn”包进行单协变量的k-NN核回归。 But there is some problem and this error is showing.但是有一些问题,这个错误正在显示。

 Error in if (!is.numeric(k) || is.null(k) || (k >= nrow(data)) || k <  : 
      missing value where TRUE/FALSE needed

How to correct the following prog?如何更正以下程序?

 require(KernelKnn)  
 require(KernSmooth)   
 nn<-function(m,n){ 
 x<-runif(n,-1,1)  
 sd<-sd(x)   
 res <- matrix(0, m, 2)  
 for(i in 1:m){   
 y<-rexp(n,1) 
 PI <- abs(dpik(x) )                        ###plug-in  
 RoT <-abs(1.06*sd(x)*(n^(-1/5)))          ###normal scale rule     

nn1<-KernelKnn(matrix(x),TEST_data=NULL,matrix(y),k=1,weights_function='gaussian', h=PI,method = 'euclidean', regression = TRUE) 
nn2<- KernelKnn(matrix(x), TEST_data = NULL, matrix(y), k =1,weights_function='gaussian', h=RoT,method = 'euclidean', regression = TRUE) 
D1=(y-nn1)^2  
D2=(y-nn2)^2   
MSE.NN1=sum(D1)/(n)
MSE.NN2=sum(D2)/(n) 
res[i,1] =MSE.NN1   
res[i,2] =MSE.NN2  
}  
(res) 
} 
apply(nn(500,25),2,mean)  
apply(nn(500,50),2,mean) 
apply(nn(500,100),2,mean) 
apply(nn(500,150),2,mean)

You can check the documentation by running ?KernelKnn and this will solve some of your problems.您可以通过运行?KernelKnn来查看文档,这将解决您的一些问题。

1.- According to the documentation, x and y should be either matrices or dataframes, but you have two lists instead. 1.- 根据文档, xy应该是矩阵或数据框,但你有两个列表。 So use matrix(x) and matrix(y) instead of x and y .所以使用matrix(x)matrix(y)而不是xy

2.- kernel K -nearest neigbors is in some sense a simple k -nearest neighbors with weighted distances, so you have to choose the closest k observations, in your case between 1 and 9. Since you have 10 observations, if you choose one of them then you can't take the closest n=10 observations as there are only 9 left. 2.- kernel K -nearest neigbors 在某种意义上是一个简单的k最近邻,具有加权距离,因此您必须选择最近的k观测,在您的情况下,在 1 和 9 之间。由于您有 10 个观测,如果您选择一个其中,您不能采用最接近的n=10观测值,因为只剩下 9 n=10观测值。

So in summary, something like this should work所以总而言之,这样的事情应该有效

nn1<- KernelKnn(matrix(x), TEST_data= NULL, matrix(y), k =9,weights_function='gaussian', h=RoT,method = 'euclidean', regression = TRUE)

Notice however that the previous will throw an error for k=1 and k=2 , unfortunately I'm not familiar with the detailed implementation of this algorithm so I can't tell you here why is it failing in those cases.但是请注意,之前的k=1k=2会引发错误,不幸的是我不熟悉这个算法的详细实现,所以我不能在这里告诉你为什么在这些情况下它会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM