I've been asked to apply knn
to data for "income", based on "age", "gender" and "occupation" from the adults.txt data. This is my code after loading the data into R.
library(class)
set.seed(1234)
ind <- sample(2, nrow(adult), replace=TRUE, prob=c(0.75, 0.25))
adult.training <- adult[ind==1, c(1,7,10)]
adult.test <- adult[ind==2, c(1,7,10)]
adult.trainLabels <- adult[ind==1, c(15)]
adult.testLabels <- adult[ind==2, c(15)]
adult_pred <- knn(train=adult.training, test=adult.test, cl=adult.trainLabels, k=3)
I get the following errors:
Error in knn(train=adult.training, test=adult.test, cl=adult.trainLabels, :
NA/NAN/Inf in foreign function call (arg 6)
In addition: Warning messages:
1: In knn(train=adult.training, test=adult.test, cl=adult.trainLabels, :
NAs introduced by coercion
2: In knn(train=adult.training, test=adult.test, cl=adult.trainLabels, :
NAs introduced by coercion`
Is it possible to generate knn
data for "income" based on the above variables two of which are factors?