Skip to content

Commit

Permalink
more tests for binning methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mjskay committed Aug 26, 2019
1 parent f01a520 commit 6a55dd1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
36 changes: 18 additions & 18 deletions R/binning_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ wilkinson_bin_to_right = function(x, width, direction = 1) {
# the basic wilkinson method, but sweeping right-to-left
# x must be sorted
wilkinson_bin_to_left = function(x, width) {
if (length(x) == 0) {
return(list(
bins = integer(0),
bin_midpoints = NULL
))
}

binning = wilkinson_bin_to_right(rev(x), width, direction = -1)
list(
# renumber bins so 1,2,3,3 => 3,2,1,1 (then reverse so it matches original vector order)
Expand Down Expand Up @@ -110,22 +117,15 @@ wilkinson_bin_from_center = function(x, width) {
n_center = 1 + edge_offset_from_center * 2 # number of items in center bin
center_midpoint = (x[[center_i - edge_offset_from_center]] + x[[center_i + edge_offset_from_center]])/2

if (n_center == length(x)) {
# everything was in the center bin
list(
bins = rep(1, n_center),
bin_midpoints = center_midpoint
)
} else {
# construct bins for regions left / right of center
left = wilkinson_bin_to_left(x[1:(center_i - edge_offset_from_center - 1)], width)
right = wilkinson_bin_to_right(x[(center_i + edge_offset_from_center + 1):length(x)], width)
center_bin_i = length(left$bin_midpoints) + 1
list(
bins = c(left$bins, rep(center_bin_i, n_center), center_bin_i + right$bins),
bin_midpoints = c(left$bin_midpoints, center_midpoint, right$bin_midpoints)
)
}
# construct bins for regions left / right of center
left = wilkinson_bin_to_left(x[1:(center_i - edge_offset_from_center - 1)], width)
right = wilkinson_bin_to_right(x[(center_i + edge_offset_from_center + 1):length(x)], width)

center_bin_i = length(left$bin_midpoints) + 1
list(
bins = c(left$bins, rep(center_bin_i, n_center), center_bin_i + right$bins),
bin_midpoints = c(left$bin_midpoints, center_midpoint, right$bin_midpoints)
)
}
}

Expand Down Expand Up @@ -189,8 +189,8 @@ nudge_bins = function(binning, width) {
bin_midpoints[[right_center_bin]] = bin_midpoints[[right_center_bin]] + center_bin_nudge
} else {
# odd number of bins => don't need to adjust the center
left_center_bin = ceiling(length(bin_midpoints) / 2)
right_center_bin = left_center_bin
left_center_bin = floor(length(bin_midpoints) / 2)
right_center_bin = left_center_bin + 2
}

# nudge the left bins (those below the center) apart as necessary
Expand Down
61 changes: 61 additions & 0 deletions tests/testthat/test.binning_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,64 @@ test_that("binning works on symmetric distributions", {
list(bins = c(1, 2), bin_midpoints = c(1, 2))
)
})

test_that("binning works on empty data", {

expect_equal(automatic_bin(NULL, width = 1),
list(bins = integer(0), bin_midpoints = NULL)
)

expect_equal(wilkinson_bin_from_center(NULL, width = 1),
list(bins = integer(0), bin_midpoints = NULL)
)

expect_equal(wilkinson_bin_to_right(NULL, width = 1),
list(bins = integer(0), bin_midpoints = NULL)
)

expect_equal(wilkinson_bin_to_left(NULL, width = 1),
list(bins = integer(0), bin_midpoints = NULL)
)
})


test_that("bin nudging works", {

# when no nudging required
expect_equal(
nudge_bins(list(bins = integer(0), bin_midpoints = NULL), width = 1),
list(bins = integer(0), bin_midpoints = NULL)
)
expect_equal(
nudge_bins(list(bins = c(1), bin_midpoints = c(1)), width = 1),
list(bins = c(1), bin_midpoints = c(1))
)
expect_equal(
nudge_bins(list(bins = c(1,2), bin_midpoints = c(1,2)), width = 1),
list(bins = c(1,2), bin_midpoints = c(1,2))
)
expect_equal(
nudge_bins(list(bins = c(1,2,3,4), bin_midpoints = c(1,2,3,4)), width = 1),
list(bins = c(1,2,3,4), bin_midpoints = c(1,2,3,4))
)
expect_equal(
nudge_bins(list(bins = c(1,2,3), bin_midpoints = c(1,2,3)), width = 1),
list(bins = c(1,2,3), bin_midpoints = c(1,2,3))
)

# on even number of bins
expect_equal(
nudge_bins(list(bins = c(1,2), bin_midpoints = c(1,2)), width = 1.1),
list(bins = c(1,2), bin_midpoints = c(0.95,2.05))
)
expect_equal(
nudge_bins(list(bins = c(1,2,3,4), bin_midpoints = c(1,2,3,4)), width = 1.1),
list(bins = c(1,2,3,4), bin_midpoints = c(.85,1.95,3.05,4.15))
)

# on odd number of bins
expect_equal(
nudge_bins(list(bins = c(1,2,3), bin_midpoints = c(1,2,3)), width = 1.1),
list(bins = c(1,2,3), bin_midpoints = c(0.9,2,3.1))
)
})

0 comments on commit 6a55dd1

Please sign in to comment.