-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbenchmark.R
66 lines (57 loc) · 2.09 KB
/
benchmark.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
imputation.benchmark.random = function(numRow = 100, numCol = 100, numMissing = 50,
imputation.fn = NULL, ...) {
if(is.null(imputation.fn)) {
stop("A handler to an imputation function should be passed in as an argument")
}
missingX = sample(1:numRow, numMissing, replace=T)
missingY = sample(1:numCol, numMissing, replace=T)
x.missing = x = matrix(rnorm(numRow * numCol), numRow, numCol)
for (i in 1:numMissing) {
x.missing[missingX[i], missingY[i]] = NA
}
x.imputed = imputation.fn(x.missing, ...)
SE = mapply(function(missingx,missingy) {
((x[missingx, missingy] - x.imputed[missingx, missingy]) / x[missingx, missingy])^2
}, missingX, missingY)
return (
list(
data = x,
missing = x.missing,
imputed = x.imputed,
rmse = sqrt(mean(SE))
)
)
}
imputation.benchmark.ts = function(numTS = 100, TSlength = 100, numMissing = 50,
imputation.fn = NULL, ...) {
if(is.null(imputation.fn)) {
stop("A handler to an imputation function should be passed in as an argument")
}
missingX = sample(1:numTS, numMissing, replace=T)
missingY = sample(1:TSlength, numMissing, replace=T)
x.missing = x = t(sapply(1:numTS, function(i) {
rand = rnorm(1)
#Need to be careful to only generate time series that are stationary
if(rand <= 0) {
series = arima.sim(n = TSlength, list(ar = c(0.8, -0.5), ma=c(-0.23, 0.25)) )
} else if(rand > 0) {
series = arima.sim(n = TSlength, list(ar = c(1, -0.5), ma=c(-.4)) )
}
return (as.vector(series))
}))
for (i in 1:numMissing) {
x.missing[missingX[i], missingY[i]] = NA
}
x.imputed = imputation.fn(x.missing, ...)
SE = mapply(function(missingx,missingy) {
((x[missingx, missingy] - x.imputed[missingx, missingy]) / x[missingx, missingy])^2
}, missingX, missingY)
return (
list(
data = x,
missing = x.missing,
imputed = x.imputed,
rmse = sqrt(mean(SE))
)
)
}