tests/testthat/test-misc.R

context("Miscellaneous")


test_that("Imputed object.", {

  x <- c(NA, 1, 2, NA, 4, NA, NA, 7, NA)

  expect_true(is_imputed(as_imputed(x)))

})


test_that("Restoring missing values works properly.", {

  x <- c(NA, 1, 2, NA, 4, NA, NA, 7, NA)

  expect_identical(na_restore(na_const(x, value = 0)), x)
  expect_identical(na_restore(na_prev(x)), x)
  expect_identical(na_restore(na_mid(x)), x)
  expect_identical(na_restore(na_interp(x)), x)

  x <- c(NA, 1, 2, NA, 4, NA, NA, 7, NA)
  class(x) <- "foo"

  expect_identical(na_restore(na_const(x, value = 0)), x)
  expect_identical(na_restore(na_prev(x)), x)
  expect_identical(na_restore(na_mid(x)), x)
  expect_identical(na_restore(na_interp(x)), x)

  x <- c(NA, 1, 2, NA, 4, NA, NA, 7, NA)
  class(x) <- c("numeric", "foo")

  expect_identical(na_restore(na_const(x, value = 0)), x)
  expect_identical(na_restore(na_prev(x)), x)
  expect_identical(na_restore(na_mid(x)), x)
  expect_identical(na_restore(na_interp(x)), x)

})


test_that("Information on missing values is inherited between imputed class objects.", {

  x <- c(NA, 1, 2, NA, 4, NA, NA, 7, NA)

  expect_identical(na_positions(na_mid(as_imputed(x))), which(is.na(x)))
  expect_identical(na_positions(na_interp(as_imputed(x))), which(is.na(x)))
  expect_identical(na_positions(na_prev(as_imputed(x))), which(is.na(x)))

})


test_that("Dimensionality reduction and plotting returns no errors and warnings.", {

  dat <- mtcars
  dat$disp[sample.int(nrow(dat), 10)] <- NA

  expect_silent(na_scatter(dat, disp ~ mpg + drat))
  expect_error(na_scatter(dat, disp ~ mpg + drat + cyl))
  expect_warning(na_scatter(mtcars, disp ~ mpg + drat))

  expect_silent(na_pca(dat, disp ~ .))
  expect_silent(na_tsne(dat, disp ~ ., perplexity = 10))

  expect_equal(ncol(na_pca(dat, disp ~ .)), ncol(dat))
  expect_equal(ncol(na_tsne(dat, disp ~ ., perplexity = 10)), 3L)

})


test_that("Other tests.", {

  x <- c(NA, 1, 2, NA, 4, NA, NA, 7, NA)

  expect_output(print(as_imputed(x)))
  expect_output(print(na_prev(x)))

})
twolodzko/misster documentation built on May 24, 2019, 2:54 p.m.