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

### R packages
#install.packages("mvoutlier")
#install.packages("clusterSim)
#install.packages("MVN")
library(mvoutlier)
library(clusterSim)
library(MVN)
In [ ]:
### boxplot, scatter plot, aqplot, chi-square plot, symbolplot
### Boxplot
data <- data.frame(iris)
head(data)
boxplot(data$Petal.Length~data$Species,
        main="Boxplot of Petal.Length and Species")
Sepal.width <- data[,2]
boxplot(Sepal.width,main="Boxplot of Sepal.width and Species")
boxplot.stats(Sepal.width)$out
In [ ]:
### Scatterplot
data <- data[1:100,c(1,2)]
data <- data.Normalization(data,type="n4",normalization="column") #  unitization with zero minimum ((x-min)/range)
Sepal.Length <- data[,1]
Sepal.Width <- data[,2]
(Sep.out1 <- which(Sepal.Length %in% boxplot.stats(Sepal.Length)$out))
(Sep.out2 <- which(Sepal.width %in% boxplot.stats(Sepal.width)$out))
### Outliers in either Sepal.Length or Sepal.Width
(outlier.list <- union(Sep.out1,Sep.out2 ))
plot(data)
points(data[outlier.list,], col="red", pch="x", cex=3)
In [ ]:
### Checking Multivariate normal
result <- mardiaTest(data, qqplot = FALSE)
result
result1 <- roystonTest(data, qqplot = FALSE) 
result1
In [ ]:
### Covariance matrix and Mahalanobis distance
mean <- colMeans(data)
mean
Sx <- cov(data)
Sx
D2<-mahalanobis(data,mean,Sx)
D2
In [ ]:
### Adjusted quantile plot
aq.plot(data,alpha=0.1)
In [ ]:
help(symbol.plot)
In [ ]:
# Symbol plot
symbol.plot(data, quan=0.5,alpha=0.025)