library(caret)
library(randomForest)
library(ROCR)
cb <- read.delim("../1022_Decision Tree_2/Hshopping.txt", stringsAsFactors=FALSE)
colnames(cb) <- c("ID","SEX","AGE","AMT","STAR","REFUND") # Jupyter note Font Error using Korean
cb$REFUND <- factor(cb$REFUND)
set.seed(1)
flds <- createFolds(cb$REFUND, k=5, list=T, returnTrain=F)
str(flds)
experiment <- function(train, test, m) {
rf <- randomForest(REFUND ~ .-ID, data=train, ntree=50)
rf_pred <- predict(rf, test, type="response")
m$acc = c(m$acc, confusionMatrix(rf_pred, test$REFUND)$overall[1])
rf_pred_prob <- predict(rf, test, type="prob")
rf_pred <- prediction(rf_pred_prob[,2], cb.test$REFUND)
m$auc = c(m$auc, performance(rf_pred, "auc")@y.values[[1]])
return(m)
}
measure = list()
for(i in 1:5){
inTest <- flds[[i]]
cb.test <- cb[inTest,]
cb.train <- cb[-inTest,]
measure = experiment(cb.train,cb.test,measure)
}
measure
mean(measure$acc); sd(measure$acc)
mean(measure$auc); sd(measure$auc)