【发布时间】:2017-10-15 07:05:07
【问题描述】:
使用函数 tree() 和以下代码在 R 中运行分类树时遇到问题:
library(tree)
library(ISLR)
attach(Carseats)
High=ifelse(Sales<=8, "No", "Yes")
Carseats=data.frame(Carseats, High)
tree.carseats=tree(High~.-Sales, Carseats)
summary(tree.carseats)
问题是,当我第一次将所有代码一起运行时,我得到的结果与我所指的书(统计学习简介)相同:
Classification tree:
tree(formula = High ~ . - Sales, data = Carseats)
Variables actually used in tree construction:
[1] "ShelveLoc" "Price" "Income" "CompPrice" "Population" "Advertising" "Age" "US"
Number of terminal nodes: 27
Residual mean deviance: 0.4575 = 170.7 / 373
Misclassification error rate: 0.09 = 36 / 400
但是,当我再次运行相同的代码时,树并没有提供任何更有意义的结果:
Classification tree:
tree(formula = High ~ . - Sales, data = Carseats)
Variables actually used in tree construction:
[1] "High.1"
Number of terminal nodes: 2
Residual mean deviance: 0 = 0 / 398
Misclassification error rate: 0 = 0 / 400
谁能解释一下这是怎么回事?
谢谢。
【问题讨论】:
-
我运行了你列出的所有代码,然后又运行了
tree.carseats = tree(High~.-Sales, Carseats)。它给出了与第一次完全相同的输出。我不知道您身边发生了什么,但我确实看到在您的第二次运行中只使用了 1 个变量。
标签: r tree statistics classification