hyunjimoon/SBC

Ranks are computed incorrectly when NAs are involved

Closed this issue · 0 comments

A new failing test:

test_that("calculate_ranks_draws_matrix infinity NA", {
  dm <- matrix(NA_real_, nrow = 5, ncol = 4)
  colnames(dm) <- c("a","b", "c","d")
  dm[,"a"] <- c(-Inf, -Inf, 1, 2, +Inf)
  dm[,"b"] <- c(-Inf, -Inf, -Inf, 1, 2)
  dm[,"c"] <- c(-Inf, NA_real_, -Inf, 1, 2)
  dm[,"d"] <- c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)

  dm <- posterior::as_draws_matrix(dm)

  vars <- matrix(c(-Inf, NA_real_, 14, NA_real_), nrow = 1)
  colnames(vars) <- c("a","b","c", "d")

  N_steps <- 200
  all_ranks <- matrix(NA_real_, nrow = N_steps, ncol = 4)
  for(i in 1:N_steps) {
    last_ranks <- calculate_ranks_draws_matrix(vars, dm)
    all_ranks[i,] <- last_ranks

  }
  # The final rank is stochastic due to presence of ties
  expect_true(all(all_ranks[,1] <= 1))
  expect_true(all(0:2 %in% all_ranks[,1]))
  expect_true(all(0:6 %in% all_ranks[,2]))
  expect_true(all(all_ranks[,3] <= 5 & all_ranks[,3] >= 4))
  expect_true(all(4:5 %in% all_ranks[,3]))
  expect_true(all(0:6 %in% all_ranks[,2]))
})