tests/testthat/test-max_entropy.R

context('Test the max_entropy function')

test_that('max_entropy gives correct result', {
  set.seed(33)
  a <- matrix(sample(0:1, 400, prob = c(0.85,0.15), replace=TRUE), ncol=20)
  diag(a) <- 0
  
  awTrue <- a * abs(rnorm(400, 40, 15))
  
  rs <- rowSums(awTrue)
  cs <- colSums(awTrue)
  
  tw <- structure(c(0, 2.84564742280768, 3.48225846179055, 9.36211549181309,
  6.33484708694693, 1.46509766755487, 7.58473222530686, 8.24955079285421, 
  5.41303909880594, 9.00657590209478, 6.74525473592549, 0.724430694545076, 
  5.87047363023096, 2.20327694127062, 1.93541149637747, 5.3967755298465, 
  3.55062282994075, 9.90359679338189, 2.58021493072636, 5.24056014789618, 
  12.7458211575781, 0, 3.65395804563229, 9.82373295974681, 6.64719914824986, 
  1.53733718181463, 7.95871232498235, 8.65631107603146, 5.67993960914488, 
  9.45066279317337, 7.07784275136804, 0.76015017089653, 6.15992884739089, 
  2.31191383254541, 2.03084075647887, 5.66287413301722, 3.72569325304953, 
  10.3919130589947, 2.7074374889279, 5.49895624524637, 7.25090838400685, 
  1.69866696667674, 0, 5.58857580060443, 3.78149288604695, 0.874568293627565, 
  4.52759325661829, 4.92444682690767, 3.23123329783385, 5.37634172284775, 
  4.02647963690152, 0.432438144166243, 3.5042920477412, 1.31521344794158, 
  1.15531514883844, 3.22152500188326, 2.11949156596419, 5.91180502172154, 
  1.54022098262142, 3.12827454967369, 25.4500876369791, 5.9621803060315, 
  7.29600324165048, 0, 13.2727267056333, 3.06966224624191, 15.8914771864565, 
  17.2844003359463, 11.3413611440437, 18.870513977893, 14.1326099021856, 
  1.51782205535492, 12.2997747313005, 4.61629022720458, 4.0550604460982, 
  11.3072858296606, 7.43923978112915, 20.7499457898825, 5.40604803040801, 
  10.9799844688882, 3.79718499956287, 0.889564780510096, 1.0885728356276, 
  2.9266479556936, 0, 0.457997457667609, 2.37102833019835, 2.57885421135962, 
  1.69214530908782, 2.81550435633007, 2.10860312509409, 0.226460954587247, 
  1.83514181852304, 0.688756292489159, 0.60502010515169, 1.6870612294384, 
  1.10994390699112, 3.09591794020388, 0.806589147384892, 1.63822745585031, 
  13.5766997307673, 3.18060719123294, 3.89215340471618, 10.4641255342287, 
  7.08051884383643, 0, 8.47752735143305, 9.22060138789255, 6.05020528758975, 
  10.0667355530376, 7.53923537674769, 0.809703077813677, 6.56148421431398, 
  2.46262359402513, 2.16322783842961, 6.03202734187065, 3.9685649092546, 
  11.0693443353648, 2.88393077013654, 5.85742392360101, 9.34823617664047, 
  2.19000698243226, 2.67994210555807, 7.20507331058545, 4.87529102933143, 
  1.12753747923016, 0, 6.34884479836744, 4.16586866227699, 6.93145045141691, 
  5.19114028377627, 0.557521029002469, 4.51790975134417, 1.69563939894347, 
  1.48949046075578, 4.15335223831843, 2.73255524462741, 7.62179669725229, 
  1.98573044192923, 4.03312906010861, 28.5763435361658, 6.69456683528901, 
  8.19223485782588, 22.0249730789902, 14.9031313138079, 3.44673559242092, 
  17.8435657218518, 0, 12.7345193007904, 21.1885435456322, 15.868641451775, 
  1.70426935652548, 13.8106631754143, 5.18334935725595, 4.55317883460832, 
  12.6962582187932, 8.35306638883903, 23.298842334316, 6.07011998911489, 
  12.3287515815391, 8.98304538019188, 2.10445390278918, 2.57524960812091, 
  6.92360561860113, 4.68483676819829, 1.08348999237969, 5.6091697113373, 
  6.10082585183741, 0, 6.66067189350861, 4.98834730562646, 0.535741353695697, 
  4.34141666439168, 1.62939889208324, 1.43130320517219, 3.99110066666547, 
  2.62580740393648, 7.32404961923194, 1.90815746795662, 3.8755740319931, 
  17.1750870630608, 4.02359973381656, 4.92373514288592, 13.237551883238, 
  8.95714938137328, 2.0715730761096, 10.7244229619648, 11.6644423719522, 
  7.65376009077221, 0, 9.53744478056828, 1.02430790489996, 8.30054909355888, 
  3.11532076790941, 2.73657274588378, 7.63076423710053, 5.0203988585968, 
  14.0031787150783, 3.6482917824784, 7.40988368661644, 11.6528104973018, 
  2.72989854683175, 3.34061494700395, 8.98130431462762, 6.07717235749293, 
  1.40550370420747, 7.27621746602181, 7.91399403205317, 5.19285961125637, 
  8.6402265684761, 0, 0.694963924366879, 5.63168764476439, 2.11365697381726, 
  1.85668715988341, 5.17725757534959, 3.40619854241795, 9.50076045189946, 
  2.47526272350133, 5.02739637302321, 16.6956061877919, 3.91127197006093, 
  4.7862780908643, 12.8679960876786, 8.70709057180067, 2.01374049173494, 
  10.4250267673676, 11.3388034382168, 7.44008830128159, 12.3793157190684, 
  9.27118573021093, 0, 8.06882074598318, 3.02834963797824, 2.66017521202467, 
  7.41773442817196, 4.8802432232815, 13.6122487382928, 3.54644157754632, 
  7.20302025106852, 11.754864329775, 2.75380665114973, 3.36987162789118, 
  9.05996143568246, 6.13039546017924, 1.41781292691411, 7.3399416532887, 
  7.98330378538072, 5.23833801536725, 8.71589657400425, 6.52755756268763, 
  0.701050330039455, 0, 2.13216811280486, 1.87294778991886, 5.22259933881408, 
  3.43602959609368, 9.58396690374644, 2.49694075966005, 5.07142567113524, 
  14.5469667365755, 3.40791119568817, 4.17030848695477, 11.2119505544351, 
  7.5865323783782, 1.75458235058177, 9.08337893856744, 9.8795572076274, 
  6.48258684461951, 10.7861608594082, 8.07803136400099, 0.867568994898341, 
  7.03040462711894, 0, 2.31782421599533, 6.46310980108472, 4.25218078558807, 
  11.8604216809288, 3.09003261585663, 6.2760282445924, 21.9391338272271, 
  5.13967077449363, 6.2894868499342, 16.9094002985196, 11.4416944884699, 
  2.64618856270005, 13.699176587487, 14.8999397370526, 9.77676947408832, 
  16.2672418835999, 12.182952938894, 1.30843169081916, 10.6029656056139, 
  3.97945227240812, 0, 9.74739500225465, 6.41296326594487, 17.887397642218, 
  4.66025944221925, 9.46524633313238, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22.6917668449369, 5.31598976481954, 
  6.50525085889599, 17.4894857784005, 11.8342075711826, 2.73696739193999, 
  14.1691337287477, 15.411089661945, 10.1121664669998, 16.8252977961571, 
  12.6008952654792, 1.35331809789811, 10.9667056722578, 4.11596938362139, 
  3.61556657476047, 10.0817842891394, 0, 18.5010338127458, 4.82013198572664, 
  9.78995636804864, 10.8568844022295, 2.54343730721694, 3.11243972605405, 
  8.36785106457665, 5.6620810741754, 1.30950308056748, 6.7792273745783, 
  7.37344165905986, 4.83816985863377, 8.05007007405959, 6.02890309056889, 
  0.6474955541685, 5.24702446356854, 1.96928710339997, 1.72986919083816, 
  4.82363349418172, 3.17354374162671, 0, 2.30619396586121, 4.68400831531063, 
  11.9663571513724, 2.80335297703205, 3.43050215828349, 9.22296772429977, 
  6.24069317157837, 1.44332213297537, 7.47200144803109, 8.12693891343259, 
  5.33258588214568, 8.87271246804382, 6.6450009910712, 0.713663585983745, 
  5.78322163033769, 2.1705299549875, 1.90664575543185, 5.31656403625775, 
  3.4978504367247, 9.75640106765555, 0, 5.16267046091927, 17.8411073353076, 
  4.17962799616478, 5.11466910486793, 13.7508813281942, 9.30449219527775, 
  2.15190510930768, 11.1402975991409, 12.1167693415711, 7.9505597145908, 
  13.2286721426777, 9.90729044981993, 1.06402879989055, 8.62243008005939, 
  3.23612753752641, 2.8426923200405, 7.92667212135082, 5.21508127285842, 
  14.5461978488908, 3.78976624937808, 0), .Dim = c(20L, 20L))

  expect_equal(max_entropy(rs,cs, minError=1e-18), tw)
})

test_that('max_entropy fails when it should', {
  expect_false(max_entropy(c(10, 3,1), c(5,0,9)))
})

test_that('max_entropy deals with missings', {
  # Should give a warning for missings
  expect_warning(max_entropy(c(1,2,NA), c(1,2,2)))
  
  # Must give a sensible result for missings
  expect_equal(suppressWarnings(max_entropy(c(1,2,NA), c(1,2,2))), 
  structure(c(0, 0.5000000003705, 0.4999999996295, 0.5000000003705, 
  0, 1.4999999996295, 0.49999999981475, 1.50000000018525, 0), .Dim = c(3L, 3L)))
  
})
dougmet/unsumnet documentation built on May 15, 2019, 10:44 a.m.