tests/testthat/test-wtd_median.R

test_that("no weights", {
  x <- c(1, 2, 3, 4, 5)
  expect_equal(wtd_median(x), stats::median(x))
})

test_that("weights", {
  x <- c(1, 2, 3, 4, 5)
  w <- c(5, 4, 3, 2, 1)
  expect_equal(wtd_median(x, weights = w),
               stats::median(rep(x, times = w)))
})

test_that("weights length does not match wih x", {
  x <- c(1, 2, 3, 4, 5)
  expect_error(wtd_median(x, weights = 1))
})

test_that("zero weight", {
  x <- c(1, 2, 3, 4, 5)
  w <- c(0, 4, 3, 2, 1)
  expect_equal(wtd_median(x, weights = w),
               wtd_median(x[-1], weights = w[-1]))
})

test_that("NA values in x", {
  x <- c(NA, 2, 3, 4, 5)
  w <- c(5, 4, 3, 2, 1)
  expect_true(is.na(wtd_median(x, weights = w)))
})

test_that("NA values in w", {
  x <- c(1, 2, 3, 4, 5)
  w <- c(NA, 4, 3, 2, 1)
  expect_true(is.na(wtd_median(x, weights = w)))
})

test_that("NA values with zero weight", {
  x <- c(NA, 2, 3, 4, 5)
  w <- c(0, 4, 3, 2, 1)
  expect_equal(wtd_median(x, weights = w), 3)
})
jcpernias/ec1047 documentation built on Nov. 19, 2020, 2:33 a.m.