-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp.impute.moon.R
239 lines (238 loc) · 10.4 KB
/
exp.impute.moon.R
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
################################################################################
## File: exp.impute.moon.R ##
## Created by: Pavlo Mozharovskyi ##
## Last revised: 22.07.2018 ##
## ##
## Contains experiments on single local-depth-based imputation of the moon ##
## data set (Figure 5). ##
## ##
################################################################################
source("impute.functions.R")
library(imputeDepth)
library(geometry)
library(norm)
library(missForest)
library(missMDA)
library(rrcov)
library(VIM)
library(mvtnorm)
# Create structures
ems.depth.loc1 <- NULL
ems.depth.loc2 <- NULL
ems.depth.loc3 <- NULL
ems.depth.Tr2 <- NULL
ems.depth.zm <- NULL
ems.depth.M <- NULL
ems.depth.Mr <- NULL
ems.em <- NULL
ems.forest <- NULL
ems.knn <- NULL
ems.regPCA.1 <- NULL
ems.mean <- NULL
k <- 100
pNA <- 0.15
set.seed(1)
cltype <- "SOCK"
nproc <- 32
if (nproc > 1){
# Imputing routine to be calles on a node
imp.worker <- function(i){
# Load packages
source("impute.functions.R")
library(imputeDepth)
library(geometry)
library(norm)
library(missForest)
library(missMDA)
library(rrcov)
library(VIM)
library(mvtnorm)
# Generate the moon
d <- 2
n <- 150
f1 <- function(x){1.5 * (1 - x^2)}
f2 <- function(x){2 * (1 - x^2)}
X <- cbind(runif(n * 1000, -1, 1), runif(n * 1000, 0, 2))
X <- X[X[,2] >= f1(X[,1]) & X[,2] <= f2(X[,1]),]
X <- X[1:n,]
# Add NAs
pNA <- 0.15
X.miss <- X
X.miss[sample.int(nrow(X), pNA * nrow(X) * ncol(X)),2] <- NA
numNA <- sum(is.na(X.miss))
cat((numNA), " NAs produced\n")
# Impute
X.imp.depth.loc1 <- impute.depth.local(X.miss, par.loc = 0.2,
depth = "halfspace",
parMcd.impute = 1)
X.imp.depth.loc2 <- impute.depth.local(X.miss, par.loc = 0.2,
depth = "zonoid",
parMcd.impute = 1)
X.imp.depth.loc3 <- impute.depth.local(X.miss, par.loc = 0.2,
depth = "Mahalanobis",
parMcd.impute = 1)
X.imp.depth.Tr2 <- impute.depth(X.miss, depth = "halfspace",
depth.outsiders = "spatial",
parMcd.outsiders = 0.5)
X.imp.depth.zm <- impute.depth(X.miss, depth = "zonoid")
X.imp.depth.M <- impute.depth(X.miss, depth = "Mahalanobis")
X.imp.depth.Mr <- impute.depth(X.miss, depth = "Mahalanobis",
parMcd.impute = 0.75)
X.imp.em <- imputeEm(as.matrix(X.miss))
X.imp.forest <- missForest(cbind(X.miss, 1))$ximp[,1:2]
X.imp.knn <- imputeKnn(X.miss)
X.imp.regPCA.1 <- imputePCA(X.miss, ncp = 1)$completeObs
X.imp.mean <- imputeMean(X.miss)
# Calculate raw statistics
ems.depth.loc1 <- sqrt(sum((X.imp.depth.loc1 - X)^2) / (n * d * pNA))
ems.depth.loc2 <- sqrt(sum((X.imp.depth.loc2 - X)^2) / (n * d * pNA))
ems.depth.loc3 <- sqrt(sum((X.imp.depth.loc3 - X)^2) / (n * d * pNA))
ems.depth.Tr2 <- sqrt(sum((X.imp.depth.Tr2 - X)^2) / (n * d * pNA))
ems.depth.zm <- sqrt(sum((X.imp.depth.zm - X)^2) / (n * d * pNA))
ems.depth.M <- sqrt(sum((X.imp.depth.M - X)^2) / (n * d * pNA))
ems.depth.Mr <- sqrt(sum((X.imp.depth.Mr - X)^2) / (n * d * pNA))
ems.em <- sqrt(sum((X.imp.em - X)^2) / (n * d * pNA))
ems.forest <- sqrt(sum((X.imp.forest - X)^2) / (n * d * pNA))
ems.knn <- sqrt(sum((X.imp.knn - X)^2) / (n * d * pNA))
ems.regPCA.1 <- sqrt(sum((X.imp.regPCA.1 - X)^2) / (n * d * pNA))
ems.mean <- sqrt(sum((X.imp.mean - X)^2) / (n * d * pNA))
# Return the errors
return (c(ems.depth.Tr2, ems.depth.zm, ems.depth.M,
ems.depth.Mr, ems.em, ems.forest, ems.knn,
ems.regPCA.1, ems.mean,
ems.depth.loc1, ems.depth.loc2, ems.depth.loc3))
}
# Printing routine
print.fun <- function(outputs, B, args){
pb <- args$mypb
len.one.run <- args$len.one.run
outputs <- unlist(outputs)
outputs <- outputs[!is.null(outputs)]
setTxtProgressBar(pb, length(outputs)/len.one.run)
}
# The parallel call
library(snowFT)
res <- performParallel(count = nproc, x = 1:k, imp.worker,
printfun = print.fun,
printargs = list(
mypb = txtProgressBar(min = 0,
max = k,
style = 3),
len.one.run = 1), printrepl = 1,
cltype = cltype)
# Assemble results
for (i in 1:k){
ems.depth.Tr2 <- c(ems.depth.Tr2, res[[i]][1])
ems.depth.zm <- c(ems.depth.zm, res[[i]][2])
ems.depth.M <- c(ems.depth.M, res[[i]][3])
ems.depth.Mr <- c(ems.depth.Mr, res[[i]][4])
ems.em <- c(ems.em, res[[i]][5])
ems.forest <- c(ems.forest, res[[i]][6])
ems.knn <- c(ems.knn, res[[i]][7])
ems.regPCA.1 <- c(ems.regPCA.1, res[[i]][8])
ems.mean <- c(ems.mean, res[[i]][9])
ems.depth.loc1 <- c(ems.depth.loc1, res[[i]][10])
ems.depth.loc2 <- c(ems.depth.loc2, res[[i]][11])
ems.depth.loc3 <- c(ems.depth.loc3, res[[i]][12])
}
# Save results
save.image(paste("imp_moon_MCAR-15_n150-d2-k", i, "_",
gsub(" ", "_", gsub(":", "_", date())), ".RData",
sep = ""))
}else{
# Start study
d <- 2
n <- 150
pNA <- 0.15
for (i in 1:k){
cat("Iteration", i, "started.\n")
# Generate the moon
f1 <- function(x){1.5 * (1 - x^2)}
f2 <- function(x){2 * (1 - x^2)}
X <- cbind(runif(n * 1000, -1, 1), runif(n * 1000, 0, 2))
X <- X[X[,2] >= f1(X[,1]) & X[,2] <= f2(X[,1]),]
X <- X[1:n,]
# Add NAs
X.miss <- X
X.miss[sample.int(nrow(X), pNA * nrow(X) * ncol(X)),2] <- NA
numNA <- sum(is.na(X.miss))
cat((numNA), " NAs produced\n")
# Impute
X.imp.depth.loc1 <- impute.depth.local(X.miss, par.loc = 0.2,
depth = "halfspace",
parMcd.impute = 1)
X.imp.depth.loc2 <- impute.depth.local(X.miss, par.loc = 0.2,
depth = "zonoid",
parMcd.impute = 1)
X.imp.depth.loc3 <- impute.depth.local(X.miss, par.loc = 0.2,
depth = "Mahalanobis",
parMcd.impute = 1)
X.imp.depth.Tr2 <- impute.depth(X.miss, depth = "halfspace",
depth.outsiders = "spatial",
parMcd.outsiders = 0.5)
X.imp.depth.zm <- impute.depth(X.miss, depth = "zonoid")
X.imp.depth.M <- impute.depth(X.miss, depth = "Mahalanobis")
X.imp.depth.Mr <- impute.depth(X.miss, depth = "Mahalanobis",
parMcd.impute = 0.75)
X.imp.em <- imputeEm(as.matrix(X.miss))
X.imp.forest <- missForest(cbind(X.miss, 1))$ximp[,1:2]
X.imp.knn <- imputeKnn(X.miss)
X.imp.regPCA.1 <- imputePCA(X.miss, ncp = 1)$completeObs
X.imp.mean <- imputeMean(X.miss)
# Calculate raw statistics
ems.depth.TrE <- c(ems.depth.TrE, sqrt(sum((X.imp.depth.TrE - X)^2) /
(n * d * pNA)))
ems.depth.Tr2 <- c(ems.depth.Tr2, sqrt(sum((X.imp.depth.Tr2 - X)^2) /
(n * d * pNA)))
ems.depth.zm <- c(ems.depth.zm, sqrt(sum((X.imp.depth.zm - X)^2) /
(n * d * pNA)))
ems.depth.M <- c(ems.depth.M, sqrt(sum((X.imp.depth.M - X)^2) /
(n * d * pNA)))
ems.depth.Mr <- c(ems.depth.Mr, sqrt(sum((X.imp.depth.Mr - X)^2) /
(n * d * pNA)))
ems.em <- c(ems.em, sqrt(sum((X.imp.em - X)^2) / (n * d * pNA)))
ems.forest <- c(ems.forest, sqrt(sum((X.imp.forest - X)^2) / (n * d * pNA)))
ems.knn <- c(ems.knn, sqrt(sum((X.imp.knn - X)^2) / (n * d * pNA)))
ems.regPCA.1 <- c(ems.regPCA.1, sqrt(sum((X.imp.regPCA.1 - X)^2) /
(n * d * pNA)))
ems.mean <- c(ems.mean, sqrt(sum((X.imp.mean - X)^2) / (n * d * pNA)))
ems.depth.loc1 <- c(ems.depth.loc1, sqrt(sum((X.imp.depth.loc1 - X)^2) /
(n * d * pNA)))
ems.depth.loc2 <- c(ems.depth.loc2, sqrt(sum((X.imp.depth.loc2 - X)^2) /
(n * d * pNA)))
ems.depth.loc3 <- c(ems.depth.loc3, sqrt(sum((X.imp.depth.loc3 - X)^2) /
(n * d * pNA)))
# Save intermediate results
if (i %% 10 < 1){
save.image(paste("imp_moon_MCAR-15_n150-d2-k", i, "_",
gsub(" ", "_", gsub(":", "_", date())), ".RData",
sep = ""))
}
# Calculate statistics
cat("Iteration", i, "finished. Median RMSEs are:\n")
cat("d.Tuk:", median(ems.depth.Tr2), ", d.zon:", median(ems.depth.zm),
", d.Mah:", median(ems.depth.M), ", d.MahR:", median(ems.depth.Mr),
", EM:", median(ems.em), ", rPCA1:", median(ems.regPCA.1),
", kNN:", median(ems.knn), ", RF:", median(ems.forest),
", mean:", median(ems.mean),
", ld.Tuk:", median(ems.depth.loc1),
", ld.zon:", median(ems.depth.loc2),
", ld.Mah:", median(ems.depth.loc3), ".\n")
}
}
errors <- list(TukeyR2 = ems.depth.Tr2, zonoidM = ems.depth.zm,
Mahalanobis = ems.depth.M, MahalanobisR = ems.depth.Mr,
em = ems.em,
pca1 = ems.regPCA.1,
knn = ems.knn, forest = ems.forest,
mean = ems.mean,
loc1 = ems.depth.loc1, loc2 = ems.depth.loc2,
loc3 = ems.depth.loc3)
boxplot(errors, main = paste("Moon, n = ", n, ", d = ", d,
", MCAR ", pNA, ", k = ", i, sep = ""),
names = c("d.Tuk", "d.zon",
"d.Mah", "d.MahR",
"EM", "rPCA1", "kNN", "RF",
"mean", "ld.Tuk", "ld.zon", "ld.Mah"),
ylab = "RMSE")
grid()