tests/testthat/test_hiertesting.R

context("Hierarchical Testing")
library(hat)

test_that("Testing yields an aggregation", {
  result = hierarchical_test(tree = taxi_tree,
                        p_vals = c(0.999999998580875,0.973522493163641,0.975108452770659,0,0.996213163618838,0.999999999954863,0.99999999999666,1,0.999999999951393,1,0.999999999958733,0.999999999963948,0.99999999999654,0.865930615640178,0.999999999901472,0.999999999837554,1,0.999999999861998,0.999999999954418,0.970118961389531,0.634740239865867,0.999999999900237,0.997538234907372,0.868355539150171,0.999999999732049,0.96975705223925,1,0.951280848617181,0.991292997335336,0,0.89447036714463,0.999999999951931,0.977242963877113,0.925991163856165,0.998016575984593,1,0.709289637478233,0.964506027173835,6.28386231937839e-14,0.827658847346553,0.995428880740768,0,0,0.478159392878208,0.770047385990749,0.948120373013635,0,0,0,0,1,0.273817445990691,0.999999999306422,0.99999999999446,0.999999999763433,1,1,1,1,0.999999999996956,0.999999999877073,0.98166706229947,0.984251566212225,1,1,0.995964139105652,1,1,0.999999445296956,0.941986825887167,0.999999999075229,0.969900006571072,0.994785647845873,0.0963177836909042,0.714041753510158,0.999999998844036,0.432247672754459,1,1,0.17962334142858,0.00119146225209854,0.99999999872844,0.775447845024855,0.230314703896457,0.0293439573296259,0.40029732175188,0.030391596163041,0.999999999699181,1,0.983967479563717,0.458739521495219,0.999999873705797,0.999999999956222,0.974856247803891,1,0.800339163951585,0.975524813199345,0.98031664176084,0.873281292533954,0.999999999994362,0.0894769736875671,0.15139192209557,0.780514010142265,0.999999945242648,1,5.83730841441366e-11,0.999999911763966,0.937478805622367,1,0,0.583586815153302,0.251384963731474,0.999994749559076,1,0.865547505811438,0,4.86986271315271e-05,0.999999999811845,0.844972235917974,0.999999867645949,0.310583359633605,0.999999877069757,0.594743265745556,0.823858362817997,0.774466851635364,0.674658354867411,0.997883352601755,0,0,0,0.781352305130824,0.00571416762288823,6.51051341837849e-06,0.395161492822718,0,0.9982418000324,0,0,1.27271332031054e-05,0,0,0,0,0,0,0,0,0.999999999882921,0,0.0310378030778551,0,6.19227915017451e-05,0,0,0.111066954259939,0.88587321386234,0,0,0,0,0,0,0,0,0.0898964853856867,0,0,0,0,0,0,0,1,1,0.999999999998848,1,1,1,1,1,1,0.999999998412209,0.999999999787965,1,1,0.999999999682523,0.999999997353681,0.999999996454767,0.99999999644293,0),
                        alpha = 0.4,
                        independent = FALSE)
  expect_equal(length(result), 4)
  expect_equal(result[[1]], 0.4)
  expect_equal(sum(result[[2]]), 48)
  expect_equal(sum(is.na(result[[3]])), 119)
  expect_equal(max(result[[4]]), 52)
})

