via retention-replacement Perturbation: each attribute we replace with probability 1-p with a randomly sample from the set of possible values.
see e.g. https://arxiv.org/pdf/1504.05353.pdf §2.3
str <- "No-deal is a disaster. The government must tell us the truth about Brexit"
pramString <- function(str, p) paste0('p = ', formatC(p, digits = 2, width = 4), ' : ', paste0(sapply(strsplit(str, '')[[1]], function(ch) ifelse(rbinom(1, 1, p), ch, sample(letters, 1))), collapse=''))
sapply(c(0.5, 0.75, 0.9, 0.95, 0.98, 1), function(p) pramString(str, p))
## [1] "p = 0.5 : uw-neayrixby disasted. Thm idkdggmlnodmurjvvelt umjtcrulrutd reomt mrxxid"
## [2] "p = 0.75 : hosdepl is aedisarterulThe govermment must tell us tht trupi aboutxBrqmit"
## [3] "p = 0.9 : No-peal is a disastehs The gqvernment must tezl us the trwtn about Brexmt"
## [4] "p = 0.95 : No-deal is a disacter. The government must tela us the trukh abokt Bnexrt"
## [5] "p = 0.98 : No-deal is a disaster. The government musz tell us the truth about Brexit"
## [6] "p = 1 : No-deal is a disaster. The government must tell us the truth about Brexit"
pramImage <- function(img, p) {
mat <- matrix(
sapply(1:length(img), function(i) ifelse(rbinom(1, 1, prob=p), img[i], sample(seq(0, 1, len=255), 1))),
nrow=28L)
image(mat, col = grey(level = seq(0, 1, by=1/255)), axes=F, main = paste0('p = ', p))
}
nil <- sapply(c(0.5, 0.75, 0.9, 0.95, 0.98, 1), function(p) pramImage(img, p))