Created
October 8, 2018 08:29
-
-
Save Aaronmoralesshildrick/f6aab98da0c7cb2087afdfd901f3f7a9 to your computer and use it in GitHub Desktop.
5.1 PreClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("tree") | |
library("tree") | |
library("ISLR") | |
attach(Carseats) | |
High = ifelse(Carseats$Sales <=8,"No","Yes ") | |
Carseats = data.frame(Carseats, High) | |
Carseats | |
tree.carseats =tree(High~ CompPrice + Income +Advertising +Population +Price+ | |
ShelveLoc +Age +Education +Urban +US, Carseats) | |
summary(tree.carseats) | |
plot(tree.carseats) | |
text(tree.carseats ,pretty =0) | |
set.seed(2) | |
train=sample (1: nrow(Carseats ), 200) | |
Carseats.test = Carseats[-train ,] | |
High.test = High[-train] | |
tree.carseats =tree(High~ High~ CompPrice + Income +Advertising +Population +Price+ | |
ShelveLoc +Age +Education +Urban +US, Carseats ,subset=train) | |
tree.pred = predict(tree.carseats , Carseats.test ,type="class") | |
table(tree.pred ,High.test) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment