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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
test_that("unnormalize work as expected", {
x <- normalize(c(0, 1, 5, -5, -2))
expect_equal(
unnormalize(x),
c(0, 1, 5, -5, -2),
ignore_attr = TRUE
)
expect_error(
unnormalize(c(0, 1, 5, -5, -2)),
"Can't unnormalize variable"
)
})
test_that("unnormalize error if not supported", {
expect_error(
unnormalize(c("a", "b")),
"can't be unnormalized"
)
})
test_that("unnormalize and unstandardized x 4", {
set.seed(123)
x <- rnorm(6, 4, 10)
z <- standardise(x)
expect_named(attributes(z), c("center", "scale", "robust", "class"))
expect_equal(attributes(z)$center, 8.47, tolerance = 0.01)
expect_equal(unstandardise(z), x, ignore_attr = TRUE)
z <- center(x)
expect_named(attributes(z), c("center", "scale", "robust", "class"))
expect_equal(unstandardise(z), x, ignore_attr = TRUE)
z <- normalize(x)
expect_named(attributes(z), c(
"include_bounds", "flag_bounds", "min_value",
"vector_length", "range_difference", "class"
))
expect_equal(unnormalize(z), x, ignore_attr = TRUE)
z <- change_scale(x, to = c(-3, 14.5))
expect_named(attributes(z), c(
"min_value", "max_value", "new_min", "new_max",
"range_difference", "to_range", "class"
))
expect_equal(unnormalize(z), x, ignore_attr = TRUE)
z <- change_scale(x, range = c(-100, 100))
expect_named(attributes(z), c(
"min_value", "max_value", "new_min", "new_max",
"range_difference", "to_range", "class"
))
expect_equal(unnormalize(z), x, ignore_attr = TRUE)
})
# select helpers ------------------------------
test_that("unnormalize regex", {
x <- normalize(mtcars, select = "mpg")
expect_identical(
unnormalize(x, select = "pg", regex = TRUE),
unnormalize(x, select = "mpg")
)
})
test_that("unnormalize: grouped data", {
skip_if_not_installed("poorman")
# 1 group, 1 normalized var
norm <- poorman::group_by(mtcars, cyl)
norm <- normalize(norm, "mpg")
expect_identical(
poorman::ungroup(unnormalize(norm, "mpg")),
mtcars,
ignore_attr = TRUE # unnormalize removed rownames
)
# 2 groups, 1 normalized var
set.seed(123)
test <- iris
test$grp <- sample(c("A", "B"), nrow(test), replace = TRUE)
norm <- poorman::group_by(test, Species, grp)
norm <- normalize(norm, "Sepal.Length")
expect_identical(
poorman::ungroup(unnormalize(norm, "Sepal.Length")),
test
)
# 2 groups, 2 normalized vars
set.seed(123)
test <- iris
test$grp <- sample(c("A", "B"), nrow(test), replace = TRUE)
norm <- poorman::group_by(test, Species, grp)
norm <- normalize(norm, c("Sepal.Length", "Petal.Length"))
unnorm <- unnormalize(norm, c("Sepal.Length", "Petal.Length"))
expect_identical(
poorman::ungroup(unnorm),
test
)
expect_s3_class(unnorm, "grouped_df")
# can't recover attributes
norm <- poorman::group_by(iris, Species)
norm <- normalize(norm, "Sepal.Length")
attr(norm, "groups") <- NULL
expect_error(
unnormalize(norm, "Sepal.Length"),
regexp = "Couldn't retrieve the necessary information"
)
# normalize applied on grouped data but unnormalize applied on ungrouped data
norm <- poorman::group_by(mtcars, cyl)
norm <- normalize(norm, "mpg")
norm <- poorman::ungroup(norm)
expect_error(
unnormalize(norm, "mpg"),
regexp = "Can't unnormalize variable"
)
# normalize applied on grouped data but unnormalize applied different grouped
# data
norm <- poorman::group_by(norm, am)
expect_error(
unnormalize(norm, "mpg"),
regexp = "Couldn't retrieve the necessary"
)
})
|