user10072460
user10072460

Reputation:

Why do I get two different outputs when I use different functions?

I have the following data:

df1<- structure(list(Score = c(26,  46, 62, 57, 18, 16, 44, 37, 47, 32, 71, 72, 
39, 85, 39, 77, 82, 34, 73, 79, 82, 29, 30, 33, 61, 18, 15, 22, 30, 15, 17, 50,
34, 67, 46, 73, 10, 62, 20, 81, 55, 69, 52, 78, 61, 14, 59, 37, 60, 55, 31, 11,
13, 30, 68, 60, 61, 69, 20, 47, 81, 62, 76, 43, 42, 10, 36, 54, 56, 49, 15, 7,  
48, 11, 51, 32, 55, 80, 13, 57, 55, 70, 16, 85, 40, 75, 45, 7,  46, 19, 81, 35,
63, 30, 16, 71, 50, 15, 81, 55, 46, 27, 64, 29, 25, 79, 70, 13, 27, 14, 62, 53,
26, 53, 74, 48, 73, 68, 82, class = "data.frame")))

I have used the following function to get the decile:

df1 %>%
    mutate(quantile = ntile(-Score, 10))

I have used the StatMeasures package to calculate the decile. I have used:

df2<- decile(vector = Score, decreasing = TRUE)

but I get two different deciles using these two functions. It is very confusing. Which one is correct? Have I missed something? Can any one help?

Upvotes: 0

Views: 66

Answers (1)

StupidWolf
StupidWolf

Reputation: 46888

n_tile is meant for roughly placing values into 10 bins / buckets. It goes by rank, i.e first n/10 rank goes into 1 , next n/10 goes into 2 and so on. Hence when you have ties around the decile value, it might go into different bins:

First we get back your calculation:

library(StatMeasures)
library(dplyr)

df1 = data.frame(
Score = c(26, 46, 62, 57, 18, 16, 44, 37, 47, 32, 71, 72, 39, 85, 39, 77, 82, 34, 73, 79, 82, 29, 30, 33, 61, 18, 15, 22, 30, 15, 17, 50, 34, 67, 46, 73, 10, 62, 20, 81, 55, 69, 52, 78, 61, 14, 59, 37, 60, 55, 31, 11, 13, 30, 68, 60, 61, 69, 20, 47, 81, 62, 76, 43, 42, 10, 36, 54, 56, 49, 15, 7, 48, 11, 51, 32, 55, 80, 13, 57, 55, 70, 16, 85, 40, 75, 45, 7, 46, 19, 81, 35, 63, 30, 16, 71, 50, 15, 81, 55, 46, 27, 64, 29, 25, 79, 70, 13, 27, 14, 62, 53, 26, 53, 74, 48, 73, 68, 82)
)

df1 = df1 %>%
mutate(quantile1 = ntile(Score, 10)) %>%
mutate(quantile2 = decile(vector = Score))

We look at your decile values:

quantile(df1$Score,seq(0,1,by=0.1))
  0%  10%  20%  30%  40%  50%  60%  70%  80%  90% 100% 
 7.0 15.0 21.2 30.4 39.2 48.0 55.0 61.6 70.0 78.2 85.0 

And where the two rankings differ:

df1[df1$quantile1 != df1$quantile2,]
    Score quantile1 quantile2
3      62         7         8
20     79         9        10
30     15         2         1
71     15         2         1
81     55         7         6
98     15         2         1
100    55         7         6
116    48         6         5

We look at one example:

df1[df1$Score==48,]
    Score quantile1 quantile2
73     48         5         5
116    48         6         5

If you want deciles, the first method by n_tile is incorrect, because 48 goes into 2 bins. So use the decile function from StatMeasures.

Upvotes: 1

Related Questions