Stats & AI tech blog - '일단 시도함'

[R] Decision Tree (의사결정나무) 본문

Programming/R

[R] Decision Tree (의사결정나무)

justdoit ok? 2024. 1. 15. 16:39

1. rpart (Recursive Partitioning and Regression Trees)

 : CART 알고리즘을 사용하여 기본적인 결정 트리를 구현

library(rpart)
library(rpart.plot)

rpartmod<-rpart(tracheostomy~. , data=train01, method="class")
rpart.plot(rpartmod)

# check xerror, cp for pruning
printcp(rpartmod)
plotcp(rpartmod)

# pruning
ptree<-prune(rpartmod, cp= rpartmod$cptable[which.min(rpartmod$cptable[,"xerror"]),"CP"])
rpart.plot(ptree)

# evaluation
rpartpred<-predict(ptree, test, type='class')
confusionMatrix(rpartpred, test$tracheostomy)

 

 

2. party and partykit

 : 조건부 추론 트리 (Conditional Inference Trees) 를 제공

  library(party)
  partymod<-ctree(tracheostomy~. , data=train01)
  plot(partymod)
  
  partypred<-predict(partymod, test)
  confusionMatrix(partypred, test$tracheostomy)

 

 

3. C50

 : C5.0 알고리즘을 사용하여 결정트리와 부스팅 모델을 제공

  library(C50)
  c50mod <- C5.0(train01[,-25],train01$tracheostomy)
  summary(c50mod)
  plot(c50mod)
  
  partypred<-predict(c50mod, test)
  confusionMatrix(partypred, test$tracheostomy) 

  # boost
  boost10 <- C5.0(train01[,-25],train01$tracheostomy, trials = 10)
  plot(boost10)
  partypred<-predict(boost10, test)
  confusionMatrix(partypred, test$tracheostomy)

 

 

4. tree

 : 단순하고 기본적인 결정 트리 구현

  library(tree)
  treemod <- tree(tracheostomy ~ ., data = train01)
  plot(treemod)
  text(treemod)
  
  cv.trees <- cv.tree(treemod, FUN = prune.misclass)
  plot(cv.trees)
  
  prune.trees <- prune.misclass(treemod, best=3) 
  plot(prune.trees)
  text(prune.trees, pretty=0)
  
  treepred <- predict(prune.trees, test, type='class')
  confusionMatrix(treepred, test$tracheostomy)}

 

 

5. CHAID

 : CHAID 알고리즘으로 트리 구현

install.packages("CHAID", repos="http://R-Forge.R-project.org")
library(CHAID)
  
train <- train[,-c(3,7,14)] # chaid predictor variables are categorical only!
test <- test[,-c(3,7,14)]
  
ctrl <- chaid_control(minsplit = 20, minbucket = 5)
model.chaid <- chaid(Discrepancy ~ ., data=train, control = ctrl)
plot(model.chaid)

chaidpred <- predict(model.chaid, test, type='class')
confusionMatrix(treepred, test$tracheostomy)}