kNNvs {kNNvs} | R Documentation |
k Nearest Neighbors with Grid Search Variable Selection
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
}