Title: | k Nearest Neighbors with Grid Search Variable Selection |
---|---|
Description: | k Nearest Neighbors with variable selection, combine grid search and forward selection to achieve variable selection in order to improve k Nearest Neighbors predictive performance. |
Authors: | Eddie Pei [aut, cre], Ernest Fokoue [aut] |
Maintainer: | Eddie Pei <[email protected]> |
License: | GPL-3 |
Version: | 0.1.0 |
Built: | 2025-02-13 03:25:37 UTC |
Source: | https://github.com/cran/kNNvs |
k Nearest Neighbors with Grid Search Variable Selection
kNNvs( train_x, test_x, cl_train, cl_test, k, model = c("regression", "classifiation") )
kNNvs( train_x, test_x, cl_train, cl_test, k, model = c("regression", "classifiation") )
train_x |
matrix or data frame of training set |
test_x |
matrix or data frame of test set |
cl_train |
factor of true classifications of training set |
cl_test |
factor of true classifications of test set |
k |
the number of neighbors |
model |
regression or classifiation |
kNNvs is simply use add one and then compare acc to pick the best variable set for the knn model
ACC or MSE, best variable combination, estimate value yhat
{ data(iris3) train_x <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3]) test_x <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3]) cl_train<- cl_test<- factor(c(rep("s",25), rep("c",25), rep("v",25))) k<- 5 # cl_test is not null mymodel<-kNNvs(train_x,test_x,cl_train,cl_test,k,model="classifiation") mymodel # cl_test is null mymodel<-kNNvs(train_x,test_x,cl_train,cl_test=NULL,k,model="classifiation") mymodel }
{ data(iris3) train_x <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3]) test_x <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3]) cl_train<- cl_test<- factor(c(rep("s",25), rep("c",25), rep("v",25))) k<- 5 # cl_test is not null mymodel<-kNNvs(train_x,test_x,cl_train,cl_test,k,model="classifiation") mymodel # cl_test is null mymodel<-kNNvs(train_x,test_x,cl_train,cl_test=NULL,k,model="classifiation") mymodel }