This function allows to generate required perturbation parameters that are used to perturb count variables.

ck_params_cnts(ptab, path = NULL)

Arguments

ptab

an object created with ptable::create_ptable(), or ptable::create_cnt_ptable()

path

a scalar character specifying a path to which the parameters created with this functions should be written to (in yaml format)

Value

an object suitable as input to method $params_cnts_set() for the perturbation of counts and frequencies.

See also

This function uses functionality from package ptable (https://github.com/sdcTools/ptable), expecially ptable::create_ptable() and ptable::create_cnt_ptable(). More detailed information on the parameters is available from the respective help-pages of these functions.

Examples

# \donttest{
x <- ck_create_testdata()

# create some 0/1 variables that should be perturbed later
x[, cnt_females := ifelse(sex == "male", 0, 1)]
#>       urbrur roof walls water electcon relat    sex        age hhcivil expend
#>    1:      2    4     3     3        1     1   male age_group3       2   9093
#>    2:      2    4     3     3        1     2 female age_group3       2   2734
#>    3:      2    4     3     3        1     3   male age_group1       1   2652
#>    4:      2    4     3     3        1     3   male age_group1       1   1807
#>    5:      2    4     2     3        1     1   male age_group4       2    671
#>   ---                                                                        
#> 4576:      2    4     3     4        1     2 female age_group3       2   3696
#> 4577:      2    4     3     4        1     3   male age_group1       1    282
#> 4578:      2    4     3     4        1     3   male age_group1       1    840
#> 4579:      2    4     3     4        1     3 female age_group1       1   6258
#> 4580:      2    4     3     4        1     3   male age_group1       1   7019
#>       income savings ori_hid sampling_weight household_weights cnt_females
#>    1:   5780      12       1              66          25.00000           0
#>    2:   2530      28       1              60          25.00000           1
#>    3:   6920     550       1              87          25.00000           0
#>    4:   7960     870       1              71          25.00000           0
#>    5:   9030      20       2              41          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              40          16.66667           1
#> 4577:   1420     987    1000              27          16.66667           0
#> 4578:   8900     684    1000              95          16.66667           0
#> 4579:   3880     294    1000              71          16.66667           1
#> 4580:   4830     911    1000              23          16.66667           0
x[, cnt_males := ifelse(sex == "male", 1, 0)]
#>       urbrur roof walls water electcon relat    sex        age hhcivil expend
#>    1:      2    4     3     3        1     1   male age_group3       2   9093
#>    2:      2    4     3     3        1     2 female age_group3       2   2734
#>    3:      2    4     3     3        1     3   male age_group1       1   2652
#>    4:      2    4     3     3        1     3   male age_group1       1   1807
#>    5:      2    4     2     3        1     1   male age_group4       2    671
#>   ---                                                                        
#> 4576:      2    4     3     4        1     2 female age_group3       2   3696
#> 4577:      2    4     3     4        1     3   male age_group1       1    282
#> 4578:      2    4     3     4        1     3   male age_group1       1    840
#> 4579:      2    4     3     4        1     3 female age_group1       1   6258
#> 4580:      2    4     3     4        1     3   male age_group1       1   7019
#>       income savings ori_hid sampling_weight household_weights cnt_females
#>    1:   5780      12       1              66          25.00000           0
#>    2:   2530      28       1              60          25.00000           1
#>    3:   6920     550       1              87          25.00000           0
#>    4:   7960     870       1              71          25.00000           0
#>    5:   9030      20       2              41          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              40          16.66667           1
#> 4577:   1420     987    1000              27          16.66667           0
#> 4578:   8900     684    1000              95          16.66667           0
#> 4579:   3880     294    1000              71          16.66667           1
#> 4580:   4830     911    1000              23          16.66667           0
#>       cnt_males
#>    1:         1
#>    2:         0
#>    3:         1
#>    4:         1
#>    5:         1
#>   ---          
#> 4576:         0
#> 4577:         1
#> 4578:         1
#> 4579:         0
#> 4580:         1
x[, cnt_highincome := ifelse(income >= 9000, 1, 0)]
#>       urbrur roof walls water electcon relat    sex        age hhcivil expend
#>    1:      2    4     3     3        1     1   male age_group3       2   9093
#>    2:      2    4     3     3        1     2 female age_group3       2   2734
#>    3:      2    4     3     3        1     3   male age_group1       1   2652
#>    4:      2    4     3     3        1     3   male age_group1       1   1807
#>    5:      2    4     2     3        1     1   male age_group4       2    671
#>   ---                                                                        
#> 4576:      2    4     3     4        1     2 female age_group3       2   3696
#> 4577:      2    4     3     4        1     3   male age_group1       1    282
#> 4578:      2    4     3     4        1     3   male age_group1       1    840
#> 4579:      2    4     3     4        1     3 female age_group1       1   6258
#> 4580:      2    4     3     4        1     3   male age_group1       1   7019
#>       income savings ori_hid sampling_weight household_weights cnt_females
#>    1:   5780      12       1              66          25.00000           0
#>    2:   2530      28       1              60          25.00000           1
#>    3:   6920     550       1              87          25.00000           0
#>    4:   7960     870       1              71          25.00000           0
#>    5:   9030      20       2              41          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              40          16.66667           1
#> 4577:   1420     987    1000              27          16.66667           0
#> 4578:   8900     684    1000              95          16.66667           0
#> 4579:   3880     294    1000              71          16.66667           1
#> 4580:   4830     911    1000              23          16.66667           0
#>       cnt_males cnt_highincome
#>    1:         1              0
#>    2:         0              0
#>    3:         1              0
#>    4:         1              0
#>    5:         1              1
#>   ---                         
#> 4576:         0              0
#> 4577:         1              0
#> 4578:         1              0
#> 4579:         0              0
#> 4580:         1              0
# a variable with positive and negative contributions
x[, mixed := sample(-10:10, nrow(x), replace = TRUE)]
#>       urbrur roof walls water electcon relat    sex        age hhcivil expend
#>    1:      2    4     3     3        1     1   male age_group3       2   9093
#>    2:      2    4     3     3        1     2 female age_group3       2   2734
#>    3:      2    4     3     3        1     3   male age_group1       1   2652
#>    4:      2    4     3     3        1     3   male age_group1       1   1807
#>    5:      2    4     2     3        1     1   male age_group4       2    671
#>   ---                                                                        
#> 4576:      2    4     3     4        1     2 female age_group3       2   3696
#> 4577:      2    4     3     4        1     3   male age_group1       1    282
#> 4578:      2    4     3     4        1     3   male age_group1       1    840
#> 4579:      2    4     3     4        1     3 female age_group1       1   6258
#> 4580:      2    4     3     4        1     3   male age_group1       1   7019
#>       income savings ori_hid sampling_weight household_weights cnt_females
#>    1:   5780      12       1              66          25.00000           0
#>    2:   2530      28       1              60          25.00000           1
#>    3:   6920     550       1              87          25.00000           0
#>    4:   7960     870       1              71          25.00000           0
#>    5:   9030      20       2              41          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              40          16.66667           1
#> 4577:   1420     987    1000              27          16.66667           0
#> 4578:   8900     684    1000              95          16.66667           0
#> 4579:   3880     294    1000              71          16.66667           1
#> 4580:   4830     911    1000              23          16.66667           0
#>       cnt_males cnt_highincome mixed
#>    1:         1              0     6
#>    2:         0              0    -9
#>    3:         1              0     4
#>    4:         1              0     0
#>    5:         1              1    -7
#>   ---                               
#> 4576:         0              0     4
#> 4577:         1              0     7
#> 4578:         1              0    -9
#> 4579:         0              0    -4
#> 4580:         1              0    -3

# create record keys
x$rkey <- ck_generate_rkeys(dat = x)

# define required inputs

# hierarchy with some bogus codes
d_sex <- hier_create(root = "Total", nodes = c("male", "female"))
d_sex <- hier_add(d_sex, root = "female", "f")
d_sex <- hier_add(d_sex, root = "male", "m")

d_age <- hier_create(root = "Total", nodes = paste0("age_group", 1:6))
d_age <- hier_add(d_age, root = "age_group1", "ag1a")
d_age <- hier_add(d_age, root = "age_group2", "ag2a")

# define the cell key object
countvars <- c("cnt_females", "cnt_males", "cnt_highincome")
numvars <- c("expend", "income", "savings", "mixed")
tab <- ck_setup(
  x = x,
  rkey = "rkey",
  dims = list(sex = d_sex, age = d_age),
  w = "sampling_weight",
  countvars = countvars,
  numvars = numvars)
#> computing contributing indices | rawdata <--> table; this might take a while

# show some information about this table instance
tab$print() # identical with print(tab)
#> ── Table Information ───────────────────────────────────────────────────────────
#> ✔ 45 cells in 2 dimensions ('sex', 'age')
#> ✔ weights: yes
#> ── Tabulated / Perturbed countvars ─────────────────────────────────────────────
#> ☐ 'total'
#> ☐ 'cnt_females'
#> ☐ 'cnt_males'
#> ☐ 'cnt_highincome'
#> ── Tabulated / Perturbed numvars ───────────────────────────────────────────────
#> ☐ 'expend'
#> ☐ 'income'
#> ☐ 'savings'
#> ☐ 'mixed'

# information about the hierarchies
tab$hierarchy_info()
#> $sex
#>      code level is_leaf parent
#> 1:  Total     1   FALSE  Total
#> 2:   male     2   FALSE  Total
#> 3:      m     3    TRUE   male
#> 4: female     2   FALSE  Total
#> 5:      f     3    TRUE female
#> 
#> $age
#>          code level is_leaf     parent
#> 1:      Total     1   FALSE      Total
#> 2: age_group1     2   FALSE      Total
#> 3:       ag1a     3    TRUE age_group1
#> 4: age_group2     2   FALSE      Total
#> 5:       ag2a     3    TRUE age_group2
#> 6: age_group3     2    TRUE      Total
#> 7: age_group4     2    TRUE      Total
#> 8: age_group5     2    TRUE      Total
#> 9: age_group6     2    TRUE      Total
#> 

# which variables have been defined?
tab$allvars()
#> $cntvars
#> [1] "total"          "cnt_females"    "cnt_males"      "cnt_highincome"
#> 
#> $numvars
#> [1] "expend"  "income"  "savings" "mixed"  
#> 

# count variables
tab$cntvars()
#> [1] "total"          "cnt_females"    "cnt_males"      "cnt_highincome"

# continuous variables
tab$numvars()
#> [1] "expend"  "income"  "savings" "mixed"  

# create perturbation parameters for "total" variable and
# write to yaml-file

# create a ptable using functionality from the ptable-pkg
f_yaml <- tempfile(fileext = ".yaml")
p_cnts1 <- ck_params_cnts(
  ptab = ptable::pt_ex_cnts(),
  path = f_yaml)
#> yaml configuration '/tmp/RtmpV7PINT/file19627d88b994.yaml' successfully written.

# read parameters from yaml-file and set them for variable `"total"`
p_cnts1 <- ck_read_yaml(path = f_yaml)

tab$params_cnts_set(val = p_cnts1, v = "total")
#> --> setting perturbation parameters for variable 'total'

# create alternative perturbation parameters by specifying parameters
para2 <- ptable::create_cnt_ptable(
  D = 8, V = 3, js = 2, create = FALSE)

p_cnts2 <- ck_params_cnts(ptab = para2)

# use these ptable it for the remaining variables
tab$params_cnts_set(val = p_cnts2, v = countvars)
#> --> setting perturbation parameters for variable 'cnt_females'
#> --> setting perturbation parameters for variable 'cnt_males'
#> --> setting perturbation parameters for variable 'cnt_highincome'

# perturb a variable
tab$perturb(v = "total")
#> Count variable 'total' was perturbed.

# multiple variables can be perturbed as well
tab$perturb(v = c("cnt_males", "cnt_highincome"))
#> Count variable 'cnt_males' was perturbed.
#> Count variable 'cnt_highincome' was perturbed.

# return weighted and unweighted results
tab$freqtab(v = c("total", "cnt_males"))
#>        sex        age     vname  uwc     wc puwc         pwc
#>  1:  Total      Total     total 4580 272100 4581 272159.4105
#>  2:  Total age_group1     total 1969 117044 1970 117103.4434
#>  3:  Total       ag1a     total 1969 117044 1970 117103.4434
#>  4:  Total age_group2     total 1143  67505 1141  67386.8810
#>  5:  Total       ag2a     total 1143  67505 1141  67386.8810
#>  6:  Total age_group3     total  864  51289  866  51407.7245
#>  7:  Total age_group4     total  423  25206  423  25206.0000
#>  8:  Total age_group5     total  168  10140  167  10079.6429
#>  9:  Total age_group6     total   13    916   13    916.0000
#> 10:   male      Total     total 2296 135673 2296 135673.0000
#> 11:      m      Total     total 2296 135673 2296 135673.0000
#> 12:   male age_group1     total 1015  60163 1015  60163.0000
#> 13:      m age_group1     total 1015  60163 1015  60163.0000
#> 14:   male       ag1a     total 1015  60163 1015  60163.0000
#> 15:      m       ag1a     total 1015  60163 1015  60163.0000
#> 16:   male age_group2     total  571  33439  571  33439.0000
#> 17:      m age_group2     total  571  33439  571  33439.0000
#> 18:   male       ag2a     total  571  33439  571  33439.0000
#> 19:      m       ag2a     total  571  33439  571  33439.0000
#> 20:   male age_group3     total  424  25289  423  25229.3561
#> 21:      m age_group3     total  424  25289  423  25229.3561
#> 22:   male age_group4     total  195  11353  196  11411.2205
#> 23:      m age_group4     total  195  11353  196  11411.2205
#> 24:   male age_group5     total   84   5026   84   5026.0000
#> 25:      m age_group5     total   84   5026   84   5026.0000
#> 26:   male age_group6     total    7    403    8    460.5714
#> 27:      m age_group6     total    7    403    8    460.5714
#> 28: female      Total     total 2284 136427 2284 136427.0000
#> 29:      f      Total     total 2284 136427 2284 136427.0000
#> 30: female age_group1     total  954  56881  953  56821.3763
#> 31:      f age_group1     total  954  56881  953  56821.3763
#> 32: female       ag1a     total  954  56881  953  56821.3763
#> 33:      f       ag1a     total  954  56881  953  56821.3763
#> 34: female age_group2     total  572  34066  572  34066.0000
#> 35:      f age_group2     total  572  34066  572  34066.0000
#> 36: female       ag2a     total  572  34066  572  34066.0000
#> 37:      f       ag2a     total  572  34066  572  34066.0000
#> 38: female age_group3     total  440  26000  441  26059.0909
#> 39:      f age_group3     total  440  26000  441  26059.0909
#> 40: female age_group4     total  228  13853  228  13853.0000
#> 41:      f age_group4     total  228  13853  228  13853.0000
#> 42: female age_group5     total   84   5114   85   5174.8810
#> 43:      f age_group5     total   84   5114   85   5174.8810
#> 44: female age_group6     total    6    513    6    513.0000
#> 45:      f age_group6     total    6    513    6    513.0000
#> 46:  Total      Total cnt_males 2296 135673 2295 135613.9090
#> 47:  Total age_group1 cnt_males 1015  60163 1016  60222.2739
#> 48:  Total       ag1a cnt_males 1015  60163 1016  60222.2739
#> 49:  Total age_group2 cnt_males  571  33439  570  33380.4378
#> 50:  Total       ag2a cnt_males  571  33439  570  33380.4378
#> 51:  Total age_group3 cnt_males  424  25289  422  25169.7123
#> 52:  Total age_group4 cnt_males  195  11353  197  11469.4410
#> 53:  Total age_group5 cnt_males   84   5026   84   5026.0000
#> 54:  Total age_group6 cnt_males    7    403    8    460.5714
#> 55:   male      Total cnt_males 2296 135673 2295 135613.9090
#> 56:      m      Total cnt_males 2296 135673 2295 135613.9090
#> 57:   male age_group1 cnt_males 1015  60163 1016  60222.2739
#> 58:      m age_group1 cnt_males 1015  60163 1016  60222.2739
#> 59:   male       ag1a cnt_males 1015  60163 1016  60222.2739
#> 60:      m       ag1a cnt_males 1015  60163 1016  60222.2739
#> 61:   male age_group2 cnt_males  571  33439  570  33380.4378
#> 62:      m age_group2 cnt_males  571  33439  570  33380.4378
#> 63:   male       ag2a cnt_males  571  33439  570  33380.4378
#> 64:      m       ag2a cnt_males  571  33439  570  33380.4378
#> 65:   male age_group3 cnt_males  424  25289  422  25169.7123
#> 66:      m age_group3 cnt_males  424  25289  422  25169.7123
#> 67:   male age_group4 cnt_males  195  11353  197  11469.4410
#> 68:      m age_group4 cnt_males  195  11353  197  11469.4410
#> 69:   male age_group5 cnt_males   84   5026   84   5026.0000
#> 70:      m age_group5 cnt_males   84   5026   84   5026.0000
#> 71:   male age_group6 cnt_males    7    403    8    460.5714
#> 72:      m age_group6 cnt_males    7    403    8    460.5714
#> 73: female      Total cnt_males    0      0    0      0.0000
#> 74:      f      Total cnt_males    0      0    0      0.0000
#> 75: female age_group1 cnt_males    0      0    0      0.0000
#> 76:      f age_group1 cnt_males    0      0    0      0.0000
#> 77: female       ag1a cnt_males    0      0    0      0.0000
#> 78:      f       ag1a cnt_males    0      0    0      0.0000
#> 79: female age_group2 cnt_males    0      0    0      0.0000
#> 80:      f age_group2 cnt_males    0      0    0      0.0000
#> 81: female       ag2a cnt_males    0      0    0      0.0000
#> 82:      f       ag2a cnt_males    0      0    0      0.0000
#> 83: female age_group3 cnt_males    0      0    0      0.0000
#> 84:      f age_group3 cnt_males    0      0    0      0.0000
#> 85: female age_group4 cnt_males    0      0    0      0.0000
#> 86:      f age_group4 cnt_males    0      0    0      0.0000
#> 87: female age_group5 cnt_males    0      0    0      0.0000
#> 88:      f age_group5 cnt_males    0      0    0      0.0000
#> 89: female age_group6 cnt_males    0      0    0      0.0000
#> 90:      f age_group6 cnt_males    0      0    0      0.0000
#>        sex        age     vname  uwc     wc puwc         pwc

# numerical variables (positive variables using flex-function)
# we also write the config to a yaml file
f_yaml <- tempfile(fileext = ".yaml")

# create a ptable using functionality from the ptable-pkg
# a single ptable for all cells
ptab1 <- ptable::pt_ex_nums(parity = TRUE, separation = FALSE)

# a single ptab for all cells except for very small ones
ptab2 <- ptable::pt_ex_nums(parity = TRUE, separation = TRUE)

# different ptables for cells with even/odd number of contributors
# and very small cells
ptab3 <- ptable::pt_ex_nums(parity = FALSE, separation = TRUE)

p_nums1 <- ck_params_nums(
  ptab = ptab1,
  type = "top_contr",
  top_k = 3,
  mult_params = ck_flexparams(
    fp = 1000,
    p = c(0.30, 0.03),
    epsilon = c(1, 0.5, 0.2),
    q = 3),
  mu_c = 2,
  same_key = FALSE,
  use_zero_rkeys = FALSE,
  path = f_yaml)
#> yaml configuration '/tmp/RtmpV7PINT/file19627c3e8f63.yaml' successfully written.

# we read the parameters from the yaml-file
p_nums1 <- ck_read_yaml(path = f_yaml)

# for variables with positive and negative values
p_nums2 <- ck_params_nums(
  ptab = ptab2,
  type = "top_contr",
  top_k = 3,
  mult_params = ck_flexparams(
    fp = 1000,
    p = c(0.15, 0.02),
    epsilon = c(1, 0.4, 0.15),
    q = 3),
  mu_c = 2,
  same_key = FALSE)

# simple perturbation parameters (not using the flex-function approach)
p_nums3 <- ck_params_nums(
  ptab = ptab3,
  type = "mean",
  mult_params = ck_simpleparams(p = 0.25),
  mu_c = 2,
  same_key = FALSE)

# use `p_nums1` for all variables
tab$params_nums_set(p_nums1, c("savings", "income", "expend"))
#> --> setting perturbation parameters for variable 'savings'
#> --> setting perturbation parameters for variable 'income'
#> --> setting perturbation parameters for variable 'expend'

# use different parameters for variable `mixed`
tab$params_nums_set(p_nums2, v = "mixed")
#> --> setting perturbation parameters for variable 'mixed'

# identify sensitive cells to which extra protection (`mu_c`) is added.
tab$supp_p(v = "income", p = 85)
#> computing contributing indices | rawdata <--> table; this might take a while
#> p%-rule: 0 new sensitive cells (incl. duplicates) found (total: 0)
tab$supp_pq(v = "income", p = 85, q = 90)
#> computing contributing indices | rawdata <--> table; this might take a while
#> pq-rule: 0 new sensitive cells (incl. duplicates) found (total: 0)
tab$supp_nk(v = "income", n = 2, k = 90)
#> computing contributing indices | rawdata <--> table; this might take a while
#> nk-rule: 0 new sensitive cells (incl. duplicates) found (total: 0)
tab$supp_freq(v = "income", n = 14, weighted = FALSE)
#> freq-rule: 5 new sensitive cells (incl. duplicates) found (total: 5)
tab$supp_val(v = "income", n = 10000, weighted = TRUE)
#> val-rule: 0 new sensitive cells (incl. duplicates) found (total: 5)
tab$supp_cells(
  v = "income",
  inp = data.frame(
    sex = c("female", "female"),
    "age" = c("age_group1", "age_group3")
  )
)
#> cell-rule: 2 new sensitive cells (incl. duplicates) found (total: 7)

# perturb variables
tab$perturb(v = c("income", "savings"))
#> Numeric variable 'income' was perturbed.
#> Numeric variable 'savings' was perturbed.

# extract results
tab$numtab("income", mean_before_sum = TRUE)
#>        sex        age  vname      uws         ws        pws
#>  1:  Total      Total income 22952978 1359983951 1360134579
#>  2:  Total age_group1 income  9810547  577588504  577478133
#>  3:  Total       ag1a income  9810547  577588504  577478133
#>  4:  Total age_group2 income  5692119  339267248  339210791
#>  5:  Total       ag2a income  5692119  339267248  339210791
#>  6:  Total age_group3 income  4406946  260501177  260435755
#>  7:  Total age_group4 income  2133543  128141218  128105840
#>  8:  Total age_group5 income   848151   50483631   50626796
#>  9:  Total age_group6 income    61672    4002173    3855253
#> 10:   male      Total income 11262049  661571824  661559373
#> 11:      m      Total income 11262049  661571824  661559373
#> 12:   male age_group1 income  4877164  285031644  285099183
#> 13:      m age_group1 income  4877164  285031644  285099183
#> 14:   male       ag1a income  4877164  285031644  285099183
#> 15:      m       ag1a income  4877164  285031644  285099183
#> 16:   male age_group2 income  2811379  166292035  166417241
#> 17:      m age_group2 income  2811379  166292035  166417241
#> 18:   male       ag2a income  2811379  166292035  166417241
#> 19:      m       ag2a income  2811379  166292035  166417241
#> 20:   male age_group3 income  2168169  129229861  129110697
#> 21:      m age_group3 income  2168169  129229861  129110697
#> 22:   male age_group4 income   978510   56934088   56954090
#> 23:      m age_group4 income   978510   56934088   56954090
#> 24:   male age_group5 income   393134   22423083   22396848
#> 25:      m age_group5 income   393134   22423083   22396848
#> 26:   male age_group6 income    33693    1661113    1686102
#> 27:      m age_group6 income    33693    1661113    1686102
#> 28: female      Total income 11690929  698412127  698388207
#> 29:      f      Total income 11690929  698412127  698388207
#> 30: female age_group1 income  4933383  292556860  292548783
#> 31:      f age_group1 income  4933383  292556860  292548783
#> 32: female       ag1a income  4933383  292556860  292548783
#> 33:      f       ag1a income  4933383  292556860  292548783
#> 34: female age_group2 income  2880740  172975213  172976307
#> 35:      f age_group2 income  2880740  172975213  172976307
#> 36: female       ag2a income  2880740  172975213  172976307
#> 37:      f       ag2a income  2880740  172975213  172976307
#> 38: female age_group3 income  2238777  131271316  131158274
#> 39:      f age_group3 income  2238777  131271316  131158274
#> 40: female age_group4 income  1155033   71207130   71160151
#> 41:      f age_group4 income  1155033   71207130   71160151
#> 42: female age_group5 income   455017   28060548   28056010
#> 43:      f age_group5 income   455017   28060548   28056010
#> 44: female age_group6 income    27979    2341060    2082684
#> 45:      f age_group6 income    27979    2341060    2082684
#>        sex        age  vname      uws         ws        pws
tab$numtab("income", mean_before_sum = FALSE)
#>        sex        age  vname      uws         ws        pws
#>  1:  Total      Total income 22952978 1359983951 1360059263
#>  2:  Total age_group1 income  9810547  577588504  577533316
#>  3:  Total       ag1a income  9810547  577588504  577533316
#>  4:  Total age_group2 income  5692119  339267248  339239018
#>  5:  Total       ag2a income  5692119  339267248  339239018
#>  6:  Total age_group3 income  4406946  260501177  260468464
#>  7:  Total age_group4 income  2133543  128141218  128123528
#>  8:  Total age_group5 income   848151   50483631   50555163
#>  9:  Total age_group6 income    61672    4002173    3928026
#> 10:   male      Total income 11262049  661571824  661565598
#> 11:      m      Total income 11262049  661571824  661565598
#> 12:   male age_group1 income  4877164  285031644  285065411
#> 13:      m age_group1 income  4877164  285031644  285065411
#> 14:   male       ag1a income  4877164  285031644  285065411
#> 15:      m       ag1a income  4877164  285031644  285065411
#> 16:   male age_group2 income  2811379  166292035  166354626
#> 17:      m age_group2 income  2811379  166292035  166354626
#> 18:   male       ag2a income  2811379  166292035  166354626
#> 19:      m       ag2a income  2811379  166292035  166354626
#> 20:   male age_group3 income  2168169  129229861  129170265
#> 21:      m age_group3 income  2168169  129229861  129170265
#> 22:   male age_group4 income   978510   56934088   56944088
#> 23:      m age_group4 income   978510   56934088   56944088
#> 24:   male age_group5 income   393134   22423083   22409962
#> 25:      m age_group5 income   393134   22423083   22409962
#> 26:   male age_group6 income    33693    1661113    1673561
#> 27:      m age_group6 income    33693    1661113    1673561
#> 28: female      Total income 11690929  698412127  698400167
#> 29:      f      Total income 11690929  698412127  698400167
#> 30: female age_group1 income  4933383  292556860  292552822
#> 31:      f age_group1 income  4933383  292556860  292552822
#> 32: female       ag1a income  4933383  292556860  292552822
#> 33:      f       ag1a income  4933383  292556860  292552822
#> 34: female age_group2 income  2880740  172975213  172975760
#> 35:      f age_group2 income  2880740  172975213  172975760
#> 36: female       ag2a income  2880740  172975213  172975760
#> 37:      f       ag2a income  2880740  172975213  172975760
#> 38: female age_group3 income  2238777  131271316  131214783
#> 39:      f age_group3 income  2238777  131271316  131214783
#> 40: female age_group4 income  1155033   71207130   71183637
#> 41:      f age_group4 income  1155033   71207130   71183637
#> 42: female age_group5 income   455017   28060548   28058279
#> 43:      f age_group5 income   455017   28060548   28058279
#> 44: female age_group6 income    27979    2341060    2208096
#> 45:      f age_group6 income    27979    2341060    2208096
#>        sex        age  vname      uws         ws        pws
tab$numtab("savings")
#>        sex        age   vname     uws        ws         pws
#>  1:  Total      Total savings 2273532 135170381 135166646.2
#>  2:  Total age_group1 savings  982386  58643412  58638680.7
#>  3:  Total       ag1a savings  982386  58643412  58638680.7
#>  4:  Total age_group2 savings  552336  32444387  32442052.1
#>  5:  Total       ag2a savings  552336  32444387  32442052.1
#>  6:  Total age_group3 savings  437101  25955604  25954971.3
#>  7:  Total age_group4 savings  214661  12644077  12641532.9
#>  8:  Total age_group5 savings   80451   4967875   4971042.4
#>  9:  Total age_group6 savings    6597    515026    506122.5
#> 10:   male      Total savings 1159816  68649082  68650410.8
#> 11:      m      Total savings 1159816  68649082  68650410.8
#> 12:   male age_group1 savings  517660  30885609  30887281.0
#> 13:      m age_group1 savings  517660  30885609  30887281.0
#> 14:   male       ag1a savings  517660  30885609  30887281.0
#> 15:      m       ag1a savings  517660  30885609  30887281.0
#> 16:   male age_group2 savings  280923  16380629  16385418.9
#> 17:      m age_group2 savings  280923  16380629  16385418.9
#> 18:   male       ag2a savings  280923  16380629  16385418.9
#> 19:      m       ag2a savings  280923  16380629  16385418.9
#> 20:   male age_group3 savings  214970  12897451  12893166.7
#> 21:      m age_group3 savings  214970  12897451  12893166.7
#> 22:   male age_group4 savings   99420   5602822   5605660.0
#> 23:      m age_group4 savings   99420   5602822   5605660.0
#> 24:   male age_group5 savings   43233   2635701   2634651.3
#> 25:      m age_group5 savings   43233   2635701   2634651.3
#> 26:   male age_group6 savings    3610    246870    247909.9
#> 27:      m age_group6 savings    3610    246870    247909.9
#> 28: female      Total savings 1113716  66521299  66524978.9
#> 29:      f      Total savings 1113716  66521299  66524978.9
#> 30: female age_group1 savings  464726  27757803  27759435.7
#> 31:      f age_group1 savings  464726  27757803  27759435.7
#> 32: female       ag1a savings  464726  27757803  27759435.7
#> 33:      f       ag1a savings  464726  27757803  27759435.7
#> 34: female age_group2 savings  271413  16063758  16065574.7
#> 35:      f age_group2 savings  271413  16063758  16065574.7
#> 36: female       ag2a savings  271413  16063758  16065574.7
#> 37:      f       ag2a savings  271413  16063758  16065574.7
#> 38: female age_group3 savings  222131  13058153  13051506.2
#> 39:      f age_group3 savings  222131  13058153  13051506.2
#> 40: female age_group4 savings  115241   7041255   7039494.2
#> 41:      f age_group4 savings  115241   7041255   7039494.2
#> 42: female age_group5 savings   37218   2332174   2332090.1
#> 43:      f age_group5 savings   37218   2332174   2332090.1
#> 44: female age_group6 savings    2987    268156    259820.0
#> 45:      f age_group6 savings    2987    268156    259820.0
#>        sex        age   vname     uws        ws         pws

# results can be resetted, too
tab$reset_cntvars(v = "cnt_males")

# we can then set other parameters and perturb again
tab$params_cnts_set(val = p_cnts1, v = "cnt_males")
#> --> setting perturbation parameters for variable 'cnt_males'

tab$perturb(v = "cnt_males")
#> Count variable 'cnt_males' was perturbed.

# write results to a .csv file
tab$freqtab(
  v = c("total", "cnt_males"),
  path = file.path(tempdir(), "outtab.csv")
)
#> File '/tmp/RtmpV7PINT/outtab.csv' successfully written to disk.
#> NULL

# show results containing weighted and unweighted results
tab$freqtab(v = c("total", "cnt_males"))
#>        sex        age     vname  uwc     wc puwc         pwc
#>  1:  Total      Total     total 4580 272100 4581 272159.4105
#>  2:  Total age_group1     total 1969 117044 1970 117103.4434
#>  3:  Total       ag1a     total 1969 117044 1970 117103.4434
#>  4:  Total age_group2     total 1143  67505 1141  67386.8810
#>  5:  Total       ag2a     total 1143  67505 1141  67386.8810
#>  6:  Total age_group3     total  864  51289  866  51407.7245
#>  7:  Total age_group4     total  423  25206  423  25206.0000
#>  8:  Total age_group5     total  168  10140  167  10079.6429
#>  9:  Total age_group6     total   13    916   13    916.0000
#> 10:   male      Total     total 2296 135673 2296 135673.0000
#> 11:      m      Total     total 2296 135673 2296 135673.0000
#> 12:   male age_group1     total 1015  60163 1015  60163.0000
#> 13:      m age_group1     total 1015  60163 1015  60163.0000
#> 14:   male       ag1a     total 1015  60163 1015  60163.0000
#> 15:      m       ag1a     total 1015  60163 1015  60163.0000
#> 16:   male age_group2     total  571  33439  571  33439.0000
#> 17:      m age_group2     total  571  33439  571  33439.0000
#> 18:   male       ag2a     total  571  33439  571  33439.0000
#> 19:      m       ag2a     total  571  33439  571  33439.0000
#> 20:   male age_group3     total  424  25289  423  25229.3561
#> 21:      m age_group3     total  424  25289  423  25229.3561
#> 22:   male age_group4     total  195  11353  196  11411.2205
#> 23:      m age_group4     total  195  11353  196  11411.2205
#> 24:   male age_group5     total   84   5026   84   5026.0000
#> 25:      m age_group5     total   84   5026   84   5026.0000
#> 26:   male age_group6     total    7    403    8    460.5714
#> 27:      m age_group6     total    7    403    8    460.5714
#> 28: female      Total     total 2284 136427 2284 136427.0000
#> 29:      f      Total     total 2284 136427 2284 136427.0000
#> 30: female age_group1     total  954  56881  953  56821.3763
#> 31:      f age_group1     total  954  56881  953  56821.3763
#> 32: female       ag1a     total  954  56881  953  56821.3763
#> 33:      f       ag1a     total  954  56881  953  56821.3763
#> 34: female age_group2     total  572  34066  572  34066.0000
#> 35:      f age_group2     total  572  34066  572  34066.0000
#> 36: female       ag2a     total  572  34066  572  34066.0000
#> 37:      f       ag2a     total  572  34066  572  34066.0000
#> 38: female age_group3     total  440  26000  441  26059.0909
#> 39:      f age_group3     total  440  26000  441  26059.0909
#> 40: female age_group4     total  228  13853  228  13853.0000
#> 41:      f age_group4     total  228  13853  228  13853.0000
#> 42: female age_group5     total   84   5114   85   5174.8810
#> 43:      f age_group5     total   84   5114   85   5174.8810
#> 44: female age_group6     total    6    513    6    513.0000
#> 45:      f age_group6     total    6    513    6    513.0000
#> 46:  Total      Total cnt_males 2296 135673 2296 135673.0000
#> 47:  Total age_group1 cnt_males 1015  60163 1015  60163.0000
#> 48:  Total       ag1a cnt_males 1015  60163 1015  60163.0000
#> 49:  Total age_group2 cnt_males  571  33439  571  33439.0000
#> 50:  Total       ag2a cnt_males  571  33439  571  33439.0000
#> 51:  Total age_group3 cnt_males  424  25289  423  25229.3561
#> 52:  Total age_group4 cnt_males  195  11353  196  11411.2205
#> 53:  Total age_group5 cnt_males   84   5026   84   5026.0000
#> 54:  Total age_group6 cnt_males    7    403    8    460.5714
#> 55:   male      Total cnt_males 2296 135673 2296 135673.0000
#> 56:      m      Total cnt_males 2296 135673 2296 135673.0000
#> 57:   male age_group1 cnt_males 1015  60163 1015  60163.0000
#> 58:      m age_group1 cnt_males 1015  60163 1015  60163.0000
#> 59:   male       ag1a cnt_males 1015  60163 1015  60163.0000
#> 60:      m       ag1a cnt_males 1015  60163 1015  60163.0000
#> 61:   male age_group2 cnt_males  571  33439  571  33439.0000
#> 62:      m age_group2 cnt_males  571  33439  571  33439.0000
#> 63:   male       ag2a cnt_males  571  33439  571  33439.0000
#> 64:      m       ag2a cnt_males  571  33439  571  33439.0000
#> 65:   male age_group3 cnt_males  424  25289  423  25229.3561
#> 66:      m age_group3 cnt_males  424  25289  423  25229.3561
#> 67:   male age_group4 cnt_males  195  11353  196  11411.2205
#> 68:      m age_group4 cnt_males  195  11353  196  11411.2205
#> 69:   male age_group5 cnt_males   84   5026   84   5026.0000
#> 70:      m age_group5 cnt_males   84   5026   84   5026.0000
#> 71:   male age_group6 cnt_males    7    403    8    460.5714
#> 72:      m age_group6 cnt_males    7    403    8    460.5714
#> 73: female      Total cnt_males    0      0    0      0.0000
#> 74:      f      Total cnt_males    0      0    0      0.0000
#> 75: female age_group1 cnt_males    0      0    0      0.0000
#> 76:      f age_group1 cnt_males    0      0    0      0.0000
#> 77: female       ag1a cnt_males    0      0    0      0.0000
#> 78:      f       ag1a cnt_males    0      0    0      0.0000
#> 79: female age_group2 cnt_males    0      0    0      0.0000
#> 80:      f age_group2 cnt_males    0      0    0      0.0000
#> 81: female       ag2a cnt_males    0      0    0      0.0000
#> 82:      f       ag2a cnt_males    0      0    0      0.0000
#> 83: female age_group3 cnt_males    0      0    0      0.0000
#> 84:      f age_group3 cnt_males    0      0    0      0.0000
#> 85: female age_group4 cnt_males    0      0    0      0.0000
#> 86:      f age_group4 cnt_males    0      0    0      0.0000
#> 87: female age_group5 cnt_males    0      0    0      0.0000
#> 88:      f age_group5 cnt_males    0      0    0      0.0000
#> 89: female age_group6 cnt_males    0      0    0      0.0000
#> 90:      f age_group6 cnt_males    0      0    0      0.0000
#>        sex        age     vname  uwc     wc puwc         pwc

# utility measures for a count variable
tab$measures_cnts(v = "total", exclude_zeros = TRUE)
#> $overview
#>    noise cnt        pct
#> 1:    -2   1 0.02222222
#> 2:    -1  11 0.24444444
#> 3:     0  24 0.53333333
#> 4:     1   7 0.15555556
#> 5:     2   2 0.04444444
#> 
#> $measures
#>       what    d1    d2    d3
#>  1:    Min 0.000 0.000 0.000
#>  2:    Q10 0.000 0.000 0.000
#>  3:    Q20 0.000 0.000 0.000
#>  4:    Q30 0.000 0.000 0.000
#>  5:    Q40 0.000 0.000 0.000
#>  6:   Mean 0.533 0.008 0.019
#>  7: Median 0.000 0.000 0.000
#>  8:    Q60 1.000 0.001 0.013
#>  9:    Q70 1.000 0.002 0.022
#> 10:    Q80 1.000 0.002 0.030
#> 11:    Q90 1.000 0.006 0.037
#> 12:    Q95 1.800 0.012 0.054
#> 13:    Q99 2.000 0.143 0.183
#> 14:    Max 2.000 0.143 0.183
#> 
#> $cumdistr_d1
#>    cat cnt       pct
#> 1:   0  24 0.5333333
#> 2:   1  42 0.9333333
#> 3:   2  45 1.0000000
#> 
#> $cumdistr_d2
#>            cat cnt       pct
#> 1:    [0,0.02]  43 0.9555556
#> 2: (0.02,0.05]  43 0.9555556
#> 3:  (0.05,0.1]  43 0.9555556
#> 4:   (0.1,0.2]  45 1.0000000
#> 5:   (0.2,0.3]  45 1.0000000
#> 6:   (0.3,0.4]  45 1.0000000
#> 7:   (0.4,0.5]  45 1.0000000
#> 8:   (0.5,Inf]  45 1.0000000
#> 
#> $cumdistr_d3
#>            cat cnt       pct
#> 1:    [0,0.02]  31 0.6888889
#> 2: (0.02,0.05]  41 0.9111111
#> 3:  (0.05,0.1]  43 0.9555556
#> 4:   (0.1,0.2]  45 1.0000000
#> 5:   (0.2,0.3]  45 1.0000000
#> 6:   (0.3,0.4]  45 1.0000000
#> 7:   (0.4,0.5]  45 1.0000000
#> 8:   (0.5,Inf]  45 1.0000000
#> 
#> $false_zero
#> [1] 0
#> 
#> $false_nonzero
#> [1] 0
#> 
#> $exclude_zeros
#> [1] TRUE
#> 

# modifications for perturbed count variables
tab$mod_cnts()
#>         sex        age row_nr pert       ckey  countvar
#>   1:  Total      Total     16    1 0.69925386     total
#>   2:  Total age_group1     16    1 0.70477037     total
#>   3:  Total       ag1a     16    1 0.70477037     total
#>   4:  Total age_group2     13   -2 0.03503265     total
#>   5:  Total       ag2a     13   -2 0.03503265     total
#>  ---                                                   
#> 131:      f age_group4     -1    0 0.00000000 cnt_males
#> 132: female age_group5     -1    0 0.00000000 cnt_males
#> 133:      f age_group5     -1    0 0.00000000 cnt_males
#> 134: female age_group6     -1    0 0.00000000 cnt_males
#> 135:      f age_group6     -1    0 0.00000000 cnt_males

# display a summary about utility measures
tab$summary()
#> ┌──────────────────────────────────────────────┐
#> │Utility measures for perturbed count variables│
#> └──────────────────────────────────────────────┘
#> ── Distribution statistics of perturbations ────────────────────────────────────
#>          countvar Min Q10  Q20  Q30 Q40   Mean Median Q60 Q70 Q80 Q90 Q95  Q99
#> 1:          total  -2  -1 -0.2  0.0   0  0.044      0   0   0   1   1   1 1.56
#> 2: cnt_highincome  -3  -2 -1.0 -0.8   0 -0.178      0   0   1   1   1   1 1.00
#> 3:      cnt_males  -1   0  0.0  0.0   0  0.067      0   0   0   0   1   1 1.00
#>    Max
#> 1:   2
#> 2:   1
#> 3:   1
#> 
#> ── Distance-based measures ─────────────────────────────────────────────────────
#> ✔ Variable: 'total'
#> 
#>       what    d1    d2    d3
#>  1:    Min 0.000 0.000 0.000
#>  2:    Q10 0.000 0.000 0.000
#>  3:    Q20 0.000 0.000 0.000
#>  4:    Q30 0.000 0.000 0.000
#>  5:    Q40 0.000 0.000 0.000
#>  6:   Mean 0.533 0.008 0.019
#>  7: Median 0.000 0.000 0.000
#>  8:    Q60 1.000 0.001 0.013
#>  9:    Q70 1.000 0.002 0.022
#> 10:    Q80 1.000 0.002 0.030
#> 11:    Q90 1.000 0.006 0.037
#> 12:    Q95 1.800 0.012 0.054
#> 13:    Q99 2.000 0.143 0.183
#> 14:    Max 2.000 0.143 0.183
#> 
#> ✔ Variable: 'cnt_males'
#> 
#>       what    d1    d2    d3
#>  1:    Min 0.000 0.000 0.000
#>  2:    Q10 0.000 0.000 0.000
#>  3:    Q20 0.000 0.000 0.000
#>  4:    Q30 0.000 0.000 0.000
#>  5:    Q40 0.000 0.000 0.000
#>  6:   Mean 0.333 0.017 0.027
#>  7: Median 0.000 0.000 0.000
#>  8:    Q60 0.000 0.000 0.000
#>  9:    Q70 1.000 0.002 0.024
#> 10:    Q80 1.000 0.005 0.033
#> 11:    Q90 1.000 0.060 0.095
#> 12:    Q95 1.000 0.143 0.183
#> 13:    Q99 1.000 0.143 0.183
#> 14:    Max 1.000 0.143 0.183
#> 
#> ✔ Variable: 'cnt_highincome'
#> 
#>       what  d1    d2    d3
#>  1:    Min 0.0 0.000 0.000
#>  2:    Q10 0.0 0.000 0.000
#>  3:    Q20 0.0 0.000 0.000
#>  4:    Q30 1.0 0.005 0.034
#>  5:    Q40 1.0 0.007 0.041
#>  6:   Mean 1.0 0.022 0.069
#>  7: Median 1.0 0.010 0.049
#>  8:    Q60 1.0 0.018 0.066
#>  9:    Q70 1.0 0.024 0.078
#> 10:    Q80 1.0 0.045 0.117
#> 11:    Q90 2.1 0.053 0.187
#> 12:    Q95 3.0 0.067 0.187
#> 13:    Q99 3.0 0.113 0.242
#> 14:    Max 3.0 0.143 0.278
#> 
#> ┌──────────────────────────────────────────────────┐
#> │Utility measures for perturbed numerical variables│
#> └──────────────────────────────────────────────────┘
#> ── Distribution statistics of perturbations ────────────────────────────────────
#> Warning: no non-missing arguments to min; returning Inf
#> Warning: no non-missing arguments to max; returning -Inf
#> Warning: no non-missing arguments to min; returning Inf
#> Warning: no non-missing arguments to max; returning -Inf
#>      vname         Min        Q10        Q20        Q30       Q40     Mean
#> 1:  expend         Inf         NA         NA         NA        NA      NaN
#> 2:  income -132963.842 -58370.720 -37208.028 -22332.659 -11960.26 -7566.71
#> 3: savings   -8903.494  -5880.621  -3844.714  -1760.751   -303.40  -304.11
#> 4:   mixed         Inf         NA         NA         NA        NA      NaN
#>       Median       Q60      Q70       Q80       Q90       Q95       Q99
#> 1:        NA        NA       NA        NA        NA        NA        NA
#> 2: -4038.389 -1142.775 8109.591 33767.314 62591.315 62591.315 73648.595
#> 3:  1328.848  1632.681 1671.965  2020.916  3679.869  4789.947  4789.947
#> 4:        NA        NA       NA        NA        NA        NA        NA
#>          Max
#> 1:      -Inf
#> 2: 75311.868
#> 3:  4789.947
#> 4:      -Inf
# }