11context(" Testing Frequency Tables" )
2+
23set.seed(120 , sample.kind = " Reject" )
34dat <- ck_create_testdata()
45
@@ -11,22 +12,29 @@ dim_sex <- hier_create(root = "Total", nodes = c("male", "female"))
1112dim_age <- hier_create(root = " Total" , paste0(" age_group" , 1 : 6 ))
1213dims <- list (sex = dim_sex , age = dim_age )
1314test_that(" dims-hash is ok" , {
14- expect_identical(digest :: sha1(dim_sex ), " fea2001f35be84e90b30f6773af75f03c11fbf7a" )
15- expect_identical(digest :: sha1(dim_age ), " a7648dc3f484720911f0de0e6ac563b69fd20c42" )
16- expect_identical(digest :: sha1(dims ), " 62748837ca3246a33081dd35f50d06334caa3119" )
15+ expect_identical(class(dims ), " list" )
16+ expect_identical(nrow(dim_sex ), 3L )
17+ expect_identical(max(dim_sex $ level ), 2 )
18+ expect_identical(nrow(dim_age ), 7L )
19+ expect_identical(max(dim_age $ level ), 2 )
1720})
1821
1922# # test generation of destatis rkeys
2023rk1 <- ck_generate_rkeys(dat = dat , nr_digits = 5 )
2124rk2 <- ck_generate_rkeys(dat = dat , nr_digits = 5 )
2225test_that(" check rkey generation and seed is ok" , {
2326 expect_identical(rk1 , rk2 )
24- expect_identical(digest :: sha1( rk1 ), " 4de74ed6170e2142ef552ee6722921db8d091d0c " )
27+ expect_identical(round(mean( rk1 ), digits = 3 ), 0.501 )
2528})
2629dat $ rec_key <- rk1
2730test_that(" checking dimension and structure of generated testdata is ok" , {
28- expect_identical(digest :: sha1(dat ), " fb66a8be3e9044c8fecdb13c6fab5fe9ec456c25" )
2931 expect_true(is.data.table(dat ))
32+ expect_identical(round(mean(dat $ sampling_weight ), digits = 3 ), 59.719 )
33+ expect_identical(round(mean(dat $ household_weights ), digits = 3 ), 21.834 )
34+
35+ expect_identical(nrow(dat ), 4580L )
36+ expect_identical(ncol(dat ), 16L )
37+ expect_identical(sum(dat $ sex == " male" ), 2296L )
3038})
3139
3240# # perturbation parameters for count variables
@@ -54,8 +62,13 @@ test_that("ck_params_cnts() is ok", {
5462test_that(" checking perturbation parameters for counts" , {
5563 expect_is(params_cnts , " ck_params" )
5664 expect_equal(params_cnts $ type , " cnts" )
57- expect_is(params_cnts $ params $ ptable , " data.table" )
58- expect_identical(digest :: sha1(params_cnts ), " cedf56d7064f15e55da506b1922c4cac6035765f" )
65+ dt <- params_cnts $ params $ ptable
66+ expect_is(dt , " data.table" )
67+ expect_identical(dim(dt ), c(66L , 7L ))
68+ expect_identical(round(mean(dt $ p ), digits = 3 ), 0.136 )
69+ expect_identical(round(mean(dt $ lb ), digits = 3 ), 0.562 )
70+ expect_identical(round(mean(dt $ ub ), digits = 3 ), 0.698 )
71+ expect_identical(round(mean(dt $ v ), digits = 3 ), 1.045 )
5972})
6073
6174countvars <- NULL
@@ -79,9 +92,11 @@ expect_message(tab$perturb("total"), "Variable 'total' was already perturbed!")
7992res_freqtab <- tab $ freqtab(" total" )
8093test_that(" check ck_define_table() with already existing rec-keys" , {
8194 expect_is(tab , " cellkey_obj" )
95+ expect_identical(dim(res_freqtab ), c(21L , 7L ))
8296 expect_identical(res_freqtab $ uwc [3 ], 1143 )
8397 expect_identical(res_freqtab $ puwc [3 ], 1147 )
84- expect_identical(digest :: sha1(res_freqtab ), " 05e71f630a385f7e428ce1fec21b5f6026bb921a" )
98+ expect_identical(round(mean(res_freqtab $ pwc ), digits = 3 ), 52096.24 )
99+ expect_identical(round(mean(res_freqtab $ puwc ), digits = 3 ), 872.333 )
85100})
86101
87102dat $ rec_key <- NULL
@@ -99,23 +114,71 @@ expect_message(tab$perturb("total"), "Variable 'total' was already perturbed!")
99114
100115test_that(" ck_define_table() with new record keys is ok" , {
101116 expect_is(tab , " cellkey_obj" )
102- expect_identical(digest :: sha1(tab $ freqtab(" total" )), " 05e71f630a385f7e428ce1fec21b5f6026bb921a" )
117+ dt <- tab $ freqtab(" total" )
118+ expect_identical(dt $ uwc [3 ], 1143 )
119+ expect_identical(dt $ puwc [3 ], 1147 )
120+ expect_identical(round(mean(dt $ pwc ), digits = 3 ), 52096.24 )
121+ expect_identical(round(mean(dt $ puwc ), digits = 3 ), 872.333 )
103122})
104123
105124freqtab <- tab $ freqtab(" total" )
106125test_that(" weighted version of ck_perturb() is ok" , {
107- expect_identical(digest :: sha1(freqtab ), " 05e71f630a385f7e428ce1fec21b5f6026bb921a" )
108- expect_identical(digest :: sha1(tab $ mod_cnts()), " ee05433bb69fbf66094cf14e5c50320b3060eab3" )
126+ expect_identical(freqtab $ uwc [3 ], 1143 )
127+ expect_identical(freqtab $ puwc [3 ], 1147 )
128+ expect_identical(round(mean(freqtab $ pwc ), digits = 3 ), 52096.24 )
129+ expect_identical(round(mean(freqtab $ puwc ), digits = 3 ), 872.333 )
130+
131+ dt <- tab $ mod_cnts()
132+ expect_identical(dim(dt ), c(21L , 6L ))
133+ expect_identical(round(mean(dt $ ckey ), digits = 3 ), 0.456 )
134+ expect_identical(round(mean(dt $ pert ), digits = 3 ), - 0.048 )
135+ expect_identical(range(dt $ row_nr ), c(40 , 65 ))
109136})
110137
111138mm <- tab $ measures_cnts(" Total" )
112139test_that(" ck_cnt_measures() [exclude_zeros = TRUE] is ok" , {
113- expect_identical(digest :: sha1(mm ), " 89f4aba98334930446c2fb97b0812b7ece98ef6d" )
140+ expect_identical(range(as.numeric(mm $ overview $ noise )), c(1 , 7 ))
141+ expect_identical(range(as.numeric(mm $ overview $ cnt )), c(1 , 9 ))
142+ expect_identical(round(mean(as.numeric(mm $ overview $ pct )), digits = 3 ), 0.143 )
143+
144+ expect_identical(range(as.numeric(mm $ measures $ d1 )), c(0 , 4 ))
145+ expect_identical(range(as.numeric(mm $ measures $ d2 )), c(0 , 0.429 ))
146+ expect_identical(range(as.numeric(mm $ measures $ d3 )), c(0 , 0.517 ))
147+
148+ expect_identical(range(mm $ cumdistr_d1 $ cnt ), c(9L , 21L ))
149+ expect_identical(round(range(mm $ cumdistr_d1 $ pct ), digits = 3 ), c(0.429 , 1 ))
150+
151+ expect_identical(range(mm $ cumdistr_d2 $ cnt ), c(19L , 21L ))
152+ expect_identical(round(range(mm $ cumdistr_d2 $ pct ), digits = 3 ), c(0.905 , 1 ))
153+
154+ expect_identical(range(mm $ cumdistr_d3 $ cnt ), c(12L , 21L ))
155+ expect_identical(round(range(mm $ cumdistr_d3 $ pct ), digits = 3 ), c(0.571 , 1 ))
156+ expect_identical(mm $ false_nonzero , 0L )
157+ expect_identical(mm $ false_zero , 0L )
158+ expect_identical(mm $ exclude_zeros , TRUE )
114159})
115160
116161mm <- tab $ measures_cnts(" Total" , exclude_zeros = FALSE )
117162test_that(" ck_cnt_measures() [exclude_zeros = FALSE] is ok" , {
118- expect_identical(digest :: sha1(mm ), " 9fb4ffe32ebc420d8ccecb6f3dab6e1431396fa0" )
163+ expect_identical(range(as.numeric(mm $ overview $ noise )), c(1 , 7 ))
164+ expect_identical(range(as.numeric(mm $ overview $ cnt )), c(1 , 9 ))
165+ expect_identical(round(mean(as.numeric(mm $ overview $ pct )), digits = 3 ), 0.143 )
166+
167+ expect_identical(range(as.numeric(mm $ measures $ d1 )), c(0 , 4 ))
168+ expect_identical(range(as.numeric(mm $ measures $ d2 )), c(0 , 0.429 ))
169+ expect_identical(range(as.numeric(mm $ measures $ d3 )), c(0 , 0.517 ))
170+
171+ expect_identical(range(mm $ cumdistr_d1 $ cnt ), c(9L , 21L ))
172+ expect_identical(round(range(mm $ cumdistr_d1 $ pct ), digits = 3 ), c(0.429 , 1 ))
173+
174+ expect_identical(range(mm $ cumdistr_d2 $ cnt ), c(19L , 21L ))
175+ expect_identical(round(range(mm $ cumdistr_d2 $ pct ), digits = 3 ), c(0.905 , 1 ))
176+
177+ expect_identical(range(mm $ cumdistr_d3 $ cnt ), c(12L , 21L ))
178+ expect_identical(round(range(mm $ cumdistr_d3 $ pct ), digits = 3 ), c(0.571 , 1 ))
179+ expect_identical(mm $ false_nonzero , 0L )
180+ expect_identical(mm $ false_zero , 0L )
181+ expect_identical(mm $ exclude_zeros , FALSE )
119182})
120183
121184# no weights
@@ -133,8 +196,17 @@ tab$perturb("total")
133196freqtab <- tab $ freqtab(" total" )
134197
135198test_that(" checking unweighted version of perturb()" , {
136- expect_identical(digest :: sha1(freqtab ), " 31831ab589f3bc7bdd20c4b6fd1ea1916e3edfef" )
137- expect_identical(digest :: sha1(tab $ mod_cnts()), " ee05433bb69fbf66094cf14e5c50320b3060eab3" )
199+ expect_identical(dim(freqtab ), c(21L , 7L ))
200+ expect_identical(freqtab $ uwc [3 ], 1143 )
201+ expect_identical(freqtab $ puwc [3 ], 1147 )
202+ expect_identical(freqtab $ pwc , freqtab $ puwc )
203+ expect_identical(round(mean(freqtab $ pwc ), digits = 3 ), 872.333 )
204+
205+ dt <- tab $ mod_cnts()
206+ expect_identical(dim(dt ), c(21L , 6L ))
207+ expect_identical(round(mean(dt $ ckey ), digits = 3 ), 0.456 )
208+ expect_identical(round(mean(dt $ pert ), digits = 3 ), - 0.048 )
209+ expect_identical(range(dt $ row_nr ), c(40 , 65 ))
138210})
139211
140212context(" Testing multiple countvars" )
@@ -153,16 +225,52 @@ tab <- ck_setup(
153225tab $ params_cnts_set(params_cnts , v = NULL )
154226tab $ perturb(c(" total" , " cnt_males" , " cnt_highincome" ))
155227test_that(" check tabulation of cnt_males is ok" , {
156- expect_identical(digest :: sha1(tab $ freqtab(" cnt_males" )), " a7260c1f65a089d8849084a7e84c946be997e986" )
157- expect_identical(digest :: sha1(tab $ measures_cnts(" cnt_males" )), " b692361c523b0d37aa38bf252a360414cd941e2b" )
228+ dt <- tab $ freqtab(" cnt_males" )
229+ expect_identical(dt $ uwc [3 ], 571 )
230+ expect_identical(dt $ puwc [3 ], 571 )
231+ expect_identical(round(mean(dt $ pwc ), digits = 3 ), 25757.65 )
232+ expect_identical(round(mean(dt $ puwc ), digits = 3 ), 437.048 )
233+
234+ expect_identical(round(range(dt $ puwc ), digits = 3 ), c(0 , 2297 ))
235+ expect_identical(round(range(dt $ pwc ), digits = 3 ), c(0 , 135387.941 ))
236+
237+ mm <- tab $ measures_cnts(" cnt_males" )
238+ expect_identical(round(range(mm $ overview $ pct ), digits = 3 ), c(0.095 , 0.810 ))
239+ expect_identical(round(range(mm $ measures $ d1 ), digits = 3 ), c(0 , 4 ))
240+ expect_identical(round(range(mm $ measures $ d2 ), digits = 3 ), c(0 , 0.048 ))
241+ expect_identical(round(range(mm $ measures $ d3 ), digits = 3 ), c(0 , 0.221 ))
158242})
159243
160244test_that(" check tabulation of cnt_highincome is ok" , {
161- expect_identical(digest :: sha1(tab $ freqtab(" cnt_highincome" )), " 72c684edc0cebef0343bce02d0075050d5286a5c" )
162- expect_identical(digest :: sha1(tab $ measures_cnts(" cnt_highincome" )), " 639f20e406ecfdd44c84e0ae04674321c2a602b1" )
245+ dt <- tab $ freqtab(" cnt_highincome" )
246+ expect_identical(dt $ uwc [3 ], 123 )
247+ expect_identical(dt $ puwc [3 ], 123 )
248+ expect_identical(round(mean(dt $ pwc ), digits = 3 ), 5063.68 )
249+ expect_identical(round(mean(dt $ puwc ), digits = 3 ), 84.286 )
250+
251+ expect_identical(round(range(dt $ puwc ), digits = 3 ), c(0 , 444 ))
252+ expect_identical(round(range(dt $ pwc ), digits = 3 ), c(0 , 26671.928 ))
253+
254+ mm <- tab $ measures_cnts(" cnt_highincome" )
255+ expect_identical(round(range(mm $ overview $ pct ), digits = 3 ), c(0.048 , 0.667 ))
256+ expect_identical(round(range(mm $ measures $ d1 ), digits = 3 ), c(0 , 3 ))
257+ expect_identical(round(range(mm $ measures $ d2 ), digits = 3 ), c(0 , 0.158 ))
258+ expect_identical(round(range(mm $ measures $ d3 ), digits = 3 ), c(0 , 0.359 ))
163259})
164260
165261test_that(" check tabulation of multiple count variables is ok" , {
166- tt <- tab $ freqtab(c(" total" , " cnt_males" , " cnt_highincome" ))
167- expect_identical(digest :: sha1(tt ), " 1984413d2bb4ebff2d268b243b300cd8494b55fb" )
262+ dt <- tab $ freqtab(c(" total" , " cnt_males" , " cnt_highincome" ))
263+ expect_identical(dim(dt ), c(63L , 7L ))
264+ expect_identical(dt $ uwc [3 ], 1143 )
265+ expect_identical(dt $ puwc [3 ], 1148 )
266+ expect_identical(round(mean(dt $ pwc ), digits = 3 ), 27640.21 )
267+ expect_identical(round(mean(dt $ puwc ), digits = 3 ), 464.587 )
268+ expect_identical(round(range(dt $ puwc ), digits = 3 ), c(0 , 4582 ))
269+ expect_identical(round(range(dt $ pwc ), digits = 3 ), c(0 , 273633.438 ))
270+
271+ mm <- tab $ measures_cnts(" cnt_highincome" )
272+ expect_identical(round(range(mm $ overview $ pct ), digits = 3 ), c(0.048 , 0.667 ))
273+ expect_identical(round(range(mm $ measures $ d1 ), digits = 3 ), c(0 , 3 ))
274+ expect_identical(round(range(mm $ measures $ d2 ), digits = 3 ), c(0 , 0.158 ))
275+ expect_identical(round(range(mm $ measures $ d3 ), digits = 3 ), c(0 , 0.359 ))
168276})
0 commit comments