11

我使用adaR 包已经有一段时间了,最​​近,caret. 根据文档,carettrain()函数应该有一个使用 ada. 但是,当我使用与我的ada()通话中相同的语法时,插入符号正在向我吐口水。

这是一个使用wine示例数据集的演示。

library(doSNOW)
registerDoSNOW(makeCluster(2, type = "SOCK"))
library(caret)
library(ada)

wine = read.csv("http://www.nd.edu/~mclark19/learn/data/goodwine.csv")


set.seed(1234) #so that the indices will be the same when re-run
trainIndices = createDataPartition(wine$good, p = 0.8, list = F)
wanted = !colnames(wine) %in% c("free.sulfur.dioxide", "density", "quality",
                            "color", "white")

wine_train = wine[trainIndices, wanted]
wine_test = wine[-trainIndices, wanted]
cv_opts = trainControl(method="cv", number=10)


 ###now, the example that works using ada() 

 results_ada <- ada(good ~ ., data=wine_train, control=rpart.control
 (maxdepth=30, cp=0.010000, minsplit=20, xval=10), iter=500)

##this works, and gives me a confusion matrix.

results_ada
     ada(good ~ ., data = wine_train, control = rpart.control(maxdepth = 30, 
     cp = 0.01, minsplit = 20, xval = 10), iter = 500)
     Loss: exponential Method: discrete   Iteration: 500 
      Final Confusion Matrix for Data:
      Final Prediction
      etc. etc. etc. etc.

##Now, the calls that don't work. 

results_ada = train(good~., data=wine_train, method="ada",
control=rpart.control(maxdepth=30, cp=0.010000, minsplit=20, 
xval=10), iter=500)
   Error in train.default(x, y, weights = w, ...) : 
   final tuning parameters could not be determined
   In addition: Warning messages:
   1: In nominalTrainWorkflow(dat = trainData, info = trainInfo, method = method,  :
    There were missing values in resampled performance measures.
   2: In train.default(x, y, weights = w, ...) :
    missing values found in aggregated results

 ###this doesn't work, either

results_ada = train(good~., data=wine_train, method="ada", trControl=cv_opts,
maxdepth=10, nu=0.1, iter=50)

  Error in train.default(x, y, weights = w, ...) : 
  final tuning parameters could not be determined
  In addition: Warning messages:
  1: In nominalTrainWorkflow(dat = trainData, info = trainInfo, method = method,  :
    There were missing values in resampled performance measures.
  2: In train.default(x, y, weights = w, ...) :
   missing values found in aggregated results

我猜这是 train() 需要额外的输入,但抛出的警告并没有给我任何关于缺少什么的提示。此外,我可能缺少一个依赖项,但没有提示应该有什么......

4

4 回答 4

2

所以这似乎有效:

wineTrainInd <- wine_train[!colnames(wine_train) %in% "good"]
wineTrainDep <- as.factor(wine_train$good)

results_ada = train(x = wineTrainInd, y = wineTrainDep, method="ada")

results_ada
Boosted Classification Trees 

5199 samples
   9 predictors
   2 classes: 'Bad', 'Good' 

No pre-processing
Resampling: Bootstrapped (25 reps) 

Summary of sample sizes: 5199, 5199, 5199, 5199, 5199, 5199, ... 

Resampling results across tuning parameters:

  iter  maxdepth  Accuracy  Kappa  Accuracy SD  Kappa SD
  50    1         0.732     0.397  0.00893      0.0294  
  50    2         0.74      0.422  0.00853      0.0187  
  50    3         0.747     0.437  0.00759      0.0171  
  100   1         0.736     0.411  0.0065       0.0172  
  100   2         0.742     0.428  0.0075       0.0173  
  100   3         0.748     0.442  0.00756      0.0158  
  150   1         0.737     0.417  0.00771      0.0184  
  150   2         0.745     0.435  0.00851      0.0198  
  150   3         0.752     0.449  0.00736      0.016   

Tuning parameter 'nu' was held constant at a value of 0.1
Accuracy was used to select the optimal model using  the largest value.
The final values used for the model were iter = 150, maxdepth = 3 and nu
 = 0.1.

原因在另一个问题中找到:

caret::train:指定模型生成参数

我认为您在train尝试找到最佳调整参数本身时将调整参数作为参数传递。如果您确实想定义自己的参数,您可以为网格搜索定义一个参数网格。

于 2014-03-24T19:34:03.927 回答
2

查找?train并搜索ada您会看到:

Method Value: ada from package ada with tuning parameters: iter, maxdepth, nu (classification only)

因此,您必须缺少nu参数和maxdepth参数。

于 2013-10-11T19:07:17.183 回答
1

中的数据类型是什么wine$good?如果它是 a factor,请尝试明确提及它是这样的:

wine$good <- as.factor(wine$factor)
stopifnot(is.factor(wine$good))

原因:通常,R 包在区分分类和回归场景时需要一些帮助,并且插入符号中可能有一些通用代码可能会错误地将练习识别为回归问题(忽略 ada 仅进行分类的事实)。

于 2013-10-21T11:32:20.053 回答
0

请在 tuneGrid 中包含参数

Grid <- expand.grid(maxdepth=25,nu=2,iter=100)
results_ada = train(good~., data=wine_train, method="ada",
trControl=cv_opts,tuneGrid=Grid)

这将起作用。

于 2015-09-09T05:44:08.390 回答