Last data update: 2014.03.03

R: k Nearest Neighbor Regression
knn.regR Documentation

k Nearest Neighbor Regression

Description

k-nearest neighbor regression

Usage

knn.reg(train, test = NULL, y, k = 3, algorithm=c("kd_tree", "cover_tree", "brute"))

Arguments

train

matrix or data frame of training set cases.

test

matrix or data frame of test set cases. A vector will be interpreted as a row vector for a single case. If not supplied, cross-validataion will be done.

y

reponse of each observation in the training set.

k

number of neighbours considered.

algorithm

nearest neighbor search algorithm.

Details

If test is not supplied, Leave one out cross-validation is performed and R-square is the predicted R-square.

Value

knn.reg returns an object of class "knnReg" or "knnRegCV" if test data is not supplied.

The returnedobject is a list containing at least the following components:

call

the match call.

k

number of neighbours considered.

n

number of predicted values, either equals test size or train size.

pred

a vector of predicted values.

residuals

predicted residuals. NULL if test is supplied.

PRESS

the sums of squares of the predicted residuals. NULL if test is supplied.

R2Pred

predicted R-square. NULL if test is supplied.

Note

The code for “VR” nearest neighbor searching is taken from class source

Author(s)

Shengqiao Li. To report any bugs or suggestions please email: shli@stat.wvu.edu.

See Also

knn.

Examples

  if(require(chemometrics)){
    data(PAC);
    pac.knn<- knn.reg(PAC$X, y=PAC$y, k=3);
    
    plot(PAC$y, pac.knn$pred, xlab="y", ylab=expression(hat(y)))
  } 

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(FNN)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/FNN/knn.reg.Rd_%03d_medium.png", width=480, height=480)
> ### Name: knn.reg
> ### Title: k Nearest Neighbor Regression
> ### Aliases: knn.reg
> ### Keywords: regression nonparametric
> 
> ### ** Examples
> 
>   if(require(chemometrics)){
+     data(PAC);
+     pac.knn<- knn.reg(PAC$X, y=PAC$y, k=3);
+     
+     plot(PAC$y, pac.knn$pred, xlab="y", ylab=expression(hat(y)))
+   } 
Loading required package: chemometrics
Loading required package: rpart
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>