Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Aaronmoralesshildrick/f6aab98da0c7cb2087afdfd901f3f7a9 to your computer and use it in GitHub Desktop.
5.1 PreClass
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