set.seed(665544)
n <- 100
x <- cbind(
x=runif(10, 0, 5) + rnorm(n, sd = 0.4),
y=runif(10, 0, 5) + rnorm(n, sd = 0.4)
)
### calculate LOF score with a neighborhood of 3 points
lof <- lof(x, minPts = 3)
### distribution of outlier factors
summary(lof)
hist(lof, breaks = 10, main = "LOF (minPts = 3)")
### plot sorted lof. Looks like outliers start arounf a LOF of 2.
plot(sort(lof), type = "l", main = "LOF (minPts = 3)",
xlab = "Points sorted by LOF", ylab = "LOF")
### point size is proportional to LOF and mark points with a LOF > 2
plot(x, pch = ".", main = "LOF (minPts = 3)", asp = 1)
points(x, cex = (lof - 1) * 2, pch = 1, col = "red")
text(x[lof > 2,], labels = round(lof, 1)[lof > 2], pos = 3)
Run the code above in your browser using DataLab