roll_percent_rank max value is not 1

Issue #20 new
Former user created an issue

Reproducible example using base::rank with ties.method = "min":

x <- rnorm(1000)
n <- 20

all.equal(
  zoo::rollapplyr(x, width = n, fill = NA_real_, FUN = function(slice) {
    ranks <- rank(slice, ties.method = "min", na.last = "keep")
    tail((ranks - 1) / (sum(!is.na(slice)) - 1), n = 1)
  }),
  QuantTools::roll_percent_rank(x, n = n)
)
[1] FALSE

Rescale QuantTools::roll_percent_rank to [0, 1]:

all.equal(
  zoo::rollapplyr(x, width = n, fill = NA_real_, FUN = function(slice) {
    ranks <- rank(slice, ties.method = "min", na.last = "keep")
    tail((ranks - 1) / (sum(!is.na(slice)) - 1), n = 1)
  }),
  QuantTools::roll_percent_rank(x, n = n) * n / (n - 1)
)
[1] TRUE

Comments (0)

  1. Log in to comment