在执行以下代码行时,我遇到了错误。
require(ISLR)
require(tree)
attach(Carseats)
hist(Sales)
high = ifelse(Sales <= 8, "No", "Yes")
Carseats = data.frame(Carseats, high)
#fit tree
tree.carseats = tree(high~. -Sales, Carseats)
summary(tree.carseats)
plot(tree.carseats)
text(tree.carseats, pretty = 0)
#for a deatailed summary of the tree, print it
tree.carseats
#creat a training and test set (250, 150) split of the 400 observations,grow the tree on the training set, and evaluate its performance on the test set
set.seed(1011)
train = sample(1:nrow(Carseats), 250)#creat training set out of n obervations of carseats
tree.carseats = tree(high~. -Sales, Carseats, subset = train)
plot(tree.carseats);text(tree.carseats, pretty = 0)
tree.pre = predict(tree.carseats, Carseats[-train,], type = "class")
with(Carseats[-train,], table(tree.pre, high))
(61 + 45) /150#erro rate of bushy tree
#using cv to prune the tree
cv.carseats = cv.tree(tree.carseats, Carseats, FUN = prune.misclass)#use misclass error as the basis for doing the pruning
cv.carseats
plot(cv.carseats)
运行以下行时收到“ R遇到致命错误”消息:
cv.carseats = cv.tree(tree.carseats, Carseats, FUN = prune.misclass)