test_that("Calculating fsp and power",{
  fsp = calculate_fsp(theta_est = c(0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,-0.966448859943501,0.352895080430454,-0.966448859943501,-0.966448859943501,0.436421127822755,-0.966448859943501,0.436421127822755,-0.966448859943501,0.352895080430454,0.436421127822755,0.352895080430454,-0.966448859943501,0.352895080430454,-0.966448859943501,0.436421127822755,0.436421127822755,-0.966448859943501,0.436421127822755,0.352895080430454,0.352895080430454,0.436421127822755,0.436421127822755,0.352895080430454,0.352895080430454,0.352895080430454,0.352895080430454,-0.966448859943501,0.436421127822755,0.436421127822755,-0.966448859943501,0.436421127822755,0.352895080430454,0.352895080430454,0.436421127822755,-0.966448859943501,-0.966448859943501,0.436421127822755,0.436421127822755,-0.966448859943501,0.352895080430454,0.352895080430454,0.352895080430454,0.352895080430454,0.352895080430454,0.436421127822755,0.436421127822755,-0.966448859943501,-0.966448859943501,0.436421127822755,0.436421127822755,0.352895080430454,0.352895080430454,-0.966448859943501,-0.966448859943501,0.436421127822755,-0.966448859943501,0.352895080430454,0.352895080430454,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205),
                theta_true = c(-1.05942641040017,-1.97377802258209,-1.05942641040017,-0.485093827245219,-1.05942641040017,-0.485093827245219,-0.485093827245219,-1.05942641040017,-1.05942641040017,-0.485093827245219,-1.05942641040017,-0.485093827245219,-1.05942641040017,-1.05942641040017,-1.05942641040017,-0.485093827245219,-0.485093827245219,-0.485093827245219,-0.485093827245219,-1.05942641040017,-1.05942641040017,-1.97377802258209,-0.485093827245219,-2.38289129447308,-1.28098692486077,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-0.485093827245219,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.97377802258209,-1.28098692486077,-1.97377802258209,-1.97377802258209,-0.485093827245219,-0.485093827245219,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-2.60396812865029,-0.485093827245219,1.55760741145128,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,1.55760741145128,1.55760741145128,1.55760741145128,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,1.55760741145128,1.55760741145128,1.55760741145128,-1.06056146324986,-1.06056146324986,-1.06056146324986,1.55760741145128,-1.06056146324986,-1.06056146324986,1.55760741145128,-1.06056146324986,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,1.55760741145128,-1.06056146324986,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,1.55760741145128,-0.68427981936591,3.18616889692097,-0.68427981936591,-0.68427981936591,1.04977943792729,-0.723080488611978,1.04977943792729,-0.723080488611978,0.90267013247988,1.04977943792729,2.75800498251514,-0.723080488611978,1.33247880744752,-0.723080488611978,-0.150357599946624,-0.150357599946624,-0.68427981936591,1.04977943792729,2.75800498251514,2.75800498251514,-0.150357599946624,-0.160427541881318,2.75800498251514,2.75800498251514,3.18616889692097,2.75800498251514,-0.723080488611978,1.04977943792729,1.04977943792729,-2.26108360451396,1.04977943792729,3.18616889692097,3.18616889692097,-0.160427541881318,-0.68427981936591,-2.26108360451396,1.04977943792729,1.04977943792729,-0.723080488611978,0.090215601376112,2.75800498251514,3.18616889692097,2.75800498251514,3.18616889692097,1.04977943792729,1.04977943792729,-0.723080488611978,-0.723080488611978,1.04977943792729,-0.0275024402918523,0.90267013247988,0.090215601376112,-3.22623019404306,-0.0983415816434343,1.04977943792729,-0.0983415816434343,1.33247880744752,4.27905049862826,-2.20947669176946,0.62351773209731,-3.70037327142942,-1.41748888258202,-1.41748888258202,-1.88553250633931,-2.7527200333794,-4.15567765364104,-2.2048633497505,1.14057467008413,1.14057467008413,-1.41748888258202,-2.09889622798083,-1.93058174795576,-2.19043683699836,-0.0882834837778634,-0.184831908857921,-1.41748888258202,-1.06806736392488,-1.09392703103974,-0.0276670782819204,-1.74202107659566,-1.41748888258202,-3.15722377247803,-1.24729680108268,-3.60576949329772,-1.88553250633931,-3.3499050068517,1.14057467008413,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507))
  power = calculate_power(theta_est = c(0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.481648517119092,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,0.29037112866957,-0.966448859943501,0.352895080430454,-0.966448859943501,-0.966448859943501,0.436421127822755,-0.966448859943501,0.436421127822755,-0.966448859943501,0.352895080430454,0.436421127822755,0.352895080430454,-0.966448859943501,0.352895080430454,-0.966448859943501,0.436421127822755,0.436421127822755,-0.966448859943501,0.436421127822755,0.352895080430454,0.352895080430454,0.436421127822755,0.436421127822755,0.352895080430454,0.352895080430454,0.352895080430454,0.352895080430454,-0.966448859943501,0.436421127822755,0.436421127822755,-0.966448859943501,0.436421127822755,0.352895080430454,0.352895080430454,0.436421127822755,-0.966448859943501,-0.966448859943501,0.436421127822755,0.436421127822755,-0.966448859943501,0.352895080430454,0.352895080430454,0.352895080430454,0.352895080430454,0.352895080430454,0.436421127822755,0.436421127822755,-0.966448859943501,-0.966448859943501,0.436421127822755,0.436421127822755,0.352895080430454,0.352895080430454,-0.966448859943501,-0.966448859943501,0.436421127822755,-0.966448859943501,0.352895080430454,0.352895080430454,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.0263919105463935,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205,-0.757357319142205),
                        theta_true = c(-1.05942641040017,-1.97377802258209,-1.05942641040017,-0.485093827245219,-1.05942641040017,-0.485093827245219,-0.485093827245219,-1.05942641040017,-1.05942641040017,-0.485093827245219,-1.05942641040017,-0.485093827245219,-1.05942641040017,-1.05942641040017,-1.05942641040017,-0.485093827245219,-0.485093827245219,-0.485093827245219,-0.485093827245219,-1.05942641040017,-1.05942641040017,-1.97377802258209,-0.485093827245219,-2.38289129447308,-1.28098692486077,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-0.485093827245219,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.97377802258209,-1.28098692486077,-1.97377802258209,-1.97377802258209,-0.485093827245219,-0.485093827245219,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-1.05942641040017,-2.60396812865029,-0.485093827245219,1.55760741145128,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,1.55760741145128,1.55760741145128,1.55760741145128,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,1.55760741145128,1.55760741145128,1.55760741145128,-1.06056146324986,-1.06056146324986,-1.06056146324986,1.55760741145128,-1.06056146324986,-1.06056146324986,1.55760741145128,-1.06056146324986,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,1.55760741145128,-1.06056146324986,-1.06056146324986,-1.06056146324986,1.55760741145128,1.55760741145128,1.55760741145128,-0.68427981936591,3.18616889692097,-0.68427981936591,-0.68427981936591,1.04977943792729,-0.723080488611978,1.04977943792729,-0.723080488611978,0.90267013247988,1.04977943792729,2.75800498251514,-0.723080488611978,1.33247880744752,-0.723080488611978,-0.150357599946624,-0.150357599946624,-0.68427981936591,1.04977943792729,2.75800498251514,2.75800498251514,-0.150357599946624,-0.160427541881318,2.75800498251514,2.75800498251514,3.18616889692097,2.75800498251514,-0.723080488611978,1.04977943792729,1.04977943792729,-2.26108360451396,1.04977943792729,3.18616889692097,3.18616889692097,-0.160427541881318,-0.68427981936591,-2.26108360451396,1.04977943792729,1.04977943792729,-0.723080488611978,0.090215601376112,2.75800498251514,3.18616889692097,2.75800498251514,3.18616889692097,1.04977943792729,1.04977943792729,-0.723080488611978,-0.723080488611978,1.04977943792729,-0.0275024402918523,0.90267013247988,0.090215601376112,-3.22623019404306,-0.0983415816434343,1.04977943792729,-0.0983415816434343,1.33247880744752,4.27905049862826,-2.20947669176946,0.62351773209731,-3.70037327142942,-1.41748888258202,-1.41748888258202,-1.88553250633931,-2.7527200333794,-4.15567765364104,-2.2048633497505,1.14057467008413,1.14057467008413,-1.41748888258202,-2.09889622798083,-1.93058174795576,-2.19043683699836,-0.0882834837778634,-0.184831908857921,-1.41748888258202,-1.06806736392488,-1.09392703103974,-0.0276670782819204,-1.74202107659566,-1.41748888258202,-3.15722377247803,-1.24729680108268,-3.60576949329772,-1.88553250633931,-3.3499050068517,1.14057467008413,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507,-0.717009512419507))
  expect_equal(fsp, 0)
  expect_equal(power, 0.1333333333333333333333333333333)

})

test_that("Aggregating observations", {
  agg_result = aggregate_observations(y = log(stocks_volatility$volatility),
                      sigma  = 0.51,
                      tree = stocks_tree,
                      alpha = 0.3)

  expect_equal(agg_result[[1]], 0.3)
  expect_equal(max(agg_result[[2]]), 52)
  expect_equal(sum(agg_result[[3]]), 10)
})

test_that("Aggregating features", {
  hc = hclust(dist(USArrests), "ave")
  groups = cutree(hc, 10)
  set.seed(1234)
  coeffs = runif(10,0, 2)[groups]
  X = MASS::mvrnorm(100, mu = rep(0,50), diag(50), empirical = FALSE)
  y = X %*% coeffs
  agg_result = aggregate_features(y = y, X = X,
                          tree = hc, alpha = 0.3)

  expect_equal(agg_result[[1]], 0.3)
  expect_equal(max(agg_result[[2]]), 3)
  expect_equal(sum(agg_result[[3]]), 2)
})
simone0628/hat documentation built on June 1, 2024, 9 a.m.