forked from davraam/DS_Graphics_Development
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNon-disclosive_ScatterPlot.R
More file actions
71 lines (50 loc) · 2.09 KB
/
Non-disclosive_ScatterPlot.R
File metadata and controls
71 lines (50 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# NON-DISCLOSIVE SCATTER PLOT USING THE CENTROIDS OF EACH 3-NEAREST NEIGHBOURS
setwd("C:/Users/da15564/Dropbox/Demetris_BRISTOL/Masking sensitive microdata in Scatterplots/k-NN/3-NN")
set.seed(050516)
# number of data points
n <- 100
x <- rnorm(n, 10, 1)
y <- x + rnorm(n, 0, 1)
data <- cbind(x,y)
# Load the RANN package to use the 'nn2' function that searches for the Nearest Neighbours
library(RANN)
# calculate the coordinates of the centroids by averaging the x and y coordinates of nearest neighbours separately
neighbours <- 3
# Find the two nearest neighbours of each data point
nearest <- nn2(data, k = neighbours)
# Calculate the centroid of each n nearest data points
x.centroid <- matrix()
y.centroid <- matrix()
for (i in 1:n){
x.centroid[i] <- mean(x[nearest$nn.idx[i,1:neighbours]])
y.centroid[i] <- mean(y[nearest$nn.idx[i,1:neighbours]])
}
# Shift the new data points to the origin
x.shifted <- matrix()
y.shifted <- matrix()
for (i in 1:n){
x.shifted[i] <- x.centroid[i] - mean(x.centroid)
y.shifted[i] <- y.centroid[i] - mean(y.centroid)
}
# Calculate the scaling factor
x.scalingFactor <- sqrt(var(x))/sqrt(var(x.centroid))
y.scalingFactor <- sqrt(var(y))/sqrt(var(y.centroid))
# Apply the scaling factor to the shifted centroids
x.masked <- x.shifted * x.scalingFactor
y.masked <- y.shifted * y.scalingFactor
# Shift the scaled data back to their actual position
x.new <- x.masked + mean(x.centroid)
y.new <- y.masked + mean(y.centroid)
min <- min(min(x), min(y))
max <- max(max(x), max(y))
# Create and save the Scatterplot
tiff(filename = "ScatterPlot.tiff", width = 480, height = 480)
# set the margins around the plot window
par(mar=c(4.5, 4.5, 1.5, 1.5))
plot(x, y, xlim=c(min, max), ylim=c(min, max), pch=1)
abline(lm(y~x))
# points(x.centroid, y.centroid, col='green', pch=3)
points(x.new, y.new, col='black', pch=3)
abline(lm(y.new~x.new), col='black', lty=2)
legend(6.8, 13, c("Original data", "Original data trendline", "Masked data", "Masked data trendline"), pch=c(1,NA,3,NA), lty=c(NA,1,NA,2))
dev.off()