ck_simpleparams() allows to define parameters for a simple perturbation approach based on a single magnitude parameter (m). The values of epsilon are used to "weight" parameter m in case type == "top_contr" is set in ck_params_nums().

ck_simpleparams(p, epsilon = 1)

Arguments

p

a percentage value used as magnitude for perturbation

epsilon

a numeric vector in descending order with all values >= 0 and <= 1 with the first element forced to equal 1. The length of this vector must correspond with the number top_k specified in ck_params_nums() when creating parameters for type == "top_contr" which is checked at runtime. This setting allows to use different flex-functions for the largest top_k contributors.

Value

an object suitable as input for ck_params_nums().

Details

details about the flex function can be found in Deliverable D4.2, Part I in SGA "Open Source tools for perturbative confidentiality methods"

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              98          25.00000           0
#>    2:   2530      28       1              81          25.00000           1
#>    3:   6920     550       1              63          25.00000           0
#>    4:   7960     870       1              41          25.00000           0
#>    5:   9030      20       2              72          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              37          16.66667           1
#> 4577:   1420     987    1000              85          16.66667           0
#> 4578:   8900     684    1000              54          16.66667           0
#> 4579:   3880     294    1000              56          16.66667           1
#> 4580:   4830     911    1000              42          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              98          25.00000           0
#>    2:   2530      28       1              81          25.00000           1
#>    3:   6920     550       1              63          25.00000           0
#>    4:   7960     870       1              41          25.00000           0
#>    5:   9030      20       2              72          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              37          16.66667           1
#> 4577:   1420     987    1000              85          16.66667           0
#> 4578:   8900     684    1000              54          16.66667           0
#> 4579:   3880     294    1000              56          16.66667           1
#> 4580:   4830     911    1000              42          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              98          25.00000           0
#>    2:   2530      28       1              81          25.00000           1
#>    3:   6920     550       1              63          25.00000           0
#>    4:   7960     870       1              41          25.00000           0
#>    5:   9030      20       2              72          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              37          16.66667           1
#> 4577:   1420     987    1000              85          16.66667           0
#> 4578:   8900     684    1000              54          16.66667           0
#> 4579:   3880     294    1000              56          16.66667           1
#> 4580:   4830     911    1000              42          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              98          25.00000           0
#>    2:   2530      28       1              81          25.00000           1
#>    3:   6920     550       1              63          25.00000           0
#>    4:   7960     870       1              41          25.00000           0
#>    5:   9030      20       2              72          16.66667           0
#>   ---                                                                     
#> 4576:   7900     278    1000              37          16.66667           1
#> 4577:   1420     987    1000              85          16.66667           0
#> 4578:   8900     684    1000              54          16.66667           0
#> 4579:   3880     294    1000              56          16.66667           1
#> 4580:   4830     911    1000              42          16.66667           0
#>       cnt_males cnt_highincome mixed
#>    1:         1              0    -6
#>    2:         0              0    -9
#>    3:         1              0    -2
#>    4:         1              0     3
#>    5:         1              1     4
#>   ---                               
#> 4576:         0              0    -4
#> 4577:         1              0     1
#> 4578:         1              0    -7
#> 4579:         0              0     4
#> 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
#> 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/file196217d3e428.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 273505 4581 273564.7172
#>  2:  Total age_group1     total 1969 117969 1967 117849.1737
#>  3:  Total       ag1a     total 1969 117969 1967 117849.1737
#>  4:  Total age_group2     total 1143  68998 1144  69058.3657
#>  5:  Total       ag2a     total 1143  68998 1144  69058.3657
#>  6:  Total age_group3     total  864  50948  864  50948.0000
#>  7:  Total age_group4     total  423  24864  425  24981.5603
#>  8:  Total age_group5     total  168   9850  167   9791.3690
#>  9:  Total age_group6     total   13    876   12    808.6154
#> 10:   male      Total     total 2296 136764 2297 136823.5662
#> 11:      m      Total     total 2296 136764 2297 136823.5662
#> 12:   male age_group1     total 1015  61268 1015  61268.0000
#> 13:      m age_group1     total 1015  61268 1015  61268.0000
#> 14:   male       ag1a     total 1015  61268 1015  61268.0000
#> 15:      m       ag1a     total 1015  61268 1015  61268.0000
#> 16:   male age_group2     total  571  33999  569  33879.9142
#> 17:      m age_group2     total  571  33999  569  33879.9142
#> 18:   male       ag2a     total  571  33999  569  33879.9142
#> 19:      m       ag2a     total  571  33999  569  33879.9142
#> 20:   male age_group3     total  424  24570  424  24570.0000
#> 21:      m age_group3     total  424  24570  424  24570.0000
#> 22:   male age_group4     total  195  11527  196  11586.1128
#> 23:      m age_group4     total  195  11527  196  11586.1128
#> 24:   male age_group5     total   84   4898   85   4956.3095
#> 25:      m age_group5     total   84   4898   85   4956.3095
#> 26:   male age_group6     total    7    502    7    502.0000
#> 27:      m age_group6     total    7    502    7    502.0000
#> 28: female      Total     total 2284 136741 2285 136800.8691
#> 29:      f      Total     total 2284 136741 2285 136800.8691
#> 30: female age_group1     total  954  56701  954  56701.0000
#> 31:      f age_group1     total  954  56701  954  56701.0000
#> 32: female       ag1a     total  954  56701  954  56701.0000
#> 33:      f       ag1a     total  954  56701  954  56701.0000
#> 34: female age_group2     total  572  34999  573  35060.1871
#> 35:      f age_group2     total  572  34999  573  35060.1871
#> 36: female       ag2a     total  572  34999  573  35060.1871
#> 37:      f       ag2a     total  572  34999  573  35060.1871
#> 38: female age_group3     total  440  26378  441  26437.9500
#> 39:      f age_group3     total  440  26378  441  26437.9500
#> 40: female age_group4     total  228  13337  227  13278.5044
#> 41:      f age_group4     total  228  13337  227  13278.5044
#> 42: female age_group5     total   84   4952   84   4952.0000
#> 43:      f age_group5     total   84   4952   84   4952.0000
#> 44: female age_group6     total    6    374    7    436.3333
#> 45:      f age_group6     total    6    374    7    436.3333
#> 46:  Total      Total cnt_males 2296 136764 2298 136883.1324
#> 47:  Total age_group1 cnt_males 1015  61268 1015  61268.0000
#> 48:  Total       ag1a cnt_males 1015  61268 1015  61268.0000
#> 49:  Total age_group2 cnt_males  571  33999  568  33820.3713
#> 50:  Total       ag2a cnt_males  571  33999  568  33820.3713
#> 51:  Total age_group3 cnt_males  424  24570  424  24570.0000
#> 52:  Total age_group4 cnt_males  195  11527  196  11586.1128
#> 53:  Total age_group5 cnt_males   84   4898   85   4956.3095
#> 54:  Total age_group6 cnt_males    7    502    6    430.2857
#> 55:   male      Total cnt_males 2296 136764 2298 136883.1324
#> 56:      m      Total cnt_males 2296 136764 2298 136883.1324
#> 57:   male age_group1 cnt_males 1015  61268 1015  61268.0000
#> 58:      m age_group1 cnt_males 1015  61268 1015  61268.0000
#> 59:   male       ag1a cnt_males 1015  61268 1015  61268.0000
#> 60:      m       ag1a cnt_males 1015  61268 1015  61268.0000
#> 61:   male age_group2 cnt_males  571  33999  568  33820.3713
#> 62:      m age_group2 cnt_males  571  33999  568  33820.3713
#> 63:   male       ag2a cnt_males  571  33999  568  33820.3713
#> 64:      m       ag2a cnt_males  571  33999  568  33820.3713
#> 65:   male age_group3 cnt_males  424  24570  424  24570.0000
#> 66:      m age_group3 cnt_males  424  24570  424  24570.0000
#> 67:   male age_group4 cnt_males  195  11527  196  11586.1128
#> 68:      m age_group4 cnt_males  195  11527  196  11586.1128
#> 69:   male age_group5 cnt_males   84   4898   85   4956.3095
#> 70:      m age_group5 cnt_males   84   4898   85   4956.3095
#> 71:   male age_group6 cnt_males    7    502    6    430.2857
#> 72:      m age_group6 cnt_males    7    502    6    430.2857
#> 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/file1962414d7be6.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 1376150557 1376242920
#>  2:  Total age_group1 income  9810547  590208265  590185158
#>  3:  Total       ag1a income  9810547  590208265  590185158
#>  4:  Total age_group2 income  5692119  342433757  342551094
#>  5:  Total       ag2a income  5692119  342433757  342551094
#>  6:  Total age_group3 income  4406946  262986075  262950103
#>  7:  Total age_group4 income  2133543  126216802  126166294
#>  8:  Total age_group5 income   848151   49829161   49811007
#>  9:  Total age_group6 income    61672    4476497    4651161
#> 10:   male      Total income 11262049  668630275  668667904
#> 11:      m      Total income 11262049  668630275  668667904
#> 12:   male age_group1 income  4877164  292153111  292131833
#> 13:      m age_group1 income  4877164  292153111  292131833
#> 14:   male       ag1a income  4877164  292153111  292131833
#> 15:      m       ag1a income  4877164  292153111  292131833
#> 16:   male age_group2 income  2811379  165760947  165684204
#> 17:      m age_group2 income  2811379  165760947  165684204
#> 18:   male       ag2a income  2811379  165760947  165684204
#> 19:      m       ag2a income  2811379  165760947  165684204
#> 20:   male age_group3 income  2168169  128544085  128576626
#> 21:      m age_group3 income  2168169  128544085  128576626
#> 22:   male age_group4 income   978510   57395662   57470404
#> 23:      m age_group4 income   978510   57395662   57470404
#> 24:   male age_group5 income   393134   22259658   22188230
#> 25:      m age_group5 income   393134   22259658   22188230
#> 26:   male age_group6 income    33693    2516812    2455365
#> 27:      m age_group6 income    33693    2516812    2455365
#> 28: female      Total income 11690929  707520282  707480814
#> 29:      f      Total income 11690929  707520282  707480814
#> 30: female age_group1 income  4933383  298055154  298024950
#> 31:      f age_group1 income  4933383  298055154  298024950
#> 32: female       ag1a income  4933383  298055154  298024950
#> 33:      f       ag1a income  4933383  298055154  298024950
#> 34: female age_group2 income  2880740  176672810  176699389
#> 35:      f age_group2 income  2880740  176672810  176699389
#> 36: female       ag2a income  2880740  176672810  176699389
#> 37:      f       ag2a income  2880740  176672810  176699389
#> 38: female age_group3 income  2238777  134441990  134488283
#> 39:      f age_group3 income  2238777  134441990  134488283
#> 40: female age_group4 income  1155033   68821140   68565506
#> 41:      f age_group4 income  1155033   68821140   68565506
#> 42: female age_group5 income   455017   27569503   27484765
#> 43:      f age_group5 income   455017   27569503   27484765
#> 44: female age_group6 income    27979    1959685    1777965
#> 45:      f age_group6 income    27979    1959685    1777965
#>        sex        age  vname      uws         ws        pws
tab$numtab("income", mean_before_sum = FALSE)
#>        sex        age  vname      uws         ws        pws
#>  1:  Total      Total income 22952978 1376150557 1376196738
#>  2:  Total age_group1 income  9810547  590208265  590196711
#>  3:  Total       ag1a income  9810547  590208265  590196711
#>  4:  Total age_group2 income  5692119  342433757  342492420
#>  5:  Total       ag2a income  5692119  342433757  342492420
#>  6:  Total age_group3 income  4406946  262986075  262968088
#>  7:  Total age_group4 income  2133543  126216802  126191546
#>  8:  Total age_group5 income   848151   49829161   49820083
#>  9:  Total age_group6 income    61672    4476497    4562993
#> 10:   male      Total income 11262049  668630275  668649089
#> 11:      m      Total income 11262049  668630275  668649089
#> 12:   male age_group1 income  4877164  292153111  292142472
#> 13:      m age_group1 income  4877164  292153111  292142472
#> 14:   male       ag1a income  4877164  292153111  292142472
#> 15:      m       ag1a income  4877164  292153111  292142472
#> 16:   male age_group2 income  2811379  165760947  165722571
#> 17:      m age_group2 income  2811379  165760947  165722571
#> 18:   male       ag2a income  2811379  165760947  165722571
#> 19:      m       ag2a income  2811379  165760947  165722571
#> 20:   male age_group3 income  2168169  128544085  128560355
#> 21:      m age_group3 income  2168169  128544085  128560355
#> 22:   male age_group4 income   978510   57395662   57433021
#> 23:      m age_group4 income   978510   57395662   57433021
#> 24:   male age_group5 income   393134   22259658   22223916
#> 25:      m age_group5 income   393134   22259658   22223916
#> 26:   male age_group6 income    33693    2516812    2485899
#> 27:      m age_group6 income    33693    2516812    2485899
#> 28: female      Total income 11690929  707520282  707500548
#> 29:      f      Total income 11690929  707520282  707500548
#> 30: female age_group1 income  4933383  298055154  298040052
#> 31:      f age_group1 income  4933383  298055154  298040052
#> 32: female       ag1a income  4933383  298055154  298040052
#> 33:      f       ag1a income  4933383  298055154  298040052
#> 34: female age_group2 income  2880740  176672810  176686099
#> 35:      f age_group2 income  2880740  176672810  176686099
#> 36: female       ag2a income  2880740  176672810  176686099
#> 37:      f       ag2a income  2880740  176672810  176686099
#> 38: female age_group3 income  2238777  134441990  134465134
#> 39:      f age_group3 income  2238777  134441990  134465134
#> 40: female age_group4 income  1155033   68821140   68693204
#> 41:      f age_group4 income  1155033   68821140   68693204
#> 42: female age_group5 income   455017   27569503   27527102
#> 43:      f age_group5 income   455017   27569503   27527102
#> 44: female age_group6 income    27979    1959685    1866615
#> 45:      f age_group6 income    27979    1959685    1866615
#>        sex        age  vname      uws         ws        pws
tab$numtab("savings")
#>        sex        age   vname     uws        ws         pws
#>  1:  Total      Total savings 2273532 136381426 136374727.2
#>  2:  Total age_group1 savings  982386  58889580  58890578.3
#>  3:  Total       ag1a savings  982386  58889580  58890578.3
#>  4:  Total age_group2 savings  552336  33796683  33799835.1
#>  5:  Total       ag2a savings  552336  33796683  33799835.1
#>  6:  Total age_group3 savings  437101  25613956  25615925.7
#>  7:  Total age_group4 savings  214661  12713393  12713205.4
#>  8:  Total age_group5 savings   80451   4912802   4912106.3
#>  9:  Total age_group6 savings    6597    455012    456652.3
#> 10:   male      Total savings 1159816  69639926  69645435.5
#> 11:      m      Total savings 1159816  69639926  69645435.5
#> 12:   male age_group1 savings  517660  31204169  31200754.4
#> 13:      m age_group1 savings  517660  31204169  31200754.4
#> 14:   male       ag1a savings  517660  31204169  31200754.4
#> 15:      m       ag1a savings  517660  31204169  31200754.4
#> 16:   male age_group2 savings  280923  16980971  16977282.5
#> 17:      m age_group2 savings  280923  16980971  16977282.5
#> 18:   male       ag2a savings  280923  16980971  16977282.5
#> 19:      m       ag2a savings  280923  16980971  16977282.5
#> 20:   male age_group3 savings  214970  12487659  12488329.7
#> 21:      m age_group3 savings  214970  12487659  12488329.7
#> 22:   male age_group4 savings   99420   6089942   6096088.5
#> 23:      m age_group4 savings   99420   6089942   6096088.5
#> 24:   male age_group5 savings   43233   2627925   2624505.7
#> 25:      m age_group5 savings   43233   2627925   2624505.7
#> 26:   male age_group6 savings    3610    249260    248972.2
#> 27:      m age_group6 savings    3610    249260    248972.2
#> 28: female      Total savings 1113716  66741500  66741003.1
#> 29:      f      Total savings 1113716  66741500  66741003.1
#> 30: female age_group1 savings  464726  27685411  27684738.5
#> 31:      f age_group1 savings  464726  27685411  27684738.5
#> 32: female       ag1a savings  464726  27685411  27684738.5
#> 33:      f       ag1a savings  464726  27685411  27684738.5
#> 34: female age_group2 savings  271413  16815712  16816947.2
#> 35:      f age_group2 savings  271413  16815712  16816947.2
#> 36: female       ag2a savings  271413  16815712  16816947.2
#> 37:      f       ag2a savings  271413  16815712  16816947.2
#> 38: female age_group3 savings  222131  13126297  13133464.2
#> 39:      f age_group3 savings  222131  13126297  13133464.2
#> 40: female age_group4 savings  115241   6623451   6616999.6
#> 41:      f age_group4 savings  115241   6623451   6616999.6
#> 42: female age_group5 savings   37218   2284877   2283192.3
#> 43:      f age_group5 savings   37218   2284877   2283192.3
#> 44: female age_group6 savings    2987    205752    200165.8
#> 45:      f age_group6 savings    2987    205752    200165.8
#>        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 273505 4581 273564.7172
#>  2:  Total age_group1     total 1969 117969 1967 117849.1737
#>  3:  Total       ag1a     total 1969 117969 1967 117849.1737
#>  4:  Total age_group2     total 1143  68998 1144  69058.3657
#>  5:  Total       ag2a     total 1143  68998 1144  69058.3657
#>  6:  Total age_group3     total  864  50948  864  50948.0000
#>  7:  Total age_group4     total  423  24864  425  24981.5603
#>  8:  Total age_group5     total  168   9850  167   9791.3690
#>  9:  Total age_group6     total   13    876   12    808.6154
#> 10:   male      Total     total 2296 136764 2297 136823.5662
#> 11:      m      Total     total 2296 136764 2297 136823.5662
#> 12:   male age_group1     total 1015  61268 1015  61268.0000
#> 13:      m age_group1     total 1015  61268 1015  61268.0000
#> 14:   male       ag1a     total 1015  61268 1015  61268.0000
#> 15:      m       ag1a     total 1015  61268 1015  61268.0000
#> 16:   male age_group2     total  571  33999  569  33879.9142
#> 17:      m age_group2     total  571  33999  569  33879.9142
#> 18:   male       ag2a     total  571  33999  569  33879.9142
#> 19:      m       ag2a     total  571  33999  569  33879.9142
#> 20:   male age_group3     total  424  24570  424  24570.0000
#> 21:      m age_group3     total  424  24570  424  24570.0000
#> 22:   male age_group4     total  195  11527  196  11586.1128
#> 23:      m age_group4     total  195  11527  196  11586.1128
#> 24:   male age_group5     total   84   4898   85   4956.3095
#> 25:      m age_group5     total   84   4898   85   4956.3095
#> 26:   male age_group6     total    7    502    7    502.0000
#> 27:      m age_group6     total    7    502    7    502.0000
#> 28: female      Total     total 2284 136741 2285 136800.8691
#> 29:      f      Total     total 2284 136741 2285 136800.8691
#> 30: female age_group1     total  954  56701  954  56701.0000
#> 31:      f age_group1     total  954  56701  954  56701.0000
#> 32: female       ag1a     total  954  56701  954  56701.0000
#> 33:      f       ag1a     total  954  56701  954  56701.0000
#> 34: female age_group2     total  572  34999  573  35060.1871
#> 35:      f age_group2     total  572  34999  573  35060.1871
#> 36: female       ag2a     total  572  34999  573  35060.1871
#> 37:      f       ag2a     total  572  34999  573  35060.1871
#> 38: female age_group3     total  440  26378  441  26437.9500
#> 39:      f age_group3     total  440  26378  441  26437.9500
#> 40: female age_group4     total  228  13337  227  13278.5044
#> 41:      f age_group4     total  228  13337  227  13278.5044
#> 42: female age_group5     total   84   4952   84   4952.0000
#> 43:      f age_group5     total   84   4952   84   4952.0000
#> 44: female age_group6     total    6    374    7    436.3333
#> 45:      f age_group6     total    6    374    7    436.3333
#> 46:  Total      Total cnt_males 2296 136764 2297 136823.5662
#> 47:  Total age_group1 cnt_males 1015  61268 1015  61268.0000
#> 48:  Total       ag1a cnt_males 1015  61268 1015  61268.0000
#> 49:  Total age_group2 cnt_males  571  33999  569  33879.9142
#> 50:  Total       ag2a cnt_males  571  33999  569  33879.9142
#> 51:  Total age_group3 cnt_males  424  24570  424  24570.0000
#> 52:  Total age_group4 cnt_males  195  11527  196  11586.1128
#> 53:  Total age_group5 cnt_males   84   4898   85   4956.3095
#> 54:  Total age_group6 cnt_males    7    502    7    502.0000
#> 55:   male      Total cnt_males 2296 136764 2297 136823.5662
#> 56:      m      Total cnt_males 2296 136764 2297 136823.5662
#> 57:   male age_group1 cnt_males 1015  61268 1015  61268.0000
#> 58:      m age_group1 cnt_males 1015  61268 1015  61268.0000
#> 59:   male       ag1a cnt_males 1015  61268 1015  61268.0000
#> 60:      m       ag1a cnt_males 1015  61268 1015  61268.0000
#> 61:   male age_group2 cnt_males  571  33999  569  33879.9142
#> 62:      m age_group2 cnt_males  571  33999  569  33879.9142
#> 63:   male       ag2a cnt_males  571  33999  569  33879.9142
#> 64:      m       ag2a cnt_males  571  33999  569  33879.9142
#> 65:   male age_group3 cnt_males  424  24570  424  24570.0000
#> 66:      m age_group3 cnt_males  424  24570  424  24570.0000
#> 67:   male age_group4 cnt_males  195  11527  196  11586.1128
#> 68:      m age_group4 cnt_males  195  11527  196  11586.1128
#> 69:   male age_group5 cnt_males   84   4898   85   4956.3095
#> 70:      m age_group5 cnt_males   84   4898   85   4956.3095
#> 71:   male age_group6 cnt_males    7    502    7    502.0000
#> 72:      m age_group6 cnt_males    7    502    7    502.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
#> 1:    -2   1 0.02222222
#> 2:    -1  19 0.42222222
#> 3:     0  15 0.33333333
#> 4:     1   4 0.08888889
#> 5:     2   6 0.13333333
#> 
#> $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 1.000 0.000 0.010
#>  6:   Mean 0.822 0.011 0.029
#>  7: Median 1.000 0.001 0.021
#>  8:    Q60 1.000 0.002 0.023
#>  9:    Q70 1.000 0.004 0.033
#> 10:    Q80 1.000 0.004 0.042
#> 11:    Q90 2.000 0.010 0.052
#> 12:    Q95 2.000 0.064 0.124
#> 13:    Q99 2.000 0.167 0.196
#> 14:    Max 2.000 0.167 0.196
#> 
#> $cumdistr_d1
#>    cat cnt       pct
#> 1:   0  15 0.3333333
#> 2:   1  38 0.8444444
#> 3:   2  45 1.0000000
#> 
#> $cumdistr_d2
#>            cat cnt       pct
#> 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
#> 1:    [0,0.02]  22 0.4888889
#> 2: (0.02,0.05]  40 0.8888889
#> 3:  (0.05,0.1]  42 0.9333333
#> 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.80259445     total
#>   2:  Total age_group1     13   -2 0.04354876     total
#>   3:  Total       ag1a     13   -2 0.04354876     total
#>   4:  Total age_group2     16    1 0.89716165     total
#>   5:  Total       ag2a     16    1 0.89716165     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  -2  -1   0   0  0.111      0 1.0   1 1.0   1   1 1.56
#> 2: cnt_highincome  -4  -1  -1   0   0  0.267      0 0.4   1 2.0   2   2 2.00
#> 3:      cnt_males  -2  -2   0   0   0 -0.067      0 0.0   0 0.2   1   1 1.00
#>    Max
#> 1:   2
#> 2:   2
#> 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 1.000 0.000 0.010
#>  6:   Mean 0.822 0.011 0.029
#>  7: Median 1.000 0.001 0.021
#>  8:    Q60 1.000 0.002 0.023
#>  9:    Q70 1.000 0.004 0.033
#> 10:    Q80 1.000 0.004 0.042
#> 11:    Q90 2.000 0.010 0.052
#> 12:    Q95 2.000 0.064 0.124
#> 13:    Q99 2.000 0.167 0.196
#> 14:    Max 2.000 0.167 0.196
#> 
#> ✔ 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.778 0.003 0.020
#>  7: Median 1.000 0.000 0.010
#>  8:    Q60 1.000 0.004 0.036
#>  9:    Q70 1.000 0.004 0.042
#> 10:    Q80 1.800 0.005 0.042
#> 11:    Q90 2.000 0.008 0.047
#> 12:    Q95 2.000 0.012 0.054
#> 13:    Q99 2.000 0.012 0.054
#> 14:    Max 2.000 0.012 0.054
#> 
#> ✔ Variable: 'cnt_highincome'
#> 
#>       what   d1    d2    d3
#>  1:    Min 0.00 0.000 0.000
#>  2:    Q10 0.00 0.000 0.000
#>  3:    Q20 0.00 0.000 0.000
#>  4:    Q30 1.00 0.004 0.030
#>  5:    Q40 1.00 0.010 0.050
#>  6:   Mean 1.15 0.044 0.090
#>  7: Median 1.00 0.013 0.057
#>  8:    Q60 1.00 0.016 0.076
#>  9:    Q70 2.00 0.020 0.108
#> 10:    Q80 2.00 0.051 0.136
#> 11:    Q90 2.00 0.093 0.188
#> 12:    Q95 2.10 0.286 0.354
#> 13:    Q99 4.00 0.286 0.354
#> 14:    Max 4.00 0.286 0.354
#> 
#> ┌──────────────────────────────────────────────────┐
#> │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 -127935.666 -42401.48 -38375.870 -29781.810 -16256.024 -11936.832
#> 3: savings   -6698.786  -4827.16  -3473.131  -3414.584   -681.791   -415.505
#> 4:   mixed         Inf        NA         NA         NA         NA        NaN
#>        Median        Q60       Q70      Q80       Q90       Q95       Q99
#> 1:         NA         NA        NA       NA        NA        NA        NA
#> 2: -11553.524 -10639.392 13288.908 18814.41 37359.024 56166.789 74249.896
#> 3:   -496.907    155.712  1187.815  1706.22  5509.486  6146.524  7167.235
#> 4:         NA         NA        NA       NA        NA        NA        NA
#>          Max
#> 1:      -Inf
#> 2: 86496.466
#> 3:  7167.235
#> 4:      -Inf
# }