-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataMining_AnomalyDetection.Rmd
More file actions
207 lines (144 loc) · 3.93 KB
/
DataMining_AnomalyDetection.Rmd
File metadata and controls
207 lines (144 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---
title: "Anomaly Detection"
author: "Parash Upreti"
date: "July 13, 2016"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Anomaly Detection in R
Distance to k-Nearest Neighbor as Outlier Score
First Example Data Set
```{r}
set.seed(5364)
x1=rnorm(50)
y1=rnorm(50)
mydata1=data.frame(x=c(x1,6),y=c(y1,6))
plot(mydata1,pch=16)
#Distance Matrix
D=as.matrix(dist(mydata1))
#Distance to kth nearest neighbor (k=5)
kdist=1:51
for(i in 1:51){
kdist[i]=(sort(D[i,]))[6]
}
```
Plotting data
```{r,fig=T}
library(proto)
library(ggplot2)
library(gridExtra)
#Ordinary plot
ggplot(data=mydata1,aes(x=x,y=y,size=3))+geom_point()
#Plot with Color Determined by kdist
ggplot(data=mydata1,aes(x=x,y=y,col=kdist,size=3))+geom_point()
#Gradient Plot (Heatmap)
ggplot(data=mydata1,aes(x=x,y=y,col=kdist,size=3))+geom_point()+
scale_colour_gradientn(colours=c("black", "red"))
ggplot(data=mydata1,aes(x=x,y=y,col=kdist,size=3))+geom_point()+
scale_colour_gradientn(colours=c("blue", "red"))
#Using Size to Represent kdist
ggplot(data=mydata1,aes(x=x,y=y,size=kdist))+geom_point()
#Density curve for Outlier Scores (kdist)
plot(density(kdist))
```
Finding Rows with Outliers
```{r}
(1:51)[kdist>=6]
#my.kdist function
my.kdist=function(data,k){
n=nrow(data)
D=as.matrix(dist(data))
kdist=1:n
for(i in 1:n){
kdist[i]=(sort(D[i,]))[k+1]
}
return(kdist)
}
temp=my.kdist(mydata1,5)
cor(temp,kdist)
range(temp-kdist)
```
Second Example Data Set
```{r, fig=T}
set.seed(5364)
x1=rnorm(50)
y1=rnorm(50)
x2=0.5*rnorm(5)+10
y2=0.5*rnorm(5)+10
set.seed(5364)
mydata2=data.frame(x=c(x1,x2),y=c(y1,y2))
plot(mydata2,pch=16)
#Plot using kdist with k=5
kdist=my.kdist(mydata2,5)
ggplot(data=mydata2,aes(x=x,y=y,col=kdist,size=3))+geom_point()+
scale_colour_gradientn(colours=c("black", "red"))
plot(density(kdist))
(1:55)[kdist>=8]
#Plot using kdist with k=4
kdist=my.kdist(mydata2,4)
ggplot(data=mydata2,aes(x=x,y=y,col=kdist,size=3))+geom_point()+
scale_colour_gradientn(colours=c("black", "red"))
plot(density(kdist))
```
Third Example Data Set
```{r, fig=T}
set.seed(5366)
x1=rnorm(10)
y1=rnorm(10)
x2=0.1*rnorm(10)+10
y2=0.1*rnorm(10)
mydata3=data.frame(x=c(x1,x2,6),y=c(y1,y2,5))
plot(mydata3,pch=16)
#Detecting Outliers
kdist=my.kdist(mydata3,5)
ggplot(data=mydata3,aes(x=x,y=y,col=kdist,size=3))+geom_point()+
scale_colour_gradientn(colours=c("blue", "red"))
plot(density(kdist))
(1:nrow(mydata3))[kdist>=4]
```
### Density as an Outlier Score
Writing a density function
```{r, fig=T}
my.density=function(data,k){
n=nrow(data)
D=as.matrix(dist(data))
density=1:n
for(i in 1:n){
knn.distances=(sort(D[i,]))[2:(k+1)]
density[i]=(mean(knn.distances))^(-1)
}
return(density)
}
mydata3.density=my.density(mydata3,5)
ggplot(data=mydata3,aes(x=x,y=y,col=mydata3.density,size=3))+geom_point()+
scale_colour_gradientn(colours=c("blue", "red"))
ggplot(data=mydata3,aes(x=x,y=y,col=mydata3.density,size=3))+geom_point()+
scale_colour_gradientn(colours=c("red","blue"))
plot(density(mydata3.density))
```
### Average Relative Density as an Outlier Score
### Local Outlier Factor Method (LOF)
```{r, fig=T}
library(DMwR)
outlier.scores=lofactor(mydata3,k=5)
ggplot(data=mydata3,aes(x=x,y=y,col=outlier.scores,size=3))+geom_point()+
scale_colour_gradientn(colours=c("blue","red"))
plot(density(outlier.scores))
```
### Exploring Outliers in Iris Data with LOF
Remove Species Labels
```{r, fig=T}
attach(iris)
iris.x=iris[,1:4]
outlier.scores=lofactor(iris.x,k=5)
plot(density(outlier.scores))
sum(outlier.scores>=1.7)
sort(outlier.scores)
outliers=(outlier.scores>=1.7)
coloring=rep("black",150)
coloring[outliers]=c("red","green","blue")
plot(iris.x,col=coloring)
#text()
```