0

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?

Multivac
  • 2,784
  • 2
  • 8
  • 26
Inuraghe
  • 481
  • 3
  • 17
  • Works fine at my machine, did you restart R and checked again? – Peter Nov 22 '21 at 14:37
  • Yes I have tried several times but I keep getting the same error – Inuraghe Nov 22 '21 at 15:04
  • the error is that I have not loaded the caret library – Inuraghe Nov 22 '21 at 15:38
  • 4
    I’m voting to close this question because it was a simple programming error – Peter Nov 22 '21 at 16:31
  • @Peter same story of this question: https://datascience.stackexchange.com/questions/88757/error-while-trying-glmnet-in-r-error-in-storage-modexd-double-list/88806#88806 Why should be this closed whereas the other one no? – Multivac Nov 27 '21 at 01:31
  • OP simply forgot to execute a line which is present in the code. So „programming error“ is a friendly description – Peter Nov 27 '21 at 08:55

1 Answers1

1

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
Multivac
  • 2,784
  • 2
  • 8
  • 26