To be used on categorical data stored as factors. The algorithm randomly changes the values of variables in selected records (usually the risky ones) according to an invariant probability transition matrix or a custom-defined transition matrix.
pram(obj, variables = NULL, strata_variables = NULL, pd = 0.8, alpha = 0.5)Input data. Allowed input data are objects of class
data.frame, factor or sdcMicroObj.
Names of variables in obj on which post-randomization
should be applied. If obj is a factor, this argument is ignored. Please note that
pram can only be applied to factor-variables.
names of variables for stratification (will be set automatically for an object of class sdcMicroObj. One can also specify an integer vector or factor that specifies that desired groups. This vector must match the dimension of the input data set, however. For a possible use case, have a look at the examples.
lower bound for the diagonal entries of the initial transition matrix P
from which the invariant transition matrix is derived (see Details). Note that pd is
not a lower bound for the diagonal of the matrix that is finally applied to the data:
the diagonal entries of the applied matrix can be smaller than pd, the more so the
larger alpha is. Either a vector of length 1 (which is recycled) or a vector of the
same length as the number of variables that should be postrandomized. It is also possible
to set pd to a numeric matrix. This matrix will be used directly as the transition
matrix, which is the way to go if specific (e.g. guaranteed minimal) diagonal entries
are required. The matrix must be constructed as follows:
the matrix must be a square matrix
the rownames and colnames of the matrix must match the levels (in the same order) of the factor-variable that should be postrandomized.
the rowSums of the matrix need to equal 1 (up to numerical tolerance); the column sums are not restricted
It is also possible to combine the different ways. For details have a look at the examples.
amount of perturbation for the invariant Pram method, a number between
0 and 1. With alpha = 0 the data are not changed at all, with alpha = 1 the
invariant transition matrix R is applied in full (see Details). This is a numeric vector
of length 1 (that will be recycled if necessary) or a vector of the same length as the number
of variables. If one specified as transition matrix directly, alpha is ignored.
a modified sdcMicroObj object or a new object containing original and post-randomized variables (with suffix "_pram").
If pd is a number, the transition matrix is generated in three steps, separately for
each variable and each stratum:
An initial transition matrix P is drawn. Its diagonal entries are sampled
uniformly from the interval [pd, 1]; the remaining probability mass of each row is
spread evenly over the other categories. Only for this matrix pd is a lower bound of
the diagonal.
From P and the observed relative frequencies p of the categories, the invariant
matrix R = P Q is computed, where Q is the estimated backward transition matrix
with entries Q[k, j] = P[j, k] p[j] / sum_i(p[i] P[i, k]).
The matrix that is applied to the data is Rs = alpha * R + (1 - alpha) * I with
I being the identity matrix.
Since R is a product of two transition matrices, its diagonal entries are in general
smaller than those of P. The diagonal of Rs therefore can be below pd, in
particular for large alpha. This is intended; if a lower bound for the diagonal of
the applied matrix is needed, a transition matrix should be passed to pd directly.
The rows of Rs sum up to 1 (row i holds the probabilities that category i is
changed into each of the categories), the columns in general do not. Invariance
means that the observed distribution is preserved in expectation, p' Rs = p'; it does
not mean that the column sums equal 1.
The matrix that was used (for the last stratum, if strata are present) is available
in the output, see attr(res, "pram_params") in the examples.
Deprecated method 'pram_strata' is no longer available in sdcMicro > 4.5.0
data(testdata)
# \donttest{
## donttest is necessary because of
## Examples with CPU time > 2.5 times elapsed time
## caused by using C++ code and/or data.table
## using a factor variable as input
res <- pram(as.factor(testdata$roof))
print(res)
#> Number of changed observations:
#> - - - - - - - - - - -
#> x != x_pram : 301 (6.57%)
summary(res)
#> Variable: x
#>
#> ----------------------
#>
#> Frequencies in original and perturbed data:
#> x 2 4 5 6 9 NA
#> <char> <char> <char> <char> <char> <char> <char>
#> 1: Original Frequencies 814 3697 19 34 16 0
#> 2: Frequencies after Perturbation 815 3696 23 33 13 0
#>
#> Transitions:
#> transition Frequency
#> <char> <int>
#> 1: 1 --> 1 685
#> 2: 1 --> 2 123
#> 3: 1 --> 3 1
#> 4: 1 --> 4 3
#> 5: 1 --> 5 2
#> 6: 2 --> 1 124
#> 7: 2 --> 2 3552
#> 8: 2 --> 3 7
#> 9: 2 --> 4 11
#> 10: 2 --> 5 3
#> 11: 3 --> 2 4
#> 12: 3 --> 3 15
#> 13: 4 --> 1 5
#> 14: 4 --> 2 10
#> 15: 4 --> 4 19
#> 16: 5 --> 1 1
#> 17: 5 --> 2 7
#> 18: 5 --> 5 8
#>
## using a data.frame as input
## pram can only be applied to factors
## -- > we have to recode to factors beforehand
testdata$roof <- factor(testdata$roof)
testdata$walls <- factor(testdata$walls)
testdata$water <- factor(testdata$water)
## pram() is applied within subgroups defined by
## variables "urbrur" and "sex"
res <- pram(
obj = testdata,
variables = "roof",
strata_variables = c("urbrur", "sex"))
print(res)
#> Number of changed observations:
#> - - - - - - - - - - -
#> roof != roof_pram : 140 (3.06%)
summary(res)
#> Variable: roof
#>
#> ----------------------
#>
#> Frequencies in original and perturbed data:
#> roof 2 4 5 6 9 NA
#> <char> <char> <char> <char> <char> <char> <char>
#> 1: Original Frequencies 814 3697 19 34 16 0
#> 2: Frequencies after Perturbation 807 3703 24 32 14 0
#>
#> Transitions:
#> transition Frequency
#> <char> <int>
#> 1: 1 --> 1 761
#> 2: 1 --> 2 49
#> 3: 1 --> 3 2
#> 4: 1 --> 4 1
#> 5: 1 --> 5 1
#> 6: 2 --> 1 44
#> 7: 2 --> 2 3633
#> 8: 2 --> 3 7
#> 9: 2 --> 4 10
#> 10: 2 --> 5 3
#> 11: 3 --> 2 4
#> 12: 3 --> 3 15
#> 13: 4 --> 1 1
#> 14: 4 --> 2 12
#> 15: 4 --> 4 21
#> 16: 5 --> 1 1
#> 17: 5 --> 2 5
#> 18: 5 --> 5 10
#>
## default parameters (pd = 0.8 and alpha = 0.5) for the generation
## of the invariant transition matrix will be used for all variables
res1 <- pram(
obj = testdata,
variables = c("roof", "walls", "water"))
print(res1)
#> Number of changed observations:
#> - - - - - - - - - - -
#> roof != roof_pram : 219 (4.78%)
#> walls != walls_pram : 385 (8.41%)
#> water != water_pram : 413 (9.02%)
## specific parameter settings for each variable
res2 <- pram(
obj = testdata,
variables = c("roof", "walls", "water"),
pd = c(0.95, 0.8, 0.9),
alpha = 0.5)
print(res2)
#> Number of changed observations:
#> - - - - - - - - - - -
#> roof != roof_pram : 49 (1.07%)
#> walls != walls_pram : 336 (7.34%)
#> water != water_pram : 166 (3.62%)
## detailed information on pram-parameters (such as the transition matrix 'Rs')
## is stored in the output, eg. for variable 'roof'
#attr(res2, "pram_params")$roof
## we can also specify a custom transition-matrix directly
mat <- diag(length(levels(testdata$roof)))
rownames(mat) <- colnames(mat) <- levels(testdata$roof)
res3 <- pram(
obj = testdata,
variables = "roof",
pd = mat)
print(res3) # of course, nothing has changed!
#> Number of changed observations:
#> - - - - - - - - - - -
#> roof != roof_pram : 0 (0%)
## it is possible use a transition matrix for a variable and use the 'traditional' way
## of specifying a number for the minimal diagonal entries of the transision matrix
## for other variables. In this case we must supply `pd` as list.
res4 <- pram(
obj = testdata,
variables = c("roof", "walls"),
pd = list(mat, 0.5),
alpha = c(NA, 0.5))
print(res4)
#> Number of changed observations:
#> - - - - - - - - - - -
#> roof != roof_pram : 0 (0%)
#> walls != walls_pram : 575 (12.55%)
summary(res4)
#> Variable: roof
#>
#> ----------------------
#>
#> Frequencies in original and perturbed data:
#> roof 2 4 5 6 9 NA
#> <char> <char> <char> <char> <char> <char> <char>
#> 1: Original Frequencies 814 3697 19 34 16 0
#> 2: Frequencies after Perturbation 814 3697 19 34 16 0
#>
#> Transitions:
#> transition Frequency
#> <char> <int>
#> 1: 1 --> 1 814
#> 2: 2 --> 2 3697
#> 3: 3 --> 3 19
#> 4: 4 --> 4 34
#> 5: 5 --> 5 16
#>
#> Variable: walls
#>
#> ----------------------
#>
#> Frequencies in original and perturbed data:
#> walls 2 3 9 NA
#> <char> <char> <char> <char> <char>
#> 1: Original Frequencies 1203 3327 50 0
#> 2: Frequencies after Perturbation 1167 3363 50 0
#>
#> Transitions:
#> transition Frequency
#> <char> <int>
#> 1: 1 --> 1 914
#> 2: 1 --> 2 280
#> 3: 1 --> 3 9
#> 4: 2 --> 1 247
#> 5: 2 --> 2 3065
#> 6: 2 --> 3 15
#> 7: 3 --> 1 6
#> 8: 3 --> 2 18
#> 9: 3 --> 3 26
#>
attr(res4, "pram_params")
#> $roof
#> $roof$Rs
#> 2 4 5 6 9
#> 2 1 0 0 0 0
#> 4 0 1 0 0 0
#> 5 0 0 1 0 0
#> 6 0 0 0 1 0
#> 9 0 0 0 0 1
#>
#> $roof$pd
#> 2 4 5 6 9
#> 2 1 0 0 0 0
#> 4 0 1 0 0 0
#> 5 0 0 1 0 0
#> 6 0 0 0 1 0
#> 9 0 0 0 0 1
#>
#> $roof$alpha
#> [1] NA
#>
#>
#> $walls
#> $walls$Rs
#> 2 3 9
#> 2 0.76599118 0.2250849 0.008923916
#> 3 0.08138778 0.9146009 0.004011367
#> 9 0.21470941 0.2669164 0.518374210
#>
#> $walls$pd
#> [1] 0.5
#>
#> $walls$alpha
#> [1] 0.5
#>
#>
## application to objects of class sdcMicro with default parameters
data(testdata2)
testdata2$urbrur <- factor(testdata2$urbrur)
sdc <- createSdcObj(
dat = testdata2,
keyVars = c("roof", "walls", "water", "electcon", "relat", "sex"),
numVars = c("expend", "income", "savings"),
w = "sampling_weight")
sdc <- pram(
obj = sdc,
variables = "urbrur")
print(sdc, type = "pram")
#> Post-Randomization (PRAM):
#> Variable:urbrur
#> --> final Transition-Matrix:
#> 1 2
#> 1 0.8691313 0.1308687
#> 2 0.0535372 0.9464628
#>
#> Changed observations:
#> variable nrChanges percChanges
#> 1 urbrur 6 6.45
#> ----------------------------------------------------------------------
#>
## this is equal to the previous application. If argument 'variables' is NULL,
## all variables from slot 'pramVars' will be used if possible.
sdc <- createSdcObj(
dat = testdata2,
keyVars = c("roof", "walls", "water", "electcon", "relat", "sex"),
numVars = c("expend", "income", "savings"),
w = "sampling_weight",
pramVars = "urbrur")
sdc <- pram(sdc)
print(sdc, type="pram")
#> Post-Randomization (PRAM):
#> Variable:urbrur
#> --> final Transition-Matrix:
#> 1 2
#> 1 0.8694249 0.1305751
#> 2 0.0534171 0.9465829
#>
#> Changed observations:
#> variable nrChanges percChanges
#> 1 urbrur 11 11.83
#> ----------------------------------------------------------------------
#>
## we can specify transition matrices for sdcMicroObj-objects too
testdata2$roof <- factor(testdata2$roof)
sdc <- createSdcObj(
dat = testdata2,
keyVars = c("roof", "walls", "water", "electcon", "relat", "sex"),
numVars = c("expend", "income", "savings"),
w = "sampling_weight")
mat <- diag(length(levels(testdata2$roof)))
rownames(mat) <- colnames(mat) <- levels(testdata2$roof)
mat[1,] <- c(0.9, 0, 0, 0.05, 0.05)
sdc <- pram(
obj = sdc,
variables = "roof",
pd = mat)
#> Warning: If pram is applied on key variables, the k-anonymity and risk assessment are not useful anymore.
print(sdc, type = "pram")
#> Post-Randomization (PRAM):
#> Variable:roof
#> --> final Transition-Matrix:
#> 2 4 5 6 9
#> 2 0.9 0 0 0.05 0.05
#> 4 0.0 1 0 0.00 0.00
#> 5 0.0 0 1 0.00 0.00
#> 6 0.0 0 0 1.00 0.00
#> 9 0.0 0 0 0.00 1.00
#>
#> Changed observations:
#> variable nrChanges percChanges
#> 1 roof 2 2.15
#> ----------------------------------------------------------------------
#>
## we can also have a look at the transitions
get.sdcMicroObj(sdc, "pram")$transitions
#> $roof
#> transition Frequency
#> <char> <int>
#> 1: 1 --> 1 25
#> 2: 1 --> 5 2
#> 3: 2 --> 2 46
#> 4: 3 --> 3 6
#> 5: 4 --> 4 8
#> 6: 5 --> 5 6
#>
# }