stRoke/tests/testthat/test-quantile_cut.R

63 lines
1.7 KiB
R
Raw Permalink Normal View History

2022-09-23 09:14:47 +02:00
test_that("quatile_cut() works for detail.list==FALSE", {
2023-01-12 13:44:29 +01:00
result <- quantile_cut(iris$Sepal.Length, 3, detail.list = FALSE)
2022-09-23 09:14:47 +02:00
expect_equal(length(levels(result)), 3)
expect_s3_class(result, "factor")
})
################################################################################
2023-01-11 12:54:08 +01:00
test_that("quatile_cut() works for inc.outs==TRUE", {
2023-01-12 13:44:29 +01:00
result <-
quantile_cut(iris$Sepal.Length,
3,
y = iris$Sepal.Length + 3,
inc.outs = FALSE)
2023-01-11 12:54:08 +01:00
expect_true(any(is.na(result)))
2023-01-12 13:44:29 +01:00
result <-
quantile_cut(iris$Sepal.Length,
3,
y = iris$Sepal.Length + 3,
inc.outs = TRUE)
2023-01-11 12:54:08 +01:00
expect_false(any(is.na(result)))
expect_equal(length(levels(result)), 3)
expect_s3_class(result, "factor")
})
################################################################################
test_that("quatile_cut() works for detail.list==TRUE", {
2023-01-12 13:44:29 +01:00
result <- quantile_cut(iris$Sepal.Length, 3, detail.list = TRUE)
2022-09-23 09:14:47 +02:00
expect_length(result, 2)
expect_type(result, "list")
})
2023-01-03 14:12:29 +01:00
################################################################################
2023-01-12 13:44:29 +01:00
# Test created using remotes::install_github("JamesHWade/gpttools")
# unit test addin.
2023-01-03 14:12:29 +01:00
test_that("quantile_cut works correctly", {
x <- runif(100)
groups <- 5
y <- runif(100)
2023-01-12 13:44:29 +01:00
expect_equal(
quantile_cut(x, groups, y, na.rm = TRUE),
cut(
x,
quantile(
y,
probs = seq(0, 1, 1 / groups),
na.rm = TRUE,
names = TRUE,
type = 7
),
include.lowest = TRUE,
labels = NULL,
ordered_result = FALSE
)
)
2023-01-03 14:12:29 +01:00
})
################################################################################