tests/testthat/test-make_validation_plot.R

context("make validation plot")

validation_data <- data.frame(dep_var = rep(c(0, 1), 30), resume = rep(c("cool", "story", "bro"), 20))

test_that("dep_var must be declared", {
  expect_error(make_validation_plot(models, data.frame(bull="crap")))
})

test_that("Can plot with a vector of predictions instead of models", {
  models <- list(jitter(rep(0.42, NROW(validation_data))), jitter(rep(0.24, NROW(validation_data))))
  with_mock(
    `base::plot` = function(...) TRUE,
    chart <- make_validation_plot(models, validation_data),

    expect_equal(chart[[1]]$model_name, "Model 1"),
    expect_equal(length(chart), length(models)),
    expect_equal(length(chart[[1]]$bucket_size), 10),
    expect_equal(length(chart[[1]]$mean_response), 10),
    expect_true(chart[[1]]$roc > 0 && chart[[1]]$roc < 1)
  )
})

test_that("Can plot with a vector of predictions instead of one model", {
  models <- jitter(rep(0.42, NROW(validation_data)))
  with_mock(
    `base::plot` = function(...) TRUE,
    chart <- make_validation_plot(models, validation_data),

    expect_equal(chart[[1]]$model_name, "Model 1"),
    expect_equal(length(chart), 1),
    expect_equal(length(chart[[1]]$bucket_size), 10),
    expect_equal(length(chart[[1]]$mean_response), 10),
    expect_true(chart[[1]]$roc > 0 && chart[[1]]$roc < 1)
  )
})

test_that("Can plot pretty charts", {
  models <- jitter(rep(0.42, NROW(validation_data)))
  with_mock(
    `base::plot` = function(...) TRUE,
    chart <- make_validation_plot(models, validation_data, buckets = 3,
                                  title = "Fake", inc = 15, line_types = 1,
                                  linecolors = 'red'),

    expect_equal(chart[[1]]$model_name, "Model 1"),
    expect_equal(length(chart), 1),
    expect_equal(length(chart[[1]]$bucket_size), 3),
    expect_equal(length(chart[[1]]$mean_response), 3),
    expect_true(chart[[1]]$roc > 0 && chart[[1]]$roc < 1)
  )
})
avantcredit/make_validation_plot documentation built on May 11, 2019, 4:07 p.m.