在一项作业中,我们被要求对 CART 模型执行交叉验证。我尝试使用 from 的cvFit
函数,cvTools
但收到一条奇怪的错误消息。这是一个最小的例子:
library(rpart)
library(cvTools)
data(iris)
cvFit(rpart(formula=Species~., data=iris))
我看到的错误是:
Error in nobs(y) : argument "y" is missing, with no default
和traceback()
:
5: nobs(y)
4: cvFit.call(call, data = data, x = x, y = y, cost = cost, K = K,
R = R, foldType = foldType, folds = folds, names = names,
predictArgs = predictArgs, costArgs = costArgs, envir = envir,
seed = seed)
3: cvFit(call, data = data, x = x, y = y, cost = cost, K = K, R = R,
foldType = foldType, folds = folds, names = names, predictArgs = predictArgs,
costArgs = costArgs, envir = envir, seed = seed)
2: cvFit.default(rpart(formula = Species ~ ., data = iris))
1: cvFit(rpart(formula = Species ~ ., data = iris))
看起来这y
是强制性的cvFit.default
。但:
> cvFit(rpart(formula=Species~., data=iris), y=iris$Species)
Error in cvFit.call(call, data = data, x = x, y = y, cost = cost, K = K, :
'x' must have 0 observations
我究竟做错了什么?哪个包可以让我对 CART 树进行交叉验证,而无需自己编写代码?(我太懒了……)