rm(list=ls(all=TRUE))
isBatch = FALSE
if(isBatch) { options(echo  =  FALSE); } else { options(echo  =  TRUE); }

library("effsize")

# ------------------- constants and parameters ----------------

EFF_SIZE_INDICATOR="VarghaDelaney"   #    "Hedges"    #  

OUT_TO_FILE=FALSE
DATA_DIR="./"

# The set of input files to be considered is specified here:
#mulNames=c( "results_NN__CoCo_McCC.csv",  "results_NN__HCPL_LLOC.csv", "results_NN__McCC_LLOC.csv")
#mulLabels=c("CoCo,McCC", "HCPL,LLOC", "McCC,LLOC")
mulLabels=c("CoCo", "HVOL", "HCPL", "McCC", "LLOC", "NLE", "MI")
mulNames=c("results_NN__CoCo.csv", "results_NN__HVOL.csv", "results_NN__HCPL.csv", 
           "results_NN__McCC.csv", "results_NN__LLOC.csv", "results_NN__NLE.csv", "results_NN__MI.csv")

# ------------------ functions ----------------------------------------

# compute a set of accuracy indicators, given actuals and predictions
computeIndicators<-function(act, pred){
  err=act-pred
  abserr=abs(err)
  MAR=mean(abserr)
  MdAR=median(abserr)
  relErr=err/act
  MMRE=mean(abs(relErr))
  MARdivMEAN=MAR/mean(abs(act))
  MdARdivMEDIAN=MdAR/median(abs(act))
  y_segnato=mean(act)
  ssTot=sum((act-y_segnato)^2)
  ssRes=sum((err)^2)
  R2=1-ssRes/ssTot
  return(cbind(MAR, MdAR, MMRE, MARdivMEAN, MdARdivMEDIAN, R2))
}

# ---------- end of functions ---------------------------------------------

# ----------- computation of indicators starts here ----------------------------------------------

par(mar=c(6.0, 2.6, 0.8, 0.4))  # bottom, left, top and right margins
# adjust the space at the bottom depending on the mesures being used 

list_abs_err=list()
list_MR=list()
vMAR=c()
vMR=c()
summaryTab=c()

for(j in 1:length(mulNames)){
  filename=paste(DATA_DIR, mulNames[j], sep="")
  ttt=read.csv(filename)
  ttt=ttt[,-1]
  abserr=abs(ttt$actuals-ttt$estimates)
  MeanTime=mean(ttt$actuals)
  MedianTime=median(ttt$actuals)
  list_abs_err[[j]]=abserr
  list_MR[[j]]=abserr/MeanTime
  mar=mean(abserr)
  mdar=median(abserr)
  vMAR=c(vMAR, mar)
  vMR=c(vMR, mean(abserr/MeanTime))
  aRow=c(mar, mdar, mar/MeanTime, mdar/MedianTime)
  summaryTab=rbind(summaryTab, aRow)
}
  
colnames(summaryTab)<-c("MAR", "MdAR", "MR", "MdR")
rownames(summaryTab)<-mulNames

# boxplot of absolute errors
boxplot(list_abs_err, boxwex=0.2, names=mulLabels, las=2)
points(vMAR, pch=23, bg="orange")
# boxplot of absolute errors without outliers
boxplot(list_abs_err, outline=FALSE, boxwex=0.2, names=mulLabels, las=2)
points(vMAR, pch=23, bg="orange")
# boxplot of relative absolute errors
boxplot(list_MR, boxwex=0.2, names=mulLabels, las=2, 
        col="light gray", cex.axis = 0.8)
points(vMR, pch=23, bg="orange")
# boxplot of relative absolute errors without outliers
boxplot(list_MR, outline=FALSE, boxwex=0.4, names=mulLabels, las=2, 
        col="light gray", cex.axis = 0.8)
points(vMR, pch=23, bg="orange")

rownames(summaryTab)<-mulLabels
print(summaryTab)
write.csv(summaryTab, "summary.csv")

effsizeTable=c()
for(j1 in 1:length(mulNames)){
  filename=paste(DATA_DIR, mulNames[j1], sep="")
  ttt1=read.csv(filename)
  ttt1=ttt1[,-1]
  abserr1=abs(ttt1$actuals-ttt1$estimates)
  aRow=c()
  for(j2 in 1:length(mulNames)){
    if(j1!=j2){
      filename=paste(DATA_DIR, mulNames[j2], sep="")
      ttt2=read.csv(filename)
      ttt2=ttt2[,-1]
      abserr2=abs(ttt2$actuals-ttt2$estimates)
      if(EFF_SIZE_INDICATOR=="Hedges"){
        gHedges=cohen.d(abserr1, abserr2, hedges.correction=TRUE)
        gHedgesMag=as.character(gHedges$magnitude)
        aRow=c(aRow, gHedges$estimate)
      }
      if(EFF_SIZE_INDICATOR=="VarghaDelaney"){
        ttt=VD.A(abserr1, abserr2)
        effSize=ttt[[4]]
        aRow=c(aRow, effSize)
      }
    } else {
      aRow=c(aRow, NA)
    }
  }
  effsizeTable=rbind(effsizeTable, aRow)
}

colnames(effsizeTable)<-mulLabels
rownames(effsizeTable)<-mulLabels
print(effsizeTable)
write.csv(effsizeTable, "effectSize.csv")
