Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

KNN outlier detection in R

$
0
0

I am trying to run a script I was given to perform outlier detection using a weighted KNN outlier score, but keep getting the following error:

Error in apply(kNNdist(x = dat, k = k), 1, mean) :
dim(X) must have a positive length

The script I am trying to run is as below. It is a single block of script, but I have added a comment directly above the section of the script that is causing the error, which is the function:

WKNN_Outlier <- apply(kNNdist(x=dat, k = k), 1, mean)

If anyone has any better or simpler ideas for unsupervised outlier detection, I am all ears (so to speak...)

    library(dbscan)
    library(ggplot2)

    set.seed(0)

    x11 <- rnorm(n = 100, mean = 10, sd = 1) # Cluster 1 (x1 coordinate)
    x21 <- rnorm(n = 100, mean = 10, sd = 1) # Cluster 1 (x2 coordinate)
    x12 <- rnorm(n = 100, mean = 20, sd = 1) # Cluster 2 (x1 coordinate)
    x22 <- rnorm(n = 100, mean = 10, sd = 1) # Cluster 2 (x2 coordinate)
    x13 <- rnorm(n = 100, mean = 15, sd = 3) # Cluster 3 (x1 coordinate)
    x23 <- rnorm(n = 100, mean = 25, sd = 3) # Cluster 3 (x2 coordinate)
    x14 <- rnorm(n = 50, mean = 25, sd = 1)  # Cluster 4 (x1 coordinate)
    x24 <- rnorm(n = 50, mean = 25, sd = 1)  # Cluster 4 (x2 coordinate)

    dat <- data.frame(x1 = c(x11,x12,x13,x14), x2 = c(x21,x22,x23,x24))

    ( g0a <- ggplot() + geom_point(data=dat, mapping=aes(x=x1, y=x2), shape = 19) )

    k <- 4 # KNN parameter
    top_n <- 20 # No. of top outliers to be displayed

    KNN_Outlier <- kNNdist(x=dat, k = k)
    rank_KNN_Outlier <- order(x=KNN_Outlier, decreasing = TRUE)    # Sorting (descending)
    KNN_Result <- data.frame(ID = rank_KNN_Outlier, score = KNN_Outlier[rank_KNN_Outlier])

    head(KNN_Result, top_n)

    graph <- g0a +
      geom_point(data=dat[rank_KNN_Outlier[1:top_n],], mapping=aes(x=x1,y=x2), shape=19, 
      color="red", size=2) +
      geom_text(data=dat[rank_KNN_Outlier[1:top_n],],
      mapping=aes(x=(x1-0.5), y=x2, label=rank_KNN_Outlier[1:top_n]), size=2.5)

    graph

    ## Use KNNdist() to calculate the weighted KNN outlier score

    k <- 4       # KNN parameter
    top_n <- 20  # No. of top outliers to be displayed

The WKNN_Outler function below is what is causing the error. From what I can gather, the apply function shouldn't be having any issues, as the data (dat) is converted into a data.frame, which should prevent the error, but doesn't.

    WKNN_Outlier <- apply(kNNdist(x=dat, k = k), 1, mean)  # Weighted KNN outlier score (mean)


    rank_WKNN_Outlier <- order(x=WKNN_Outlier, decreasing = TRUE) 
    WKNN_Result <- data.frame(ID = rank_WKNN_Outlier, score = WKNN_Outlier[rank_WKNN_Outlier])

    head(WKNN_Result, top_n)

    ge1 <- g0a +
      geom_point(data=dat[rank_WKNN_Outlier[1:top_n],], mapping=aes(x=x1,y=x2), shape=19, 
      color="red", size=2) +
      geom_text(data=dat[rank_WKNN_Outlier[1:top_n],],
      mapping=aes(x=(x1-0.5), y=x2, label=rank_WKNN_Outlier[1:top_n]), size=2.5)

    ge1

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>