File: test-weighted-stats.R

package info (click to toggle)
r-cran-datawizard 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: sh: 13; makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,732 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
test_that("weighted centrality and dispersion measures work as expected", {
  x <- c(3.7, 3.3, 3.5, 2.8)
  wt <- c(5, 5, 4, 1) / 15

  set.seed(123)
  expect_equal(weighted_mean(x, wt), 3.453333, tolerance = 0.001)
  expect_equal(weighted_median(x, wt), 3.5, tolerance = 0.001)
  expect_equal(weighted_sd(x, wt), 0.2852935, tolerance = 0.001)
  expect_equal(weighted_mad(x, wt), 0.29652, tolerance = 0.001)
})

test_that("weighted centrality and dispersion measures work with NA", {
  x <- c(3.7, 3.3, NA, 3.5, 2.8, 5.5)
  wt <- c(5, 5, 4, NA, 1, 7) / 15

  set.seed(123)
  expect_equal(weighted_mean(x, wt), 4.238889, tolerance = 0.001)
  expect_equal(weighted_median(x, wt), 3.7, tolerance = 0.001)
  expect_equal(weighted_sd(x, wt), 1.237671, tolerance = 0.001)
  expect_equal(weighted_mad(x, wt), 0.59304, tolerance = 0.001)
})

test_that("weighted centrality and dispersion measures work with NA when not removed", {
  x <- c(3.7, 3.3, NA, 3.5, 2.8, 5.5)
  wt <- c(5, 5, 4, NA, 1, 7) / 15

  set.seed(123)
  expect_identical(weighted_mean(x, wt, remove_na = FALSE), NA_real_)
  expect_identical(weighted_median(x, wt, remove_na = FALSE), NA_real_)
  expect_identical(weighted_sd(x, wt, remove_na = FALSE), NA_real_)
  expect_identical(weighted_mad(x, wt, remove_na = FALSE), NA_real_)
})

test_that("weighted centrality and dispersion measures work with Inf", {
  x <- c(3.7, 3.3, NA, 3.5, 2.8, 5.5, Inf, 4)
  wt <- c(5, 5, 4, NA, 1, 7, 3, Inf) / 15

  set.seed(123)
  expect_equal(weighted_mean(x, wt), 4.238889, tolerance = 0.001)
  expect_equal(weighted_median(x, wt), 3.7, tolerance = 0.001)
  expect_equal(weighted_sd(x, wt), 1.237671, tolerance = 0.001)
  expect_equal(weighted_mad(x, wt), 0.59304, tolerance = 0.001)
})