Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(nnet) # 인공신경망 구현 라이브러리
- library(party)# CART 라이브러리
- library(caret)
- library(doParallel)
- library(ada)
- library(gbm)
- library(randomForest)
- library(rpart)
- library(adabag)
- earthquate_data <- read.csv("Earthquate_Damage.csv")
- # Performance Evaluation Function -----------------------------------------
- perf_eval <- function(cm){
- # True positive rate: TPR (Recall)
- TPR <- cm[2,2]/sum(cm[2,])
- # Precision
- PRE <- cm[2,2]/sum(cm[,2])
- # True negative rate: TNR
- TNR <- cm[1,1]/sum(cm[1,])
- # Simple Accuracy
- ACC <- (cm[1,1]+cm[2,2])/sum(cm)
- # Balanced Correction Rate
- BCR <- sqrt(TPR*TNR)
- # F1-Measure
- F1 <- 2*TPR*PRE/(TPR+PRE)
- return(c(TPR, PRE, TNR, ACC, BCR, F1))
- }
- Perf.Table <- matrix(0, nrow = 8, ncol = 6)
- rownames(Perf.Table) <- c("MLR", "CART", "ANN","Bagging CART","Random Forests", "Bagging ANN", "AdaBoost", "GBM" )
- colnames(Perf.Table) <- c("TPR", "Precision", "TNR", "Accuracy", "BCR", "F1-Measure")
- # [사전 작업 사항] numeric 이 아닌 변수들에 대해 1-of-C coding
- id.idx <- 1 # 일련 번호는 제외 해줌
- target.idx <- 40 # 타겟 변수
- earthquate.input <- earthquate_data[,c(-id.idx,-target.idx)] # id와 타겟변수를 제외해줌
- earthquate.target <- as.factor(earthquate_data[,target.idx]) # 종속 변수
- str(earthquate.input) # 각 변수의 타입을 확인 => 명목형 변수를 변환
- factor_val <- c(8,9,10,11,12,13,14,26) # factor형 변수들의 인덱스
- # 명목형 변수 하나하나 반복문을 돌면서 일단 추가해주고, 마지막에 기존의 명목형 변수 열을 삭제 해줌
- for(i in factor_val){
- earthquate.input <- cbind(earthquate.input,class.ind(earthquate.input[,i])) # 일단 다 추가 해 줌
- }
- earthquate.input <- earthquate.input[,-factor_val] # 기존 명목형 변수 열을 삭제
- earthquate.input.scaled <- scale(earthquate.input, center = TRUE, scale = TRUE)
- earthquate.data.scaled <- data.frame(earthquate.input.scaled, earthquate.target) # 데이터를 합쳐줌
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement