Hi:
I couldn't find one by searching with RSiteSearch() or package sos,
but it's not hard to write a simple function that reproduces the
example on the Wikipedia page:
ndcg <- function(x) {
# x is a vector of relevance scores
ideal_x <- rev(sort(x))
DCG <- function(y) y[1] + sum(y[-1]/log(2:length(y), base = 2))
DCG(x)/DCG(ideal_x)
}
rel <- c(3,2,3,0,1,2) # example from Wikipedia
ndcg(rel)
[1] 0.9315085
The difference between this and the result on the Wikipedia page is
due to rounding. You can see this by pulling out the DCG function and
evaluating it separately:
> DCG <- function(y) y[1] + sum(y[-1]/log(2:length(y), base = 2))
> DCG(rel)
[1] 8.097171> DCG(rev(sort(rel)))
[1] 8.692536
The Wikipedia page clearly indicates that there are several
considerations that need to be addressed in the generation and
processing of relevance scores. The above function obviously does not
cover all the bases, but it does cover the situation corresponding to
the given example. It's a start...
HTH,
Dennis
On Fri, Jun 10, 2011 at 6:14 PM, Jim Cheng <jxcheng at gmail.com>
wrote:> Dose R have a function to calculate NDCG?
> http://en.wikipedia.org/wiki/Discounted_cumulative_gain#Normalized_DCG
>
> Thanks!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>