Sat Oct 18, 2014 5:28 am
setwd("D:/newFolder/")
data <- read.csv("Data.csv",head=TRUE )
require(randomForest)
require(ROSE)
if(!require(caret)){
library(caret)
}
if(!require(pROC)){
library(pROC)
}
library (ROCR);
k=10 #Folds
id <- sample(1:k,nrow(data),replace=TRUE)
list <- 1:k
prediction <- data.frame()
trainingset <- data.frame()
testsetCopy <- data.frame()
#Creating a progress bar to know the status of CV
#progress.bar <- create_progress_bar("text")
#progress.bar$init(k)
PrecisionClassOne=0;
RecallClassOne=0;
PrecisionClassTwo=0;
RecallClassTwo=0;
for (i in 1:k){
trainingset <- subset(data, id %in% list[-i])
# Performing upsampling of minorities using ROSE package
#trainingset <- ROSE(class~., data=trainingset, N=length(trainingset$class))$data
# Note that the sizes of the arrays here are based on your data. So you may need to change it!!
trainingset=downSample(trainingset[,1:22],as.factor( trainingset[,23]), list = FALSE, yname = "class")
trainingset=upSample(trainingset[,1:22],as.factor( trainingset[,23]), list = FALSE, yname = "class")
#print(trainingset[,23])
testset <- subset(data, id %in% c(i))
#which(sapply(testset, class) != sapply(trainingset, class))
library(party)
cf1 <- cforest(class~.,data=trainingset,control=cforest_unbiased(mtry=2,ntree=100))
print("perform predictions on test data...")
predictions <- predict(cf1, newdata=testset)
metrics<- confusionMatrix(predictions,testset$class,positive='1')
ClassOne=metrics$byClass
metrics2<- confusionMatrix(predictions,testset$class,positive='2')
ClassTwo=metrics2$byClass;
PrecisionClassOne=ClassOne[3]+PrecisionClassOne;
RecallClassOne=ClassOne[1]+RecallClassOne;
PrecisionClassTwo=ClassTwo[3]+PrecisionClassTwo;
RecallClassTwo=ClassTwo[1]+RecallClassTwo;
rocValue=roc.curve(testset$class, predictions,
main="ROC curve \n (Half circle depleted data)")
importToSave=varImp(cf1)
#varImp(model2,conditional=TRUE)
#plot(varImp(model2), top = 20)
if(i>1)
{
saveTemp= cbind(saveTemp,importToSave)
saveROCtemp= rbind(saveROCtemp ,rocValue$auc)
}else
{
saveTemp= importToSave;
saveROCtemp=rocValue$auc;
}
}
PrecisionClassOne=PrecisionClassOne/k;
RecallClassOne=RecallClassOne/k;
PrecisionClassTwo=PrecisionClassTwo/k;
RecallClassTwo=RecallClassTwo/k;
print("Class One Precision/ Recall");
print(PrecisionClassOne);
print(RecallClassOne);
print("Class Two(Re-open) Precision/ Recall");
print(PrecisionClassTwo);
print(RecallClassTwo);
### Saving the importance variables .
write.table ( saveTemp,
file = "CondsaveTemptable.csv",
append = FALSE,
quote = TRUE,
sep = ",",
col.names = TRUE,
row.names = TRUE);
meansOfCOlS=rowMeans(saveTemp)
max(saveTemp)
min(meansOfCOlS)
write.table (meansOfCOlS,
file = "CondsaveTemptableMeans.csv",
append = FALSE,
quote = TRUE,
sep = ",",
col.names = TRUE,
row.names = TRUE);
### Saving the RCOC variables .
write.table ( saveROCtemp,
file = "CondsaveROCTemptable.csv",
append = FALSE,
quote = TRUE,
sep = ",",
col.names = TRUE,
row.names = TRUE);
newSaveTemp<-t(saveTemp)
melt(newSaveTemp)
b <- ggplot(saveTemp, aes(x = saveTemp, ymin = `0%`, lower = `25%`, middle = `50%`, upper = `75%`, ymax = `100%`))
b + geom_boxplot(stat = "identity")
Sat Nov 28, 2015 4:57 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.