Skip to content

Commit

Permalink
DS-4360 Housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwishart committed Mar 31, 2023
1 parent db20a1f commit 7e40f8b
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions tests/testthat/test-CART.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context("CART")

data("spam7", package = "flipTrees")
spam.sample <- spam7[sample(seq(1,4601), 500, replace=FALSE), ]
spam.sample <- spam7[sample(seq(1, 4601), 500, replace = FALSE), , drop = FALSE]
data(cola, package = "flipExampleData")
colas <- cola
data(bank, package = "flipExampleData")
Expand Down Expand Up @@ -48,7 +48,7 @@ library(hot.deck)

test_that("Error if missing data",
{
type = "Sankey"
type <- "Sankey"
# Changing data
expect_error((CART(yesno ~ crl.tot + dollar + bang + money + n000 + make,
data = spam.sample, missing = "Error if missing data")),NA)
Expand Down Expand Up @@ -134,50 +134,42 @@ test_that("CART: many levels", {
expect_error(CART(yesno ~ ., data = spam7, early.stopping = FALSE), NA)
})

food <- structure(c(19.55, 27.24, 28.76, 31.19, 21.91, 27.62, 29.46,
26.35, 26.69, 30.22, 27.81, 23.46, 23.64, 27.85, 20.62, 25.35,
28, 23.49, 27.77, 18.48, 23.01, 22.66, 23.24, 22.82, 17.94, 26.67,
28.98, 21.48, 14.47, 28.29, 27.97, 23.53, 26.39, 30.9, 26.05,
23.27, 29.17, 30.93, 17.55, 32.55, 28.87, 26.53, 25.26, 25.65,
29.39, 23.26, 24.77, 25.42, 23.65, 32.22, 18.86, 21.75, 23.07,
22.3, 27.04, 22.24, 24.87, 30.85, 21.15, 16.47, 29.05, 26.99,
21.34, 29.94, 32.95, 29.4, 22.32, 28.36, 28.49, 22.24, 36.15,
30.62, 26.53, 27.95, 31.49, 30.24, 24.8, 26.43, 29.35, 21.15,
29.18, 21.6, 25.39, 22.26, 24.85, 24.56, 16.35, 22.96, 25.82,
19.46, 23.6, 33.1, 27.13, 24.4, 25.88, 27.97, 24.54, 22.66, 28.94,
30.72, 16.7, 30.27, 26.29, 22.33, 24.85, 24.33, 24.5, 22.67,
22.28, 23.8, 25.36, 29.5, 20.19, 20.14, 21.09, 24.78, 24.74,
22.73, 21.08, 25.7, 19.79, 16.82, 31.15, 27.84, 22.5, 23.1, 28.26,
25.55, 16.71, 27.88, 31.07, 23.44, 28.82, 27.77, 24.54, 24.55,
27.78, 26.14, 23.44, 26.44, 27.47, 24.94, 29.68, 24.33, 25.42,
24.64, 22.78, 26.5, 18.71, 22.86, 25.09, 19.72, 17.05, 30.91,
25.92, 21.32, 26.18, 25.93, 28.61, 20.54, 26.44, 29.36, 19.77,
31.69, 24.64, 22.09, 23.42, 28.63, 26.3, 22.89, 22.68, 30.92,
20.74, 27.24, 17.12, 23.63, 20.91, 23.49, 24.86, 16.28, 21.52,
27.22, 17.41, 16.42, 28.22, 27.52), questiontype = "Number",
name = "Food.Pinching.Effeciency", label = "Food.Pinching.Effeciency",
question = "Food.Pinching.Effeciency")

chopsticks <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L),
class = "factor", .Label = c("180", "210", "240", "270", "300", "330"),
questiontype = "PickOne", name = "Chopstick.Length_2",
label = "Chopstick.Length (categorical)", question = "Chopstick.Length (categorical)")
food <- structure(
c(19.55, 27.24, 28.76, 31.19, 21.91, 27.62, 29.46, 26.35, 26.69, 30.22, 27.81,
23.46, 23.64, 27.85, 20.62, 25.35, 28, 23.49, 27.77, 18.48, 23.01, 22.66, 23.24,
22.82, 17.94, 26.67, 28.98, 21.48, 14.47, 28.29, 27.97, 23.53, 26.39, 30.9,
26.05, 23.27, 29.17, 30.93, 17.55, 32.55, 28.87, 26.53, 25.26, 25.65, 29.39,
23.26, 24.77, 25.42, 23.65, 32.22, 18.86, 21.75, 23.07, 22.3, 27.04, 22.24,
24.87, 30.85, 21.15, 16.47, 29.05, 26.99, 21.34, 29.94, 32.95, 29.4, 22.32,
28.36, 28.49, 22.24, 36.15, 30.62, 26.53, 27.95, 31.49, 30.24, 24.8, 26.43,
29.35, 21.15, 29.18, 21.6, 25.39, 22.26, 24.85, 24.56, 16.35, 22.96, 25.82,
19.46, 23.6, 33.1, 27.13, 24.4, 25.88, 27.97, 24.54, 22.66, 28.94, 30.72, 16.7,
30.27, 26.29, 22.33, 24.85, 24.33, 24.5, 22.67, 22.28, 23.8, 25.36, 29.5, 20.19,
20.14, 21.09, 24.78, 24.74, 22.73, 21.08, 25.7, 19.79, 16.82, 31.15, 27.84,
22.5, 23.1, 28.26, 25.55, 16.71, 27.88, 31.07, 23.44, 28.82, 27.77, 24.54, 24.55,
27.78, 26.14, 23.44, 26.44, 27.47, 24.94, 29.68, 24.33, 25.42, 24.64, 22.78,
26.5, 18.71, 22.86, 25.09, 19.72, 17.05, 30.91, 25.92, 21.32, 26.18, 25.93,
28.61, 20.54, 26.44, 29.36, 19.77, 31.69, 24.64, 22.09, 23.42, 28.63, 26.3,
22.89, 22.68, 30.92, 20.74, 27.24, 17.12, 23.63, 20.91, 23.49, 24.86, 16.28,
21.52, 27.22, 17.41, 16.42, 28.22, 27.52),
questiontype = "Number",
name = "Food.Pinching.Effeciency",
label = "Food.Pinching.Effeciency",
question = "Food.Pinching.Effeciency"
)

