ck_read_yaml() allows to create perturbation parameter inputs from yaml-files that were previously created using ck_params_cnts() or ck_params_nums().

ck_read_yaml(path)

Arguments

path

a path to a yaml-input file

Value

an object object suitable as input to method $params_nums_set() for the perturbation of continous variables in case path was created using ck_params_nums() or an object suitable as input for $params_cnts_set() for the perturbation of counts and frequencies if the input file was generated using ck_params_cnts().

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
#>        <int> <int> <int> <int>    <int> <int> <fctr>     <fctr>   <int>  <num>
#>    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
#>        <num>   <num>   <int>           <int>             <num>       <num>
#>    1:   5780      12       1              30          25.00000           0
#>    2:   2530      28       1              44          25.00000           1
#>    3:   6920     550       1              81          25.00000           0
#>    4:   7960     870       1              96          25.00000           0
#>    5:   9030      20       2              41          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              84          16.66667           1
#> 4577:   1420     987    1000              89          16.66667           0
#> 4578:   8900     684    1000              72          16.66667           0
#> 4579:   3880     294    1000              45          16.66667           1
#> 4580:   4830     911    1000              29          16.66667           0
x[, cnt_males := ifelse(sex == "male", 1, 0)]
#>       urbrur  roof walls water electcon relat    sex        age hhcivil expend
#>        <int> <int> <int> <int>    <int> <int> <fctr>     <fctr>   <int>  <num>
#>    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
#>        <num>   <num>   <int>           <int>             <num>       <num>
#>    1:   5780      12       1              30          25.00000           0
#>    2:   2530      28       1              44          25.00000           1
#>    3:   6920     550       1              81          25.00000           0
#>    4:   7960     870       1              96          25.00000           0
#>    5:   9030      20       2              41          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              84          16.66667           1
#> 4577:   1420     987    1000              89          16.66667           0
#> 4578:   8900     684    1000              72          16.66667           0
#> 4579:   3880     294    1000              45          16.66667           1
#> 4580:   4830     911    1000              29          16.66667           0
#>       cnt_males
#>           <num>
#>    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
#>        <int> <int> <int> <int>    <int> <int> <fctr>     <fctr>   <int>  <num>
#>    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
#>        <num>   <num>   <int>           <int>             <num>       <num>
#>    1:   5780      12       1              30          25.00000           0
#>    2:   2530      28       1              44          25.00000           1
#>    3:   6920     550       1              81          25.00000           0
#>    4:   7960     870       1              96          25.00000           0
#>    5:   9030      20       2              41          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              84          16.66667           1
#> 4577:   1420     987    1000              89          16.66667           0
#> 4578:   8900     684    1000              72          16.66667           0
#> 4579:   3880     294    1000              45          16.66667           1
#> 4580:   4830     911    1000              29          16.66667           0
#>       cnt_males cnt_highincome
#>           <num>          <num>
#>    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
#>        <int> <int> <int> <int>    <int> <int> <fctr>     <fctr>   <int>  <num>
#>    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
#>        <num>   <num>   <int>           <int>             <num>       <num>
#>    1:   5780      12       1              30          25.00000           0
#>    2:   2530      28       1              44          25.00000           1
#>    3:   6920     550       1              81          25.00000           0
#>    4:   7960     870       1              96          25.00000           0
#>    5:   9030      20       2              41          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              84          16.66667           1
#> 4577:   1420     987    1000              89          16.66667           0
#> 4578:   8900     684    1000              72          16.66667           0
#> 4579:   3880     294    1000              45          16.66667           1
#> 4580:   4830     911    1000              29          16.66667           0
#>       cnt_males cnt_highincome mixed
#>           <num>          <num> <int>
#>    1:         1              0    -1
#>    2:         0              0    -5
#>    3:         1              0    -9
#>    4:         1              0    -7
#>    5:         1              1     8
#>   ---                               
#> 4576:         0              0    -9
#> 4577:         1              0     4
#> 4578:         1              0    -9
#> 4579:         0              0     7
#> 4580:         1              0    -9

