-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.R
46 lines (41 loc) · 1.52 KB
/
cart.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
library(rpart)
library(rpart.plot)
data <- read.csv(file="titanic.csv")
train = data[1: floor(nrow(data) * 0.7), ]
test = data[(floor(nrow(data) * 0.7) + 1) : nrow(data), ]
features = c("Pclass", "Sex", "Age", "FamilySize" , "Fare", "Embarked")
formula = Survived ~ Pclass + Sex + Age + FamilySize + Fare + Embarked
#build the model
cart_model <- rpart (formula, train) #control = ctrl
#print results
print(cart_model)
# summarize the model
summary(cart_model)
#prints a tree as a set of rules
rpart.rules(cart_model)
#display cp table
printcp(cart_model)
#plot cross-validation results
plotcp(cart_model)
#plot the tree
rpart.plot (cart_model, tweak = 1.25, type = 5, extra = 101)
training <- predict(cart_model, train[, features], type="class")
table(training, train$Survived)
# make predictions
predictions <- predict(cart_model, test[, features], type="class")
# summarize accuracy
table(predictions, test$Survived)
#prune tree
#value of CP that minimize the cross-validated error, (xerror)
prune_cart <- prune(cart_model, cp= 0.02)
rpart.plot (prune_cart, tweak = 1.25, type = 5, extra = 101)
training <- predict(prune_cart, train[, features], type="class")
table(training, train$Survived)
# make predictions after pruning
predictions <- predict(prune_cart, test[, features], type="class")
# summarize accuracy
table(predictions, test$Survived)
#minsplit, minimum number of observations in a node
ctrl = rpart.control(minsplit=50)
cart_model <- rpart (formula, train, control = ctrl)
rpart.plot (cart_model, tweak = 1.25, type = 5, extra = 101)