 

d=read.table("Glass_withoutdupl_norm.arff", skip=13,sep=",")



library(archetypes)
source("LstepArchetypesMod.R")

#1 if it is an outlier, 0 if it is not an outlier
label=d[,9]=='yes' 
label[label==TRUE]='1'
label[label==FALSE]='0'
label=as.factor(label)



X=d[,1:7]




#Find archetypes and screeplot (Step 2)
library(DDoutlier)
library(AUC)
nada=10 #15 #it is P
norep=20
set.seed(1234)
ai=LstepArchetypesMod(X, 1:nada, nrep=norep)

#Draw the RSS graph in order to find the elbow (number of initial archetypes)
screeplot(ai)


#Choose the number of initial archetypes
e=3 #2,4,5

vi=10 #5
vf=30 #15
#AA+ k-NN (Step 3)
auci=c()

for (i in vi:vf)
{
pei=rep(0,dim(X)[1])
for (j in e:nada){

a=bestModel(ai[[j]])

pei=pei+KNN_SUM(a$alphas, k=i) 

}
auci=c(auci,auc(roc(pei, label)))

}


#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)


#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.8802  0.8835  0.8846  0.8846  0.8862  0.8878 
#> sd(auci)
#[1] 0.001877562


####################

#Knn
pei=c()
auci=c()
vi=10
vf=30

for (i in vi:vf)
{

pei=KNN_SUM(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)


#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.8640  0.8661  0.8667  0.8666  0.8678  0.8683 
#> sd(auci)
#[1] 0.001178606



#LOF
pei=c()
auci=c()
vi=10
vf=30

for (i in vi:vf)
{

pei=LOF(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.7187  0.7393  0.8114  0.7999  0.8629  0.8683 
#> sd(auci)
#[1] 0.06273274



# COF
pei=c()
auci=c()
vi=10
vf=30

for (i in vi:vf)
{

pei=COF(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.7599  0.8081  0.8309  0.8280  0.8547  0.8585 
#> sd(auci)
#[1] 0.02924788




#LOOP
pei=c()
auci=c()
vi=10
vf=30

for (i in vi:vf)
{

pei=LOOP(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.7463  0.7729  0.8125  0.8008  0.8298  0.8396 
#> sd(auci)
#[1] 0.03283691

#PCA robust

pc.rob <- princomp(X, covmat = MASS::cov.rob(X))

pei=c()
auci=c()
vi=10
vf=30

for (i in vi:vf)
{

pei=KNN_SUM(pc.rob$scores, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.8640  0.8661  0.8667  0.8666  0.8678  0.8683 
#> sd(auci)
#[1] 0.001178606


#RDOS
pei=c()
auci=c()
vi=10
vf=30

for (i in vi:vf)
{

pei=RDOS(X, k=i) 
auci=c(auci,auc(roc(pei, label)))

}
#
#Mean and standard desviation of AUC 
summary(auci) 
sd(auci)

#> summary(auci) 
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 0.7014  0.7122  0.7176  0.7210  0.7274  0.7491 
#> sd(auci)
#[1] 0.01265894



#hbos
library('reticulate')
py_install("pandas")
os <- import("os")
source_python('hbos.py')
hbos = HBOS()
hr = hbos$fit_predict(X)
 hr = hbos$fit_predict(data.frame(X))
auc(roc(hr, label)) #

# 0.7311653
