user
user

Reputation: 914

Error in eval(expr, envir, enclos) : object 'V2' not found

Why am I attaining this error? It appears my svm classifier worked based on the output, however the rest does not work. I have attempted this with the given iris dataset from R, and the code below worked perfectly. However, when trying to run with the loaded data set it does not.

error:

Error in eval(expr, envir, enclos) : object 'V2' not found

Code:

balance_data = read.table(file.choose(), sep=",")
str(balance_data)
x <- balance_data[, c(2,3,4,5)]
y <- balance_data[,1]
X_train <-head(x,500)
Y_train <- head(y,100)
X_test <-tail(x,122)


decisionTree_Learnruntime = c()
svm_Learnruntime = c()
naivebayes_Learnruntime = c()
knn_Learnruntime = c()

decisionTree_Predictruntime = c()
svm_Predictruntime = c()
naivebayes_Predictruntime =c()
knn_Predictruntime = c()


for (i in 1:20){
  library(e1071)
  library(caret)
  #SVM Model
  start.time <- Sys.time()
  svm_model <- svm(X_train,Y_train)
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  svm_Learnruntime [i]<- time.taken

  #Prediction Time
  start.time <- Sys.time()
  pred <- predict(svm_model,X_test)
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  svm_Predictruntime [i]<- time.taken

  library(rpart)
  #Decision Tree
  #Learn Time
  start.time <- Sys.time()
  tree_model <- rpart(X_train,Y_train)
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  decisionTree_Learnruntime [i]<- time.taken

  #Prediction Time 
  start.time <- Sys.time()
  pred = predict(tree_model,X_test)
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  decisionTree_Predictruntime[i] <- time.taken


  #Naive Bayes
  #Learn Time
  start.time <- Sys.time()
  naive_model <-naiveBayes(X_train,Y_train)
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  naivebayes_Learnruntime [i]<- time.taken

  #Prediction Time
  start.time <- Sys.time()
  pred <- predict(naive_model,X_test)
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  naivebayes_Predictruntime [i]<- time.taken

}

mean(svm_Learnruntime)
mean(svm_Predictruntime)
decisionTree_Learnruntime
decisionTree_Predictruntime
naivebayes_Learnruntime
naivebayes_Predictruntime

output

> mean(svm_Learnruntime)
[1] 0.01403713
> mean(svm_Predictruntime)
[1] 0.001003027
> decisionTree_Learnruntime
NULL
> decisionTree_Predictruntime
NULL
> naivebayes_Learnruntime
NULL
> naivebayes_Predictruntime
NULL

structure

> str(balance_data)
'data.frame':   625 obs. of  5 variables:
 $ V1: Factor w/ 3 levels "B","L","R": 1 3 3 3 3 3 3 3 3 3 ...
 $ V2: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 1 1 1 2 2 2 2 2 ...
 $ V5: int  1 2 3 4 5 1 2 3 4 5 ...
> str(X_train)
'data.frame':   500 obs. of  4 variables:
 $ V2: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 1 1 1 2 2 2 2 2 ...
 $ V5: int  1 2 3 4 5 1 2 3 4 5 ...
> str(X_test)
'data.frame':   122 obs. of  4 variables:
 $ V2: int  5 5 5 5 5 5 5 5 5 5 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 2 2 2 2 2 3 3 3 ...
 $ V5: int  4 5 1 2 3 4 5 1 2 3 ...
> str(Y_train)
 Factor w/ 3 levels "B","L","R": 1 3 3 3 3 3 3 3 3 3 ...

Upvotes: 0

Views: 1555

Answers (1)

rconradin
rconradin

Reputation: 346

Your error comes from the rpart function.

From the rpart documentation:

Usage

rpart(formula, data, weights, subset, na.action = na.rpart, method,
  model = FALSE, x = FALSE, y = TRUE, parms, control, cost, ...) 

Arguments

formula: a formula, with a response but no interaction terms. If this a a data frome, that is taken as the model frame (see model.frame).

So you need something like:

data_train <- head(balance_data,100)
...
tree_model <- rpart(V1 ~ V2 + V3 + V4, data_train) 

Depending on what are your model.

Upvotes: 1

Related Questions