tests/testthat/test-MI.R

context("MI")

data(danedb)

x <- danedb[, 1L:2L]
x_err <- x
x_err[1, 2] <- x_err[8, 1] <- NA_integer_

test_that("MI_categorical_two errors", {
  expect_error(MI_categorical_two(x_err))
  expect_error(MI_categorical_two(data.frame(x = x[, 1], y = x[, 2])))
  expect_error(MI_categorical_two(x + 0.5))
})

test_that("MI_categorical_all errors", {
  expect_error(MI_categorical_all(x_err))
  expect_error(MI_categorical_all(data.frame(x = x[, 1], y = x[, 2])))
  expect_error(MI_categorical_all(x + 0.5))
})

mi_all <- MI_categorical_all(danedb)
test_that("MI_categorical_all", {
  expect_equal(MI_categorical_two(x), mi_all[1, 2])
  expect_true(isSymmetric(mi_all))
})

mi_all_ord <- MI_order(mi_all)
test_that("MI_order descendingly", {
  expect_true(nrow(mi_all_ord) == ncol(mi_all)*(ncol(mi_all) - 1L)/2L)
  
  for (i in 2L:nrow(mi_all_ord)) {
    x <- mi_all_ord[i-1L, ]
    y <- mi_all_ord[i, ]
    expect_true(mi_all[x[1], x[2]] >= mi_all[y[1], y[2]])
  }
})

if (FALSE) {
  library(entropy)
  
  n <- ncol(danedb)
  # On purpose that the symmetry not exploited
  ent_tab <- do.call(rbind, 
          lapply(seq_len(n), 
                 function(i) 
                   do.call(cbind, 
                           lapply(seq_len(n), 
                                  function(j) 
                                    mi.plugin(prop.table(table(danedb[, i], danedb[, j])), unit = "log")))))
  ent_tab
  dput(ent_tab)
}
# See above, by entropy package
ent_tab <- structure(c(1.03428799401102, 0.0797449194715118, 0.212554460022178, 
            0.146421395679519, 0.0388402771786957, 0.162412975316272, 0.0527394034255984, 
            0.167906084647392, 0.173827412125135, 0.174600623826692, 0.206859655487181, 
            0.239061534034384, 0.0797449194715118, 1.0573887441353, 0.561196634042672, 
            0.201675229352434, 0.0975179999544116, 0.174326890901367, 0.0636812864366659, 
            0.313880698159844, 0.0539246559756922, 0.061446240228451, 0.255848938890055, 
            0.324603419458728, 0.212554460022178, 0.561196634042672, 1.51102383646715, 
            0.228195500546128, 0.0764358918374833, 0.163137670792497, 0.077378986255133, 
            0.278689534682118, 0.107164355933147, 0.148859681104428, 0.284421191944237, 
            0.336351225780712, 0.146421395679519, 0.201675229352434, 0.228195500546128, 
            1.48923987593415, 0.158522819574341, 0.195152703023822, 0.0604303662641546, 
            0.366738392508147, 0.120212693525364, 0.13807854554992, 0.400876785806704, 
            0.369017026899429, 0.0388402771786957, 0.0975179999544116, 0.0764358918374833, 
            0.158522819574341, 0.810781712041024, 0.179242723220789, 0.0406120067021102, 
            0.253973445610641, 0.0466087874059225, 0.0457812064949348, 0.256852405441464, 
            0.178614217585147, 0.162412975316272, 0.174326890901367, 0.163137670792497, 
            0.195152703023822, 0.179242723220789, 1.11070364766576, 0.110602085598739, 
            0.394240583772531, 0.122393835315044, 0.188448597791934, 0.617578603633353, 
            0.398138765715564, 0.0527394034255984, 0.0636812864366659, 0.077378986255133, 
            0.0604303662641546, 0.0406120067021102, 0.110602085598739, 0.735809628059841, 
            0.123796591610341, 0.134754792265077, 0.0671077005825808, 0.148539668541404, 
            0.0712001670350926, 0.167906084647392, 0.313880698159844, 0.278689534682118, 
            0.366738392508147, 0.253973445610641, 0.394240583772531, 0.123796591610341, 
            1.52009178968598, 0.231754086880936, 0.1776303035885, 0.512619361337671, 
            0.407867101004431, 0.173827412125135, 0.0539246559756922, 0.107164355933147, 
            0.120212693525364, 0.0466087874059225, 0.122393835315044, 0.134754792265077, 
            0.231754086880936, 1.32372109288152, 0.0768003382404416, 0.169047485530117, 
            0.0463361947513026, 0.174600623826692, 0.061446240228451, 0.148859681104428, 
            0.13807854554992, 0.0457812064949348, 0.188448597791934, 0.0671077005825808, 
            0.1776303035885, 0.0768003382404416, 1.17794860036784, 0.257096648836906, 
            0.200536710316495, 0.206859655487181, 0.255848938890055, 0.284421191944237, 
            0.400876785806704, 0.256852405441464, 0.617578603633353, 0.148539668541404, 
            0.512619361337671, 0.169047485530117, 0.257096648836906, 1.19247418816907, 
            0.553436047077869, 0.239061534034384, 0.324603419458728, 0.336351225780712, 
            0.369017026899429, 0.178614217585147, 0.398138765715564, 0.0712001670350926, 
            0.407867101004431, 0.0463361947513026, 0.200536710316495, 0.553436047077869, 
            1.08222854292777), .Dim = c(12L, 12L))


mi_all <- MI_categorical_all(danedb)
test_that("MI_categorical_all same as entropy package", {
  expect_equal(mi_all, ent_tab)
})
mikldk/structlearn documentation built on May 13, 2018, 12:48 a.m.