# tests/testthat/test-univariate.R In twolodzko/misster: Simple tools for dealing with missing values

```context("Univariate")

test_that("The missing values are replaced.", {

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

expect_false(anyNA(na_const(x)))
expect_false(anyNA(na_mean(x)))
expect_false(anyNA(na_median(x)))
expect_message(expect_false(anyNA(na_mode(x))))
expect_false(anyNA(na_interp(x)))

expect_false(anyNA(na_interp(c(NA, 1, 2, NA))))
expect_false(anyNA(na_interp(c(NA, NA, 1, 2, NA))))
expect_false(anyNA(na_interp(c(NA, 1, 2, NA, NA))))
expect_false(anyNA(na_interp(c(NA, 1, NA, 2, NA))))

expect_false(anyNA(na_random(x, replace = TRUE)))
expect_false(anyNA(na_random(c(1, 2, NA, NA), replace = FALSE)))
expect_false(anyNA(na_density(x, preserve_var = TRUE)))
expect_false(anyNA(na_density(x, preserve_var = FALSE)))

})

test_that("The missing values are replaced by the interpolating functions.", {

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

expect_false(anyNA(na_prev(x)))
expect_false(anyNA(na_mid(x)))
expect_warning(expect_false(anyNA(na_loess(x))))

})

test_that("Interpolation works as expected.", {

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

expect_equal(as.numeric(na_interp(x)), 1:7)
expect_warning(expect_equal(as.numeric(na_loess(x)), 1:7, tolerance = 0.15))

})

test_that("C++ code identifies NAs correctly.", {

test_cases <- list(
c(NA, 1, 2, NA),
c(NA, NA, 1, 2, NA),
c(NA, 1, 2, NA, NA),
c(NA, 1, NA, 2, NA),
c(NA, NA, NA, 2, NA)
)

# na_interp

expect_identical(na_positions(na_interp(test_cases[[1]])),
which(is.na(test_cases[[1]])))

expect_identical(na_positions(na_interp(test_cases[[2]])),
which(is.na(test_cases[[2]])))

expect_identical(na_positions(na_interp(test_cases[[3]])),
which(is.na(test_cases[[3]])))

expect_identical(na_positions(na_interp(test_cases[[4]])),
which(is.na(test_cases[[4]])))

expect_identical(na_positions(na_interp(test_cases[[5]])),
which(is.na(test_cases[[5]])))

# na_mid

expect_identical(na_positions(na_mid(test_cases[[1]])),
which(is.na(test_cases[[1]])))

expect_identical(na_positions(na_mid(test_cases[[2]])),
which(is.na(test_cases[[2]])))

expect_identical(na_positions(na_mid(test_cases[[3]])),
which(is.na(test_cases[[3]])))

expect_identical(na_positions(na_mid(test_cases[[4]])),
which(is.na(test_cases[[4]])))

expect_identical(na_positions(na_mid(test_cases[[5]])),
which(is.na(test_cases[[5]])))

# na_prev

expect_identical(na_positions(na_prev(test_cases[[1]])),
which(is.na(test_cases[[1]])))

expect_identical(na_positions(na_prev(test_cases[[2]])),
which(is.na(test_cases[[2]])))

expect_identical(na_positions(na_prev(test_cases[[3]])),
which(is.na(test_cases[[3]])))

expect_identical(na_positions(na_prev(test_cases[[4]])),
which(is.na(test_cases[[4]])))

expect_identical(na_positions(na_prev(test_cases[[5]])),
which(is.na(test_cases[[5]])))

# no class attributes:

expect_true(is_imputed(na_prev(c(1, 2, NA, 4, NA, NA, 7))))
expect_true(is_imputed(na_mid(c(1, 2, NA, 4, NA, NA, 7))))
expect_true(is_imputed(na_interp(c(1, 2, NA, 4, NA, NA, 7))))

})

test_that("After replacing missing values, vectors do not change their length.", {

x <- c(NA, 1, 2, 2, NA, 4, 5, NA, NA, 7, 9, NA, NA)
n <- length(x)

expect_identical(length(na_const(x)), n)
expect_identical(length(na_mean(x)), n)
expect_identical(length(na_median(x)), n)
expect_identical(length(na_mode(x)), n)
expect_identical(length(na_interp(x)), n)
expect_identical(length(na_mid(x)), n)
expect_identical(length(na_prev(x)), n)
expect_identical(length(na_random(x, replace = TRUE)), n)
expect_identical(length(na_random(x, replace = FALSE)), n)
expect_identical(length(na_density(x, preserve_var = TRUE)), n)
expect_identical(length(na_density(x, preserve_var = FALSE)), n)

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