eval(expr,envir,enclos)出错:找不到对象'V2'

时间:2017-03-31 22:02:58

标签: r

为什么我会收到此错误? 看来我的svm分类器根据输出工作,但其余的不起作用。我用R中给定的iris数据集尝试了这个,下面的代码工作得很好。但是,当尝试使用加载的数据集运行时,它不会。

错误:

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

代码:

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

输出

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

结构

> 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 ...

1 个答案:

答案 0 :(得分:1)

您的错误来自rpart功能。

来自rpart文档:

  

<强>用法

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

<强>参数

     

formula:      公式,具有响应但没有交互条件。如果这是一个   data frome,作为模型框架(参见model.frame)。

所以你需要这样的东西:

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

取决于您的型号。