# 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
#>    <char> <int>  <lgcl> <char>
#> 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
#>        <char> <int>  <lgcl>     <char>
#> 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/Rtmpu511Tl/file1fa57e63194e.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
#>     <char>     <char>    <char> <num>  <num> <num>       <num>
#>  1:  Total      Total     total  4580 275978  4582 276098.5144
#>  2:  Total age_group1     total  1969 119009  1970 119069.4413
#>  3:  Total       ag1a     total  1969 119009  1970 119069.4413
#>  4:  Total age_group2     total  1143  67758  1143  67758.0000
#>  5:  Total       ag2a     total  1143  67758  1143  67758.0000
#>  6:  Total age_group3     total   864  52694   864  52694.0000
#>  7:  Total age_group4     total   423  25256   422  25196.2931
#>  8:  Total age_group5     total   168  10474   166  10349.3095
#>  9:  Total age_group6     total    13    787    14    847.5385
#> 10:   male      Total     total  2296 137589  2298 137708.8510
#> 11:      m      Total     total  2296 137589  2298 137708.8510
#> 12:   male age_group1     total  1015  61767  1015  61767.0000
#> 13:      m age_group1     total  1015  61767  1015  61767.0000
#> 14:   male       ag1a     total  1015  61767  1015  61767.0000
#> 15:      m       ag1a     total  1015  61767  1015  61767.0000
#> 16:   male age_group2     total   571  33706   571  33706.0000
#> 17:      m age_group2     total   571  33706   571  33706.0000
#> 18:   male       ag2a     total   571  33706   571  33706.0000
#> 19:      m       ag2a     total   571  33706   571  33706.0000
#> 20:   male age_group3     total   424  25075   424  25075.0000
#> 21:      m age_group3     total   424  25075   424  25075.0000
#> 22:   male age_group4     total   195  11466   194  11407.2000
#> 23:      m age_group4     total   195  11466   194  11407.2000
#> 24:   male age_group5     total    84   5217    84   5217.0000
#> 25:      m age_group5     total    84   5217    84   5217.0000
#> 26:   male age_group6     total     7    358     7    358.0000
#> 27:      m age_group6     total     7    358     7    358.0000
#> 28: female      Total     total  2284 138389  2282 138267.8187
#> 29:      f      Total     total  2284 138389  2282 138267.8187
#> 30: female age_group1     total   954  57242   953  57181.9979
#> 31:      f age_group1     total   954  57242   953  57181.9979
#> 32: female       ag1a     total   954  57242   953  57181.9979
#> 33:      f       ag1a     total   954  57242   953  57181.9979
#> 34: female age_group2     total   572  34052   573  34111.5315
#> 35:      f age_group2     total   572  34052   573  34111.5315
#> 36: female       ag2a     total   572  34052   573  34111.5315
#> 37:      f       ag2a     total   572  34052   573  34111.5315
#> 38: female age_group3     total   440  27619   438  27493.4591
#> 39:      f age_group3     total   440  27619   438  27493.4591
#> 40: female age_group4     total   228  13790   227  13729.5175
#> 41:      f age_group4     total   228  13790   227  13729.5175
#> 42: female age_group5     total    84   5257    84   5257.0000
#> 43:      f age_group5     total    84   5257    84   5257.0000
#> 44: female age_group6     total     6    429     5    357.5000
#> 45:      f age_group6     total     6    429     5    357.5000
#> 46:  Total      Total cnt_males  2296 137589  2299 137768.7766
#> 47:  Total age_group1 cnt_males  1015  61767  1016  61827.8542
#> 48:  Total       ag1a cnt_males  1015  61767  1016  61827.8542
#> 49:  Total age_group2 cnt_males   571  33706   572  33765.0298
#> 50:  Total       ag2a cnt_males   571  33706   572  33765.0298
#> 51:  Total age_group3 cnt_males   424  25075   424  25075.0000
#> 52:  Total age_group4 cnt_males   195  11466   193  11348.4000
#> 53:  Total age_group5 cnt_males    84   5217    84   5217.0000
#> 54:  Total age_group6 cnt_males     7    358     8    409.1429
#> 55:   male      Total cnt_males  2296 137589  2299 137768.7766
#> 56:      m      Total cnt_males  2296 137589  2299 137768.7766
#> 57:   male age_group1 cnt_males  1015  61767  1016  61827.8542
#> 58:      m age_group1 cnt_males  1015  61767  1016  61827.8542
#> 59:   male       ag1a cnt_males  1015  61767  1016  61827.8542
#> 60:      m       ag1a cnt_males  1015  61767  1016  61827.8542
#> 61:   male age_group2 cnt_males   571  33706   572  33765.0298
#> 62:      m age_group2 cnt_males   571  33706   572  33765.0298
#> 63:   male       ag2a cnt_males   571  33706   572  33765.0298
#> 64:      m       ag2a cnt_males   571  33706   572  33765.0298
#> 65:   male age_group3 cnt_males   424  25075   424  25075.0000
#> 66:      m age_group3 cnt_males   424  25075   424  25075.0000
#> 67:   male age_group4 cnt_males   195  11466   193  11348.4000
#> 68:      m age_group4 cnt_males   195  11466   193  11348.4000
#> 69:   male age_group5 cnt_males    84   5217    84   5217.0000
#> 70:      m age_group5 cnt_males    84   5217    84   5217.0000
#> 71:   male age_group6 cnt_males     7    358     8    409.1429
#> 72:      m age_group6 cnt_males     7    358     8    409.1429
#> 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/Rtmpu511Tl/file1fa578e7be86.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
#>     <char>     <char> <char>    <num>      <num>      <num>
#>  1:  Total      Total income 22952978 1390097117 1390250262
#>  2:  Total age_group1 income  9810547  596138148  596091650
#>  3:  Total       ag1a income  9810547  596138148  596091650
#>  4:  Total age_group2 income  5692119  340396980  340625967
#>  5:  Total       ag2a income  5692119  340396980  340625967
#>  6:  Total age_group3 income  4406946  268062846  268052487
#>  7:  Total age_group4 income  2133543  127534152  127539046
#>  8:  Total age_group5 income   848151   54128543   54040643
#>  9:  Total age_group6 income    61672    3836448    3615818
#> 10:   male      Total income 11262049  678855172  678861447
#> 11:      m      Total income 11262049  678855172  678861447
#> 12:   male age_group1 income  4877164  299145003  299132625
#> 13:      m age_group1 income  4877164  299145003  299132625
#> 14:   male       ag1a income  4877164  299145003  299132625
#> 15:      m       ag1a income  4877164  299145003  299132625
#> 16:   male age_group2 income  2811379  165593138  165573300
#> 17:      m age_group2 income  2811379  165593138  165573300
#> 18:   male       ag2a income  2811379  165593138  165573300
#> 19:      m       ag2a income  2811379  165593138  165573300
#> 20:   male age_group3 income  2168169  129963271  129993816
#> 21:      m age_group3 income  2168169  129963271  129993816
#> 22:   male age_group4 income   978510   57528731   57511088
#> 23:      m age_group4 income   978510   57528731   57511088
#> 24:   male age_group5 income   393134   24664249   24720462
#> 25:      m age_group5 income   393134   24664249   24720462
#> 26:   male age_group6 income    33693    1960780    1812904
#> 27:      m age_group6 income    33693    1960780    1812904
#> 28: female      Total income 11690929  711241945  711086313
#> 29:      f      Total income 11690929  711241945  711086313
#> 30: female age_group1 income  4933383  296993145  297259034
#> 31:      f age_group1 income  4933383  296993145  297259034
#> 32: female       ag1a income  4933383  296993145  297259034
#> 33:      f       ag1a income  4933383  296993145  297259034
#> 34: female age_group2 income  2880740  174803842  174823395
#> 35:      f age_group2 income  2880740  174803842  174823395
#> 36: female       ag2a income  2880740  174803842  174823395
#> 37:      f       ag2a income  2880740  174803842  174823395
#> 38: female age_group3 income  2238777  138099575  138157291
#> 39:      f age_group3 income  2238777  138099575  138157291
#> 40: female age_group4 income  1155033   70005421   70083535
#> 41:      f age_group4 income  1155033   70005421   70083535
#> 42: female age_group5 income   455017   29464294   29402316
#> 43:      f age_group5 income   455017   29464294   29402316
#> 44: female age_group6 income    27979    1875668    2026783
#> 45:      f age_group6 income    27979    1875668    2026783
#>        sex        age  vname      uws         ws        pws
tab$numtab("income", mean_before_sum = FALSE)
#>        sex        age  vname      uws         ws        pws
#>     <char>     <char> <char>    <num>      <num>      <num>
#>  1:  Total      Total income 22952978 1390097117 1390173687
#>  2:  Total age_group1 income  9810547  596138148  596114898
#>  3:  Total       ag1a income  9810547  596138148  596114898
#>  4:  Total age_group2 income  5692119  340396980  340511454
#>  5:  Total       ag2a income  5692119  340396980  340511454
#>  6:  Total age_group3 income  4406946  268062846  268057666
#>  7:  Total age_group4 income  2133543  127534152  127536599
#>  8:  Total age_group5 income   848151   54128543   54084575
#>  9:  Total age_group6 income    61672    3836448    3724500
#> 10:   male      Total income 11262049  678855172  678858310
#> 11:      m      Total income 11262049  678855172  678858310
#> 12:   male age_group1 income  4877164  299145003  299138814
#> 13:      m age_group1 income  4877164  299145003  299138814
#> 14:   male       ag1a income  4877164  299145003  299138814
#> 15:      m       ag1a income  4877164  299145003  299138814
#> 16:   male age_group2 income  2811379  165593138  165583218
#> 17:      m age_group2 income  2811379  165593138  165583218
#> 18:   male       ag2a income  2811379  165593138  165583218
#> 19:      m       ag2a income  2811379  165593138  165583218
#> 20:   male age_group3 income  2168169  129963271  129978542
#> 21:      m age_group3 income  2168169  129963271  129978542
#> 22:   male age_group4 income   978510   57528731   57519909
#> 23:      m age_group4 income   978510   57528731   57519909
#> 24:   male age_group5 income   393134   24664249   24692340
#> 25:      m age_group5 income   393134   24664249   24692340
#> 26:   male age_group6 income    33693    1960780    1885393
#> 27:      m age_group6 income    33693    1960780    1885393
#> 28: female      Total income 11690929  711241945  711164125
#> 29:      f      Total income 11690929  711241945  711164125
#> 30: female age_group1 income  4933383  296993145  297126060
#> 31:      f age_group1 income  4933383  296993145  297126060
#> 32: female       ag1a income  4933383  296993145  297126060
#> 33:      f       ag1a income  4933383  296993145  297126060
#> 34: female age_group2 income  2880740  174803842  174813618
#> 35:      f age_group2 income  2880740  174803842  174813618
#> 36: female       ag2a income  2880740  174803842  174813618
#> 37:      f       ag2a income  2880740  174803842  174813618
#> 38: female age_group3 income  2238777  138099575  138128430
#> 39:      f age_group3 income  2238777  138099575  138128430
#> 40: female age_group4 income  1155033   70005421   70044467
#> 41:      f age_group4 income  1155033   70005421   70044467
#> 42: female age_group5 income   455017   29464294   29433289
#> 43:      f age_group5 income   455017   29464294   29433289
#> 44: female age_group6 income    27979    1875668    1949762
#> 45:      f age_group6 income    27979    1875668    1949762
#>        sex        age  vname      uws         ws        pws
tab$numtab("savings")
#>        sex        age   vname     uws        ws         pws
#>     <char>     <char>  <char>   <num>     <num>       <num>
#>  1:  Total      Total savings 2273532 136463855 136457078.3
#>  2:  Total age_group1 savings  982386  58998714  58998155.1
#>  3:  Total       ag1a savings  982386  58998714  58998155.1
#>  4:  Total age_group2 savings  552336  32714762  32719970.1
#>  5:  Total       ag2a savings  552336  32714762  32719970.1
#>  6:  Total age_group3 savings  437101  26504426  26504544.2
#>  7:  Total age_group4 savings  214661  12864913  12867655.7
#>  8:  Total age_group5 savings   80451   5035230   5032357.0
#>  9:  Total age_group6 savings    6597    345810    341385.4
#> 10:   male      Total savings 1159816  69053529  69054252.1
#> 11:      m      Total savings 1159816  69053529  69054252.1
#> 12:   male age_group1 savings  517660  31439332  31440103.7
#> 13:      m age_group1 savings  517660  31439332  31440103.7
#> 14:   male       ag1a savings  517660  31439332  31440103.7
#> 15:      m       ag1a savings  517660  31439332  31440103.7
#> 16:   male age_group2 savings  280923  16361874  16361381.0
#> 17:      m age_group2 savings  280923  16361874  16361381.0
#> 18:   male       ag2a savings  280923  16361874  16361381.0
#> 19:      m       ag2a savings  280923  16361874  16361381.0
#> 20:   male age_group3 savings  214970  12693671  12698958.8
#> 21:      m age_group3 savings  214970  12693671  12698958.8
#> 22:   male age_group4 savings   99420   5700034   5694413.1
#> 23:      m age_group4 savings   99420   5700034   5694413.1
#> 24:   male age_group5 savings   43233   2702016   2703720.1
#> 25:      m age_group5 savings   43233   2702016   2703720.1
#> 26:   male age_group6 savings    3610    156602    154700.8
#> 27:      m age_group6 savings    3610    156602    154700.8
#> 28: female      Total savings 1113716  67410326  67412529.5
#> 29:      f      Total savings 1113716  67410326  67412529.5
#> 30: female age_group1 savings  464726  27559382  27558856.8
#> 31:      f age_group1 savings  464726  27559382  27558856.8
#> 32: female       ag1a savings  464726  27559382  27558856.8
#> 33:      f       ag1a savings  464726  27559382  27558856.8
#> 34: female age_group2 savings  271413  16352888  16353310.7
#> 35:      f age_group2 savings  271413  16352888  16353310.7
#> 36: female       ag2a savings  271413  16352888  16353310.7
#> 37:      f       ag2a savings  271413  16352888  16353310.7
#> 38: female age_group3 savings  222131  13810755  13813523.6
#> 39:      f age_group3 savings  222131  13810755  13813523.6
#> 40: female age_group4 savings  115241   7164879   7171200.5
#> 41:      f age_group4 savings  115241   7164879   7171200.5
#> 42: female age_group5 savings   37218   2333214   2332179.0
#> 43:      f age_group5 savings   37218   2333214   2332179.0
#> 44: female age_group6 savings    2987    189208    192858.7
#> 45:      f age_group6 savings    2987    189208    192858.7
#>        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/Rtmpu511Tl/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
#>     <char>     <char>    <char> <num>  <num> <num>       <num>
#>  1:  Total      Total     total  4580 275978  4582 276098.5144
#>  2:  Total age_group1     total  1969 119009  1970 119069.4413
#>  3:  Total       ag1a     total  1969 119009  1970 119069.4413
#>  4:  Total age_group2     total  1143  67758  1143  67758.0000
#>  5:  Total       ag2a     total  1143  67758  1143  67758.0000
#>  6:  Total age_group3     total   864  52694   864  52694.0000
#>  7:  Total age_group4     total   423  25256   422  25196.2931
#>  8:  Total age_group5     total   168  10474   166  10349.3095
#>  9:  Total age_group6     total    13    787    14    847.5385
#> 10:   male      Total     total  2296 137589  2298 137708.8510
#> 11:      m      Total     total  2296 137589  2298 137708.8510
#> 12:   male age_group1     total  1015  61767  1015  61767.0000
#> 13:      m age_group1     total  1015  61767  1015  61767.0000
#> 14:   male       ag1a     total  1015  61767  1015  61767.0000
#> 15:      m       ag1a     total  1015  61767  1015  61767.0000
#> 16:   male age_group2     total   571  33706   571  33706.0000
#> 17:      m age_group2     total   571  33706   571  33706.0000
#> 18:   male       ag2a     total   571  33706   571  33706.0000
#> 19:      m       ag2a     total   571  33706   571  33706.0000
#> 20:   male age_group3     total   424  25075   424  25075.0000
#> 21:      m age_group3     total   424  25075   424  25075.0000
#> 22:   male age_group4     total   195  11466   194  11407.2000
#> 23:      m age_group4     total   195  11466   194  11407.2000
#> 24:   male age_group5     total    84   5217    84   5217.0000
#> 25:      m age_group5     total    84   5217    84   5217.0000
#> 26:   male age_group6     total     7    358     7    358.0000
#> 27:      m age_group6     total     7    358     7    358.0000
#> 28: female      Total     total  2284 138389  2282 138267.8187
#> 29:      f      Total     total  2284 138389  2282 138267.8187
#> 30: female age_group1     total   954  57242   953  57181.9979
#> 31:      f age_group1     total   954  57242   953  57181.9979
#> 32: female       ag1a     total   954  57242   953  57181.9979
#> 33:      f       ag1a     total   954  57242   953  57181.9979
#> 34: female age_group2     total   572  34052   573  34111.5315
#> 35:      f age_group2     total   572  34052   573  34111.5315
#> 36: female       ag2a     total   572  34052   573  34111.5315
#> 37:      f       ag2a     total   572  34052   573  34111.5315
#> 38: female age_group3     total   440  27619   438  27493.4591
#> 39:      f age_group3     total   440  27619   438  27493.4591
#> 40: female age_group4     total   228  13790   227  13729.5175
#> 41:      f age_group4     total   228  13790   227  13729.5175
#> 42: female age_group5     total    84   5257    84   5257.0000
#> 43:      f age_group5     total    84   5257    84   5257.0000
#> 44: female age_group6     total     6    429     5    357.5000
#> 45:      f age_group6     total     6    429     5    357.5000
#> 46:  Total      Total cnt_males  2296 137589  2298 137708.8510
#> 47:  Total age_group1 cnt_males  1015  61767  1015  61767.0000
#> 48:  Total       ag1a cnt_males  1015  61767  1015  61767.0000
#> 49:  Total age_group2 cnt_males   571  33706   571  33706.0000
#> 50:  Total       ag2a cnt_males   571  33706   571  33706.0000
#> 51:  Total age_group3 cnt_males   424  25075   424  25075.0000
#> 52:  Total age_group4 cnt_males   195  11466   194  11407.2000
#> 53:  Total age_group5 cnt_males    84   5217    84   5217.0000
#> 54:  Total age_group6 cnt_males     7    358     7    358.0000
#> 55:   male      Total cnt_males  2296 137589  2298 137708.8510
#> 56:      m      Total cnt_males  2296 137589  2298 137708.8510
#> 57:   male age_group1 cnt_males  1015  61767  1015  61767.0000
#> 58:      m age_group1 cnt_males  1015  61767  1015  61767.0000
#> 59:   male       ag1a cnt_males  1015  61767  1015  61767.0000
#> 60:      m       ag1a cnt_males  1015  61767  1015  61767.0000
#> 61:   male age_group2 cnt_males   571  33706   571  33706.0000
#> 62:      m age_group2 cnt_males   571  33706   571  33706.0000
#> 63:   male       ag2a cnt_males   571  33706   571  33706.0000
#> 64:      m       ag2a cnt_males   571  33706   571  33706.0000
#> 65:   male age_group3 cnt_males   424  25075   424  25075.0000
#> 66:      m age_group3 cnt_males   424  25075   424  25075.0000
#> 67:   male age_group4 cnt_males   195  11466   194  11407.2000
#> 68:      m age_group4 cnt_males   195  11466   194  11407.2000
#> 69:   male age_group5 cnt_males    84   5217    84   5217.0000
#> 70:      m age_group5 cnt_males    84   5217    84   5217.0000
#> 71:   male age_group6 cnt_males     7    358     7    358.0000
#> 72:      m age_group6 cnt_males     7    358     7    358.0000
#> 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
#>    <fctr> <int>      <num>
#> 1:     -2     3 0.06666667
#> 2:     -1     7 0.15555556
#> 3:      0    19 0.42222222
#> 4:      1    11 0.24444444
#> 5:      2     5 0.11111111
#> 
#> $measures
#>       what    d1    d2    d3
#>     <char> <num> <num> <num>
#>  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.756 0.010 0.026
#>  7: Median 1.000 0.001 0.016
#>  8:    Q60 1.000 0.001 0.021
#>  9:    Q70 1.000 0.002 0.021
#> 10:    Q80 1.000 0.004 0.033
#> 11:    Q90 2.000 0.005 0.048
#> 12:    Q95 2.000 0.064 0.124
#> 13:    Q99 2.000 0.167 0.213
#> 14:    Max 2.000 0.167 0.213
#> 
#> $cumdistr_d1
#>       cat   cnt       pct
#>    <char> <int>     <num>
#> 1:      0    19 0.4222222
#> 2:      1    37 0.8222222
#> 3:      2    45 1.0000000
#> 
#> $cumdistr_d2
#>            cat   cnt       pct
#>         <char> <int>     <num>
#> 1:    [0,0.02]    42 0.9333333
#> 2: (0.02,0.05]    42 0.9333333
#> 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
#>         <char> <int>     <num>
#> 1:    [0,0.02]    26 0.5777778
#> 2: (0.02,0.05]    41 0.9111111
#> 3:  (0.05,0.1]    42 0.9333333
#> 4:   (0.1,0.2]    43 0.9555556
#> 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
#>      <char>     <char>  <num> <int>     <num>    <char>
#>   1:  Total      Total     17     2 0.9843284     total
#>   2:  Total age_group1     16     1 0.8343575     total
#>   3:  Total       ag1a     16     1 0.8343575     total
#>   4:  Total age_group2     15     0 0.3992561     total
#>   5:  Total       ag2a     15     0 0.3992561     total
#>  ---                                                   
#> 131:      f age_group4     -1     0 0.0000000 cnt_males
#> 132: female age_group5     -1     0 0.0000000 cnt_males
#> 133:      f age_group5     -1     0 0.0000000 cnt_males
#> 134: female age_group6     -1     0 0.0000000 cnt_males
#> 135:      f age_group6     -1     0 0.0000000 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
#>            <char> <num> <num> <num> <num> <num>  <num>  <num> <num> <num> <num>
#> 1:          total    -2  -1.6    -1    -1     0 -0.178      0     0     0     1
#> 2: cnt_highincome    -2  -2.0    -1    -1    -1 -0.244     -1     0     1     1
#> 3:      cnt_males    -1   0.0     0     0     0  0.067      0     0     0     0
#>      Q90   Q95   Q99   Max
#>    <num> <num> <num> <num>
#> 1:     1   1.8  2.00     2
#> 2:     1   1.8  3.68     5
#> 3:     0   1.6  2.00     2
#> 
#> ── Distance-based measures ─────────────────────────────────────────────────────
#> ✔ Variable: 'total'
#> 
#>       what    d1    d2    d3
#>     <char> <num> <num> <num>
#>  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.756 0.010 0.026
#>  7: Median 1.000 0.001 0.016
#>  8:    Q60 1.000 0.001 0.021
#>  9:    Q70 1.000 0.002 0.021
#> 10:    Q80 1.000 0.004 0.033
#> 11:    Q90 2.000 0.005 0.048
#> 12:    Q95 2.000 0.064 0.124
#> 13:    Q99 2.000 0.167 0.213
#> 14:    Max 2.000 0.167 0.213
#> 
#> ✔ Variable: 'cnt_males'
#> 
#>       what    d1    d2    d3
#>     <char> <num> <num> <num>
#>  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.001 0.006
#>  7: Median 0.000 0.000 0.000
#>  8:    Q60 0.000 0.000 0.000
#>  9:    Q70 0.000 0.000 0.000
#> 10:    Q80 0.800 0.001 0.017
#> 11:    Q90 1.400 0.003 0.027
#> 12:    Q95 2.000 0.005 0.036
#> 13:    Q99 2.000 0.005 0.036
#> 14:    Max 2.000 0.005 0.036
#> 
#> ✔ Variable: 'cnt_highincome'
#> 
#>       what    d1    d2    d3
#>     <char> <num> <num> <num>
#>  1:    Min 0.000 0.000 0.000
#>  2:    Q10 1.000 0.005 0.034
#>  3:    Q20 1.000 0.010 0.051
#>  4:    Q30 1.000 0.011 0.059
#>  5:    Q40 1.000 0.015 0.065
#>  6:   Mean 1.325 0.044 0.106
#>  7: Median 1.000 0.018 0.069
#>  8:    Q60 1.000 0.020 0.087
#>  9:    Q70 1.000 0.032 0.104
#> 10:    Q80 2.000 0.055 0.132
#> 11:    Q90 2.000 0.143 0.196
#> 12:    Q95 2.000 0.154 0.410
#> 13:    Q99 3.830 0.286 0.412
#> 14:    Max 5.000 0.286 0.414
#> 
#> ┌──────────────────────────────────────────────────┐
#> │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
#>     <char>      <num>      <num>      <num>     <num>     <num>     <num>
#> 1:  expend        Inf         NA         NA        NA        NA       NaN
#> 2:  income -111948.37 -62819.345 -23249.683 -9700.107 -6189.091 13280.453
#> 3: savings   -6776.69  -2484.279   -654.092  -525.196  -493.031   599.867
#> 4:   mixed        Inf         NA         NA        NA        NA       NaN
#>      Median      Q60       Q70       Q80        Q90        Q95      Q99
#>       <num>    <num>     <num>     <num>      <num>      <num>    <num>
#> 1:       NA       NA        NA        NA         NA         NA       NA
#> 2: 3137.731 9776.134 28090.748 46055.805 114474.050 132914.919 132914.9
#> 3:  422.707  771.691  1704.068  2768.644   5208.107   5287.844   6321.5
#> 4:       NA       NA        NA        NA         NA         NA       NA
#>         Max
#>       <num>
#> 1:     -Inf
#> 2: 132914.9
#> 3:   6321.5
#> 4:     -Inf
# }