In [ ]:
#  Module: Anomaly detection
#  Version 1.1
#  Topic : Clustering approach
#  Example source:
#####################################################################
#  For support or questions, contact Sri Krishnamurthy at
#  sri@quantuniversity.com
#  Copyright 2016 QuantUniversity LLC.
#####################################################################

### R packages
#install.packages("kmodR")
#install.packages("cluster")
library(kmodR)
library(cluster)
library(MASS)
library(clusterSim)
In [ ]:
### Kmod
set.seed(123)
data <- data.frame(iris)
data <- data[1:100,1:4]
data <- data.Normalization(data,type="n4",normalization="column")
head(data)
In [ ]:
## Decide about number of clusters
wss <- nrow((data)-1)*sum(apply(data,2,var)) 
for (i in 2:15) 
{
  wss[i] <- sum(kmeans(data,centers=i)$withinss) # Within Sum of Squared for 
  # different number of clusters
}  

plot(1:15, wss, type="b", xlab="Number of Clusters",
     ylab="Within groups sum of squares")
In [ ]:
## Kmod
cl <- kmod(data,4,10)
index <- cl$L_index
outlier <- data[index,]
normal <- data[-index,]
outlier $ id <-1
normal $ id <- 0
newdata <- rbind (outlier,normal)
In [ ]:
newdata
with(newdata, pairs(newdata, col=ifelse(newdata$id == 1,"red","black")))