Fitting Classification Trees
attach (Carseats )
The following object is masked _by_ .GlobalEnv:
High
The following objects are masked from Carseats (pos = 4):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.13,
High.14, High.15, High.16, High.17, High.18, High.19, High.2, High.20, High.3, High.4, High.5,
High.6, High.7, High.8, High.9, Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 5):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.13,
High.14, High.15, High.16, High.17, High.18, High.19, High.2, High.3, High.4, High.5, High.6,
High.7, High.8, High.9, Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 6):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.13,
High.14, High.15, High.16, High.17, High.18, High.2, High.3, High.4, High.5, High.6, High.7,
High.8, High.9, Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 7):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.13,
High.14, High.15, High.16, High.17, High.2, High.3, High.4, High.5, High.6, High.7, High.8,
High.9, Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 12):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.13,
High.14, High.15, High.16, High.2, High.3, High.4, High.5, High.6, High.7, High.8, High.9,
Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 13):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.13,
High.14, High.15, High.2, High.3, High.4, High.5, High.6, High.7, High.8, High.9, Income,
Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 14):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.13,
High.14, High.2, High.3, High.4, High.5, High.6, High.7, High.8, High.9, Income, Population,
Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 16):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.13,
High.2, High.3, High.4, High.5, High.6, High.7, High.8, High.9, Income, Population, Price,
Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 17):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.12, High.2,
High.3, High.4, High.5, High.6, High.7, High.8, High.9, Income, Population, Price, Sales,
ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 18):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.11, High.2, High.3,
High.4, High.5, High.6, High.7, High.8, High.9, Income, Population, Price, Sales, ShelveLoc,
Urban, US
The following objects are masked from Carseats (pos = 19):
Advertising, Age, CompPrice, Education, High, High.1, High.10, High.2, High.3, High.4, High.5,
High.6, High.7, High.8, High.9, Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 20):
Advertising, Age, CompPrice, Education, High, High.1, High.2, High.3, High.4, High.5, High.6,
High.7, High.8, High.9, Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 21):
Advertising, Age, CompPrice, Education, High, High.1, High.2, High.3, High.4, High.5, High.6,
High.7, High.8, Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 22):
Advertising, Age, CompPrice, Education, High, High.1, High.2, High.3, High.4, High.5, High.6,
High.7, Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 23):
Advertising, Age, CompPrice, Education, High, High.1, High.2, High.3, High.4, High.5, High.6,
Income, Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 24):
Advertising, Age, CompPrice, Education, High, High.1, High.2, High.3, High.4, High.5, Income,
Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 25):
Advertising, Age, CompPrice, Education, High, High.1, High.2, High.3, High.4, Income,
Population, Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 26):
Advertising, Age, CompPrice, Education, High, High.1, High.2, High.3, Income, Population,
Price, Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 27):
Advertising, Age, CompPrice, Education, High, High.1, High.2, Income, Population, Price,
Sales, ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 28):
Advertising, Age, CompPrice, Education, High, High.1, Income, Population, Price, Sales,
ShelveLoc, Urban, US
The following objects are masked from Carseats (pos = 29):
Advertising, Age, CompPrice, Education, High, Income, Population, Price, Sales, ShelveLoc,
Urban, US
The following objects are masked from Carseats (pos = 30):
Advertising, Age, CompPrice, Education, Income, Population, Price, Sales, ShelveLoc, Urban, US
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] "High.1"
Number of terminal nodes: 2
Residual mean deviance: 0 = 0 / 398
Misclassification error rate: 0 = 0 / 400
plot(tree.carseats )
text(tree.carseats ,pretty =0)
tree.carseats
node), split, n, deviance, yval, (yprob)
* denotes terminal node
1) root 400 541.5 No ( 0.59 0.41 )
2) High.1: No 236 0.0 No ( 1.00 0.00 ) *
3) High.1: Yes 164 0.0 Yes ( 0.00 1.00 ) *
set.seed (2)
train=sample (1: nrow(Carseats ), 200)
Carseats.test=Carseats[-train ,]
High.test=High[-train ]
tree.carseats =tree(High~.-Sales ,Carseats ,subset =train )
tree.pred=predict(tree.carseats ,Carseats.test ,type ="class")
table(tree.pred ,High.test)
High.test
tree.pred No Yes
No 116 0
Yes 0 84
set.seed (3)
cv.carseats =cv.tree(tree.carseats ,FUN=prune.misclass )
names(cv.carseats )
[1] "size" "dev" "k" "method"
cv.carseats
$size
[1] 2 1
$dev
[1] 0 80
$k
[1] -Inf 80
$method
[1] "misclass"
attr(,"class")
[1] "prune" "tree.sequence"
par(mfrow =c(1,2))

plot(cv.carseats$size ,cv.carseats$dev ,type="b")
plot(cv.carseats$k ,cv.carseats$dev ,type="b")

prune.carseats =prune.misclass (tree.carseats ,best =9)
best is bigger than tree size
plot(prune.carseats )
text(prune.carseats ,pretty =0)
tree.pred=predict(prune.carseats , Carseats.test ,type="class")
table(tree.pred ,High.test)
High.test
tree.pred No Yes
No 116 0
Yes 0 84
(94+60) /200
[1] 0.77
prune.carseats =prune.misclass (tree.carseats ,best =15)
best is bigger than tree size
plot(prune.carseats )
text(prune.carseats ,pretty =0)

tree.pred=predict(prune.carseats , Carseats.test ,type="class")
table(tree.pred ,High.test)
High.test
tree.pred No Yes
No 116 0
Yes 0 84
LS0tDQp0aXRsZTogIkxhYm9yYXRvcmlvIDgiDQpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sNCi0tLQ0KDQpgYGB7cn0NCmxpYnJhcnkoTUFTUykNCmxpYnJhcnkoSVNMUikNCmxpYnJhcnkoY2FyKQ0KbGlicmFyeShkcGx5cikNCmxpYnJhcnkoZ2dwbG90MikNCmxpYnJhcnkoY2FyZXQpDQpsaWJyYXJ5KG1sYmVuY2gpDQpsaWJyYXJ5KGNhVG9vbHMpDQpsaWJyYXJ5IChyYW5kb21Gb3Jlc3QpDQpsaWJyYXJ5ICh0cmVlKQ0KbGlicmFyeSAoZ2JtKQ0KYGBgDQojI0ZpdHRpbmcgQ2xhc3NpZmljYXRpb24gVHJlZXMNCmBgYHtyfQ0KYXR0YWNoIChDYXJzZWF0cyApDQpIaWdoPWlmZWxzZSAoU2FsZXMgPD04LCIgTm8iLCIgWWVzICIpDQpDYXJzZWF0cyA9ZGF0YS5mcmFtZShDYXJzZWF0cyAsSGlnaCkNCnRyZWUuY2Fyc2VhdHMgPXRyZWUoSGlnaH4uLVNhbGVzICxDYXJzZWF0cyApDQpzdW1tYXJ5ICh0cmVlLmNhcnNlYXRzICkNCnBsb3QodHJlZS5jYXJzZWF0cyApDQp0ZXh0KHRyZWUuY2Fyc2VhdHMgLHByZXR0eSA9MCkNCnRyZWUuY2Fyc2VhdHMNCnNldC5zZWVkICgyKQ0Kd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