I have some problems in finding the outliers using clustering.
The data.frame is ~20000 observations and each row has mixed types of variables(numeric, nominal and binary). What I want to do is to detect the outliers by clustering.
I have calculated the dissimilarity matrix using daisy() function in R:
diss = daisy(data,metric="gower")
And I know I can use pam() and hclust() functions to do the clustering. But how do I find the outliers after that?
Here is my R code to find the outliers from pam():
kmedoid = pam(diss,k=10,diss=T)
centers = kmedoid$id.med
distMat = as.matrix(diss)
distances = rep(-99,20000)
for (k in 1:20000) {
distances[k] = min(distMat[centers,k])
}
outliers = order(distances, decreasing=T)[1:5]
outliers = data[outliers]
outliers
I don't know whether it is correct, because the result seems to be pretty different each time when I tried different value of k in pam().
So the main question is: Once I have the "kmedoid" and "hc" calculated below, how do I find the outliers?
kmedoid = pam(data,k=10,diss=T)
hc = hclust(data)
I did search Google, but there wasn't much info about this. I am not fluent in programming, so just using existing package and function in R would help me a lot:)
And is there any better method to find the outliers?
Thanks!