tests/testthat/test-diffdensity.plot.R

context("diffdensity.plot")

setosa <- subset(iris, Species == "setosa")
versicolor <- subset(iris, Species == "versicolor")


expected_diffd <- structure(list(x = c(4.3, 4.6, 4.9, 5.2, 5.5, 5.8, 6.1, 6.4,  6.7, 7), y = c(2, 2.26666666666667, 2.53333333333333, 2.8, 3.06666666666667,  3.33333333333333, 3.6, 3.86666666666667, 4.13333333333333, 4.4 ), z = structure(c(-0.056387056663922, -0.0833659398207195, 0.396739337416388,  0.24641796213326, 0.118671096820208, 0.129984168693827, 0.292333546545335,  0.156334626527219, 0.00936794337915659, 4.39023388085111e-05,  -0.233799038375297, -0.367391337762424, 0.531106654181753, 0.346260826470351,  0.70528740481798, 0.312827082934439, 0.567507350128498, 0.46784722676656,  0.0383489999099153, 0.00252158795784119, -0.110691700342295,  -0.141817004561696, 0.446975477301654, 0.529697315449668, 0.965658537923516,  0.805143685644612, 0.451382872132391, 0.439361229129139, 0.157839834971403,  0.0765559129886248, -0.454819797191376, -0.338056672577417, -0.448166270779698,  0.396738897767298, 0.794738448758618, 1.05773239260991, 0.993558496164969,  0.667323106017284, 0.666616909867164, 0.268176240290954, -0.684841368779022,  -0.888759295862428, -1.14848645791151, -0.340408352484113, 0.724335452685496,  0.777105026265097, 0.731418740354042, 0.62105277394133, 0.923376829024152,  0.545413098407261, -0.33947787021642, -0.820096032239754, -1.12387867096049,  -1.0033086182349, -0.58803847689768, 0.372473040212843, 0.47990115526495,  0.516056369469711, 0.325357560496072, 0.381602492416752, -0.125018589337782,  -0.557388436291469, -0.978664125921135, -1.03946600192465, -0.653382128848502,  -0.189104517943845, 0.210501149785553, 0.0993421340760843, 0.0190847039691979,  0.0357052177806083, -0.0344398944841238, -0.150904214523909,  -0.465813267740361, -0.831422242194181, -0.663306574342851, -0.496971130254831,  -0.0859623594899633, 0.00228186685168581, 0.00024571769585812,  0.000369870812014032, -0.00112906237241394, -0.00566586013633641,  -0.126884937055008, -0.489820026951459, -0.504067079244254, -0.380934572336174,  -0.0814614811545815, -0.00107205477085648, -5.89158450257398e-07,  3.99726669600497e-07, -3.82870639511123e-06, -0.000340401881555849,  -0.0253819703110471, -0.12135090286982, -0.337776178856698, -0.390293783578562,  -0.0358655796308315, -0.000208950638225357, -9.65292928207387e-08,  4.43723223379283e-11), .Dim = c(10L, 10L)), sum.z = structure(c(0.0563878623763604,  0.0982758164041467, 0.396911553906374, 0.246417969325305, 0.118671096820209,  0.129984168693827, 0.292333546545335, 0.156334626527219, 0.00936794337915659,  4.39023388085111e-05, 0.233804081056402, 0.388006459477939, 0.533314280264042,  0.346260921320133, 0.70528740482134, 0.312827082934439, 0.567507350128498,  0.46784722676656, 0.0383489999099153, 0.00252158795784119, 0.110702098220809,  0.183360397573434, 0.448857585186108, 0.529824175257911, 0.96565860674229,  0.80514368564503, 0.451382872132391, 0.439361229129139, 0.157839834971403,  0.0765559129886248, 0.454819823827035, 0.338275658038993, 0.47580852271393,  0.447374413336615, 0.794768329848548, 1.05773241724257, 0.993558496165915,  0.667323106017284, 0.666616909867164, 0.268176240290954, 0.684841368781024,  0.888759338156382, 1.14867474298384, 0.474502184417517, 0.737579848258922,  0.777230772929083, 0.731418749670773, 0.621052773941336, 0.923376829024152,  0.545413098407261, 0.33947787021642, 0.820096032255406, 1.12388043674926,  1.00499484884114, 0.613985232582756, 0.39074530346458, 0.479905405405993,  0.516056369530778, 0.325357560496072, 0.381602492416752, 0.125018589337782,  0.557388436291473, 0.978664126615223, 1.03946656734675, 0.653430015475651,  0.26275754388336, 0.212432152424009, 0.0993423441302914, 0.0190847039695885,  0.0357052177806083, 0.0344398944841238, 0.150904214523909, 0.465813267740366,  0.831422242197092, 0.663306672511741, 0.497078535743582, 0.0875890220074573,  0.00274811814266004, 0.000245720302810954, 0.000369870812014035,  0.00112906237241394, 0.00566586013633641, 0.126884937055008,  0.489820026951459, 0.504067079248811, 0.380934577331769, 0.0814615423172973,  0.00107212388924586, 9.64494246034256e-07, 3.9972667240558e-07,  3.82870639511123e-06, 0.000340401881555849, 0.0253819703110471,  0.12135090286982, 0.337776178856698, 0.390293783578564, 0.0358655796308838,  0.000208950638309971, 9.65294919899824e-08, 4.46531164955966e-11 ), .Dim = c(10L, 10L)), d1.z = structure(c(0.0563874595215803,  0.0911263299274512, 0.00826679414215622, 4.20980876540403e-05,  1.20712589245659e-08, 1.82814873551e-12, 1.98701934456555e-16,  2.12728906594723e-21, 2.95489299273463e-26, 3.48076351235962e-32,  0.233801559729445, 0.377839521264961, 0.0342771309871237, 0.000181225738679179,  1.53928945978047e-06, 2.96159325405382e-09, 4.41826995049078e-13,  9.00126010080291e-17, 1.81960686395159e-21, 3.07756775938858e-27,  0.110696899403637, 0.163910186048854, 0.0290349312902808, 0.0081978867136808,  0.000257789525189142, 5.79578387885136e-07, 1.58498454116692e-09,  5.51664269297929e-13, 1.59023469119042e-17, 3.46950001539759e-23,  0.454819810509206, 0.338166183034192, 0.462194091563162, 0.146188427562975,  0.00487320197588596, 0.000161414866756254, 9.69789238141175e-07,  4.75783021324151e-10, 1.87824581435273e-14, 1.00706250755473e-19,  0.684841368780023, 0.888759317009405, 1.14858060430583, 0.412934722115944,  0.0983925417411082, 0.00988565985314068, 8.25495714689792e-05,  6.30847578466104e-08, 9.6155436949557e-12, 1.54582977740203e-16,  0.33947787021642, 0.82009603224758, 1.12387955385523, 1.00415208749015,  0.60115185940902, 0.0835036720576656, 0.00142816547653287, 5.61372995650101e-06,  1.78282737186866e-09, 4.40854043151748e-14, 0.125018589337782,  0.557388436291471, 0.978664126268179, 1.03946628463574, 0.653406072600764,  0.228912696867594, 0.0202074902258746, 0.000144455028530616,  8.6344026850339e-08, 3.46688372458474e-12, 0.0344398944841238,  0.150904214523909, 0.465813267740363, 0.831422242195637, 0.663306623427298,  0.497024835900455, 0.0867795022609156, 0.00108287510757122, 8.00361069563921e-07,  3.34926205469616e-11, 0.00112906237241394, 0.00566586013633641,  0.126884937055008, 0.489820026951459, 0.504067079246533, 0.380934574833971,  0.0814615117359452, 0.00107208933060818, 7.99173582566043e-07,  3.34853180910460e-11, 3.82870639511123e-06, 0.000340401881555849,  0.0253819703110471, 0.12135090286982, 0.337776178856698, 0.390293783578563,  0.0358655796308576, 0.000208950638267664, 9.65293924054119e-08,  3.53537997309416e-12), .Dim = c(10L, 10L)), d2.z = structure(c(0.00021314801783726,  0.0367984793687845, 0.396825455003813, 0.246417965729283, 0.118671096820209,  0.129984168693827, 0.292333546545335, 0.156334626527219, 0.00936794337915659,  4.39023388085111e-05, 0.00108581154319537, 0.0882400632755367,  0.532211611884638, 0.346260873895246, 0.70528740481966, 0.312827082934439,  0.567507350128498, 0.46784722676656, 0.0383489999099153, 0.00252158795784119,  0.00107285269757279, 0.0821856818899602, 0.447917519800323, 0.529760749151119,  0.965658572332904, 0.805143685644821, 0.451382872132391, 0.439361229129139,  0.157839834971403, 0.0765559129886248, 0.000110065551143133,  0.00860543280721871, 0.11300607068346, 0.422815337180365, 0.794753389444017,  1.05773240492624, 0.993558496165442, 0.667323106017284, 0.666616909867164,  0.268176240290954, 1.17102706566952e-06, 0.000193879204244925,  0.0147058009450244, 0.233746097909727, 0.730987647151016, 0.777167902140334,  0.731418745012407, 0.621052773941333, 0.923376829024152, 0.545413098407261,  2.17601730243418e-09, 3.58271593161246e-06, 0.00140873485671439,  0.0411488928939164, 0.124877170686953, 0.381718520552137, 0.479903280340176,  0.516056369500244, 0.325357560496072, 0.381602492416752, 6.09690442135277e-12,  5.09035457793556e-08, 2.60629811058794e-05, 0.000766640207816288,  0.00559369403478866, 0.128998077816768, 0.211468855206313, 0.0993422391032433,  0.0190847039693932, 0.0357052177806083, 1.06556819446334e-14,  9.7295988429327e-11, 4.99211667085697e-08, 1.55575115523579e-06,  0.000255178515490111, 0.00730638043833718, 0.0118808570217211,  0.00252577410459049, 0.000245718999337994, 0.000369870812014033,  2.09575922685285e-18, 1.91473976049806e-14, 9.82978620254911e-12,  2.97270228423485e-09, 1.51553022357806e-06, 4.36233286383242e-05,  7.05861692504559e-05, 8.6081988686731e-06, 5.39972902618034e-07,  3.99726671003038e-07, 4.23739578828435e-23, 3.87142708601079e-19,  2.80896876815908e-16, 1.81405457874235e-12, 9.30311485952076e-10,  2.67827781054923e-08, 4.32928915181748e-08, 4.20475879041308e-09,  1.38656720307766e-10, 4.4512940828653e-11), .Dim = c(10L, 10L ))), .Names = c("x", "y", "z", "sum.z", "d1.z", "d2.z"))

