# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

# Any results you write to the current directory are saved as output.
train_full=read.csv("../input/train_numeric.csv",nrows=20000)
test=read.csv("../input/train_numeric.csv",nrows=20000,skip=20000)

colnames(test)<-colnames(train_full)

#Remove less relevant columns
sna<-function(a)
{
  sum(is.na(a)|(a==""))
}

trn=train_full[,((apply(train_full,2,"sna"))<nrow(train_full)/2)]
trn=trn[((apply(trn,1,"sna"))<ncol(trn)),]

tst=test[,colnames(trn)]

#Replace NA's by 0
trn[is.na(trn)]<-0
tst[is.na(tst)]<-0

#Free memory
rm(train_full)
rm(test)

#Categorical response
trn[,"Response"]<-as.factor(trn[,"Response"])
tst[,"Response"]<-as.factor(tst[,"Response"])
levels(trn[,"Response"])<-c("N","P")
levels(tst[,"Response"])<-c("N","P")

#Create balanced train set
#   Positive samples
trn_sample=trn[trn[,"Response"]=="P",]
trn_sample=trn_sample[sample(0:nrow(trn_sample),2000,replace=TRUE),]
#Add noise
for (c in colnames(trn_sample)[-ncol(trn_sample)])
{
  sd=sd(trn_sample[,c])/100
  trn_sample[,c]=trn_sample[,c]+rnorm(nrow(trn_sample),0,sd)
}
#   Negative samples
trn_sample=rbind(trn_sample,trn[sample(which(trn[,"Response"]=="N"),2000,replace=TRUE),])

library(caret)

#Train Model
my.grid <- expand.grid(.decay = c(0.5, 0.1), .size = c(3,5,10))
my.tc<-trainControl(classProbs=TRUE,summaryFunction = twoClassSummary)
mynnetfit <- train(Response ~ ., data = trn_sample, method = "nnet", maxit = 100, tuneGrid = my.grid
                   ,trControl=my.tc
                   ,metric = "ROC")


plot(varImp(mynnetfit,scale=F))

pred<-predict(mynnetfit,trn_sample[,-ncol(trn_sample)])
cls<-trn_sample[,ncol(trn_sample)]
cmx_t<-confusionMatrix(pred,cls)
cmx_t

#Test model
pred<-predict(mynnetfit,tst[,-ncol(tst)])
ppred<-predict(mynnetfit,tst[,-ncol(tst)],type='prob')
cls<-tst[,ncol(tst)]
cmx_s<-confusionMatrix(pred,cls)

#Matthews correlation coefficient
TN=cmx_s$table[1]/sum(cmx_t$table)
FP=cmx_s$table[2]/sum(cmx_t$table)
FN=cmx_s$table[3]/sum(cmx_t$table)
TP=cmx_s$table[4]/sum(cmx_t$table)
MCC=((TP*TN)-(FP*FN))/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))


MCC
