In [ ]:
#  Module: Anomaly detection
#  Version 1.1
#  Topic : Density based approach
#  Example source:
#####################################################################
#  For support or questions, contact Sri Krishnamurthy at
#  sri@quantuniversity.com
#  Copyright 2016 QuantUniversity LLC.
#####################################################################
In [ ]:
library(DMwR)
library(MASS)
library(clusterSim)
library(Rlof)
library(ggplot2)
In [ ]:
### LOF Algorithm
data <- data.frame(Boston)
data <- data[1:50,c(3,5,6,7)]
data <- data.Normalization(data,type="n4",normalization="column")
outlier.scores <- lofactor(data, k=5)
plot(density(outlier.scores))
outliers <- order(outlier.scores, decreasing=T)[1:5]
print(outliers)
In [ ]:
### Visualize outliers by plots
pch <- rep("*", nrow(data))
pch[outliers] <- "o"
col <- rep("black", nrow(data))
col[outliers] <- "red"
pairs(data, pch=pch, col=col)
In [ ]:
data.lof<-lof(data,5)
data.lof
plot(hist(data.lof))
range(data.lof)
data.lof <- data.frame(data.lof)
data.lof$out=ifelse(data.lof[,1] > 1.5,"Yes","No")
data.lof
In [ ]:
X=data.lof[,1]
out=data.lof[,2]
ggplot(data.lof,aes(x=X,color=out,fill=out))+
  geom_histogram(binwidth=diff(range(X))/30)+
  theme_bw()