|
| 1 | +Feature_selection <- function(predictors, target_vari, data) |
| 2 | +{ |
| 3 | + if(!require(VSURF)) install.packages("VSURF"); library(VSURF) |
| 4 | + if(!require(caret)) install.packages("caret"); library(caret) |
| 5 | + if(!require(snow)) install.packages("snow"); library(snow) |
| 6 | + if(!require(doParallel)) install.packages("doParallel"); library(doParallel) |
| 7 | + if(!require(randomForest)) install.packages("randomForest"); library(randomForest) |
| 8 | + |
| 9 | + c1 <- makeCluster(detectCores() - 1) # convention to leave 1 core for OS |
| 10 | + registerDoParallel(c1) |
| 11 | + |
| 12 | + for (i in 1:20){ |
| 13 | + set.seed(i+4567) |
| 14 | + correlationMatrix <- cor(predictors) |
| 15 | + highlyCorrelated <- findCorrelation(correlationMatrix, cutoff=0.90) |
| 16 | + # print indexes of highly correlated attributes |
| 17 | + #print(highlyCorrelated) |
| 18 | + predictors_cor <- predictors[,-highlyCorrelated] |
| 19 | + nbclass.sp <- table(as.character(data$SPECIES)) |
| 20 | + #nbclass.sp <- table(as.character(data$Level2)) |
| 21 | + nmin.sp<- min(nbclass.sp) |
| 22 | + tree.sampsize.sp <- rep(nmin.sp, nrow(nbclass.sp)) |
| 23 | + Fea.sel.species_VSURF_3DINT <- randomForest(x=predictors_cor, y=target_vari, sampsize = tree.sampsize.sp, ntree = 501, mtry = 3, parallel=TRUE) |
| 24 | + |
| 25 | + #deadtree includes, no balance sampling |
| 26 | + #Fea.sel.species_VSURF_3DINT <- randomForest(x=predictors_cor, y=target_vari, ntree = 501, mtry = 3, parallel=TRUE) |
| 27 | + |
| 28 | + #Fea.sel.species_VSURF_3DINT <- randomForest(x=predictors_cor, y=target_vari, ntree = 501, mtry = 3, parallel=TRUE) # B/C |
| 29 | + importance = Fea.sel.species_VSURF_3DINT$importance[,1] |
| 30 | + importance = importance[order(-importance)] |
| 31 | + #importance_select = importance[which(importance > 5.5)] |
| 32 | + importance_select = importance[1:15] |
| 33 | + predictors_cor_acc_imp <- predictors_cor[, which(colnames(predictors_cor) %in% names(importance_select))] |
| 34 | + varNames_species1 <- colnames(predictors_cor_acc_imp) |
| 35 | + varNames_species <- paste(varNames_species1, collapse = "+") |
| 36 | + SP.form <- as.formula(paste(name_target_vari, varNames_species, sep = "~")) |
| 37 | + cat("i = ", i, ", Total number of features, n = ", length(predictors_cor_acc_imp), "\n") |
| 38 | + show(SP.form) |
| 39 | + |
| 40 | + OOB_error_rf <- vector() |
| 41 | + Accuracy_rf <- vector() |
| 42 | + |
| 43 | + for (j in 1:100){ |
| 44 | + #RF_m <- randomForest(x=predictors_cor_acc_imp, y=as.factor(data$Broad_Needle), mtry = 3, ntree=501) # B/C |
| 45 | + RF_m <- randomForest(x=predictors_cor_acc_imp, y=as.factor(data$SPECIES), sampsize = tree.sampsize.sp, ntree=501, mtry = 3) # SPECIES |
| 46 | + #RF_m <- randomForest(x=predictors_cor_acc_imp, y=as.factor(data$Level2), sampsize = tree.sampsize.sp, ntree=501, mtry = 3) # SPECIES |
| 47 | + |
| 48 | + #deadtree includes, no balance sampling |
| 49 | + #RF_m <- randomForest(x=predictors_cor_acc_imp, y=as.factor(data$SPECIES), ntree=501, mtry = 3) # SPECIES |
| 50 | + |
| 51 | + confusion_mat_spp <- as.data.frame(RF_m$confusion) |
| 52 | + confusion_mat_spp_2 <- confusion_mat_spp[1:(length(confusion_mat_spp)-1)] |
| 53 | + rowsum_species <- rowSums(as.matrix(confusion_mat_spp_2)) |
| 54 | + total_n_err_spp <- confusion_mat_spp$class.error * rowsum_species |
| 55 | + OOB_error <- sum(total_n_err_spp) / sum(rowsum_species) |
| 56 | + OOB_error_rf <- c(OOB_error_rf, OOB_error) |
| 57 | + total_n_corr_spp <- rowsum_species - total_n_err_spp |
| 58 | + Accuracy_rf_each <- sum(total_n_corr_spp) / sum(rowsum_species) |
| 59 | + Accuracy_rf <- c(Accuracy_rf, Accuracy_rf_each) |
| 60 | + } |
| 61 | + cat("\n") |
| 62 | + cat("i = ", i, ", OOB_error_mean = ", mean(OOB_error_rf)*100, "\n") |
| 63 | + #cat("i = ", i, ", Accuracy_mean = ", mean(Accuracy_rf)*100, "\n") |
| 64 | + cat("\n") |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + stopCluster(c1) |
| 70 | + registerDoSEQ() |
| 71 | +} |
0 commit comments