chopsticks <- structure(
factor(rep(c("180", "210", "240", "270", "300", "330"), each = 31L)),
questiontype = "PickOne",
name = "Chopstick.Length_2",
label = "Chopstick.Length (categorical)",
question = "Chopstick.Length (categorical)"
)

test_that("label abbrev",
{
res.abbrv <- CART(food~chopsticks, early.stopping = FALSE)
res.full <- CART(food~chopsticks, early.stopping = FALSE, predictor.level.treatment = "Full labels")
res.abbrv <- CART(food ~ chopsticks, early.stopping = FALSE)
res.full <- CART(food ~ chopsticks, early.stopping = FALSE, predictor.level.treatment = "Full labels")
expect_equal(dim(res.abbrv$splits), dim(res.full$splits))
})

Expand All @@ -189,10 +181,12 @@ adult.2000$age[runif(2000) > 0.9] <- -Inf
adult.2000$hrs_per_week[runif(2000) > 0.9] <- Inf

test_that("Infinity allowed in predictors but not outcome", {
expected.error <- paste0("Variable(s) age contain infinite values. Either recode ",
"the infinities to finite values or set them as missing data.")
expect_error(CART(age ~ sex + race + hrs_per_week + marital,
data = adult.2000,
missing = "Exclude cases with missing data"),
"Variable(s) age contain infinite values. Either recode the infinities to finite values or set them as missing data.",
expected.error,
fixed = TRUE)
expect_error(CART(marital ~ sex + race + hrs_per_week + age,
data = adult.2000,
Expand Down

0 comments on commit 7e40f8b

Please sign in to comment.