test_that("diffdensity.plot returns expected values", {
	diffd <- diffdensity.plot(setosa$Sepal.Length, versicolor$Sepal.Length, setosa$Sepal.Width, versicolor$Sepal.Width, bw = 0.5, n = 10)
	expect_equal(diffd, expected_diffd)
})

test_that("diffdensity.plot follows ztransform ", {
	diffd <- diffdensity.plot(setosa$Sepal.Length, versicolor$Sepal.Length, setosa$Sepal.Width, versicolor$Sepal.Width, bw = 0.5, n = 10, ztransform = I)
	expect_equal(as.numeric(sqrt(diffd$sum.z)), as.numeric(expected_diffd$sum.z))

})

test_that("diffdensity.plot stops with relative and zlim", {
	expect_error(diffdensity.plot(setosa$Sepal.Length, versicolor$Sepal.Length, setosa$Sepal.Width, versicolor$Sepal.Width, bw = 0.5, n = 10, zlim = c(0, 1), relative = TRUE))
})

test_that("diffdensity.plot runs with zlim and relative", {
	expect_silent(diffdensity.plot(setosa$Sepal.Length, versicolor$Sepal.Length, setosa$Sepal.Width, versicolor$Sepal.Width, bw = 0.5, n = 10, zlim = c(0, 1)))
	expect_silent(diffdensity.plot(setosa$Sepal.Length, versicolor$Sepal.Length, setosa$Sepal.Width, versicolor$Sepal.Width, bw = 0.5, n = 10, relative = TRUE))
})

test_that("diffdensity.plot plot.d1 and d2 run", {
	expect_silent(diffdensity.plot(setosa$Sepal.Length, versicolor$Sepal.Length, setosa$Sepal.Width, versicolor$Sepal.Width, bw = 0.5, n = 10, plot.d1 = TRUE, plot.d2 = TRUE))
})

test_that("diffdensity.plot accepts a bw function", {
	diffd <- diffdensity.plot(setosa$Sepal.Length, versicolor$Sepal.Length, setosa$Sepal.Width, versicolor$Sepal.Width, bw = function(x) return(0.5), n = 10)
	expect_equal(diffd, expected_diffd)
	# And has an effect on the output
	diffd <- diffdensity.plot(setosa$Sepal.Length, versicolor$Sepal.Length, setosa$Sepal.Width, versicolor$Sepal.Width, bw = function(x) return(0.6), n = 10)
	expect_false(isTRUE(all.equal(diffd, expected_diffd)))
})
xrobin/xavamess documentation built on June 15, 2021, 3:46 a.m.