0
$\begingroup$

this is my KNN code:

library(class)
library(ggplot2)
library(gmodels)
library(scales)
library(caret)
library(tidyverse)


db_data <- iris
row_train <- sample(nrow(iris), nrow(iris)*0.8)
db_train <- iris[row_train,]
db_test <- iris[-row_train,]

unique(db_train$Species)
table(db_train$Species)
#--------

#FIST METHOD 

#KNN
#-------
model_knn<-train(Species ~ ., data = db_train, method = "knn",tuneLength = 10)
#summary(model_knn)

set.seed(1)
model_knn<-train(Species ~ ., data = db_train, method = "knn",tuneGrid = data.frame(k = c(2:20)))
summary(model_knn)
#-------

#PREDICTION NEW RECORD
#-------
test_data <- db_test
db_test$predict <- predict(model_knn, newdata=test_data, interval='confidence')
confusionMatrix(data=factor(db_test$predict),reference=factor(db_test$Species))

but when I run model_knn I have this error:

Error in train(Species ~ ., data = db_train, method = "knn", tuneLength = 10) : 
  could not find function "train"

How can I fix it?

$\endgroup$
6
  • $\begingroup$ Works fine at my machine, did you restart R and checked again? $\endgroup$
    – Peter
    Nov 22 at 14:37
  • $\begingroup$ Yes I have tried several times but I keep getting the same error $\endgroup$
    – Inuraghe
    Nov 22 at 15:04
  • $\begingroup$ the error is that I have not loaded the caret library $\endgroup$
    – Inuraghe
    Nov 22 at 15:38
  • 4
    $\begingroup$ I’m voting to close this question because it was a simple programming error $\endgroup$
    – Peter
    Nov 22 at 16:31
  • $\begingroup$ @Peter same story of this question: datascience.stackexchange.com/questions/88757/… Why should be this closed whereas the other one no? $\endgroup$ Nov 27 at 1:31
1
$\begingroup$

As someone that is more used to use Python's structure, I highly recommend to use the package/class name before the method.

So if you are using the method train, you want to specify that this method comes from caret package by using the special character ::

# install.packages("gmodels")
# install.packages("caret")
# install.packages("e1071")

library(class)
library(ggplot2)
library(gmodels)
library(scales)
library(caret)
library(tidyverse)


db_data <- iris
row_train <- sample(nrow(iris), nrow(iris)*0.8)
db_train <- iris[row_train,]
db_test <- iris[-row_train,]

unique(db_train$Species)
table(db_train$Species)
#--------

#FIST METHOD 

#KNN
#-------
model_knn<-caret::train(Species ~ ., data = db_train, method = "knn",tuneLength = 10)
#summary(model_knn)

set.seed(1)
model_knn<-caret::train(Species ~ ., data = db_train, method = "knn",tuneGrid = data.frame(k = c(2:20)))
summary(model_knn)
#-------

#PREDICTION NEW RECORD
#-------
test_data <- db_test
db_test$predict <- predict(model_knn, newdata=test_data, interval='confidence')
confusionMatrix(data=factor(db_test$predict),reference=factor(db_test$Species))

Outputs:

Confusion Matrix and Statistics

            Reference
Prediction   setosa versicolor virginica
  setosa          9          0         0
  versicolor      0          7         1
  virginica       0          2        11

Overall Statistics
                                          
               Accuracy : 0.9             
                 95% CI : (0.7347, 0.9789)
    No Information Rate : 0.4             
    P-Value [Acc > NIR] : 1.698e-08       
                                          
                  Kappa : 0.8477          
                                          
 Mcnemar's Test P-Value : NA              

Statistics by Class:

                     Class: setosa Class: versicolor Class: virginica
Sensitivity                    1.0            0.7778           0.9167
Specificity                    1.0            0.9524           0.8889
Pos Pred Value                 1.0            0.8750           0.8462
Neg Pred Value                 1.0            0.9091           0.9412
Prevalence                     0.3            0.3000           0.4000
Detection Rate                 0.3            0.2333           0.3667
Detection Prevalence           0.3            0.2667           0.4333
Balanced Accuracy              1.0            0.8651           0.9028
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.