-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.R
252 lines (228 loc) · 8.11 KB
/
evaluation.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
240
241
242
243
244
245
246
247
248
249
250
251
252
# evaluation
library(parallel)
H <- function(x) {
ifelse(x < 0, 0, 1)
}
isNonDecreasing <- function(cdfs) {
cdfs <- as.matrix(cdfs)
all(apply(cdfs, MARGIN = 1, function(row) {
all(diff(row) >= 0)
}))
}
CRPS <- function(cdfmat, actual, cls = FALSE) {
if (!isNonDecreasing(cdfmat)) {
stop("cdf isn't nondecreasing")
}
temp <- data.frame(cdfmat=cdfmat, actual=actual)
mms <- matrix(rep(0:69, nrow(temp)), nrow = nrow(temp), byrow = TRUE)
if (!cls) {
mean(sapply(1:nrow(temp), function(row) {
mean((temp[row, 0:70] - H(mms[row, ] - actual[row]))^2)
}))
} else {
numNodes <- detectCores() - 1
cls <- makeCluster(numNodes, type="FORK")
crps <- mean(parSapply(cls, 1:nrow(temp), function(row) {
mean((temp[row, 0:70] - H(mms[row, ] - actual[row]))^2)
}))
stopCluster(cls)
return(crps)
}
}
logit70 <- function(trtemp, tetemp, mmmax=69) {
probsByMM <- sapply(0:mmmax, function(mm) {
tempExpected <- ifelse(trtemp$Expected >= (mm - .5) & trtemp$Expected <= (mm + .5), 1, 0)
tempFit <- glm(tempExpected ~ . - Id - Expected, family = "binomial", data = trtemp)
predict(tempFit, tetemp, type="response")
})
probsByMM <- cbind(probsByMM, matrix(0, nrow = nrow(tetemp), ncol = 70 - ncol(probsByMM)))
allCumsums <- t(apply(probsByMM, MARGIN=1, cumsum))
cdfs <- as.data.frame(t(apply(allCumsums, MARGIN=1, function(obs) obs/obs[length(obs)])))
cdfs$Id <- as.integer(tetemp$Id)
cdfs <- cdfs[,c(ncol(cdfs), 1:(ncol(cdfs)-1))]
names(cdfs) <- c("Id", paste("Predicted", 0:69, sep=''))
return(cdfs)
}
getCRPS <- function(model = logit70, data = data, dataSize = nrow(data), cvk = 10, mmmax = 69, cls = FALSE) {
data <- data[sample(x = nrow(data), size = dataSize, replace = FALSE), ]
nobs <- floor(nrow(data) / cvk)
dfsplitup <- lapply(1:cvk, function(block) {
data[sample(x = 1:nrow(data), size = nobs, replace = FALSE), ]
})
begin <- Sys.time()
crpslist <- sapply(1:cvk, function(testnum) {
trnums <- (1:10)[-testnum]
trtemp <- do.call(rbind, dfsplitup[trnums])
tetemp <- dfsplitup[[testnum]]
predtemp <- model(trtemp, tetemp, mmmax)
CRPS(predtemp[ , -1], tetemp$Expected, cls = cls)
})
end <- Sys.time()
tot <- end - begin
list(varnames = names(data), time = tot, crps = crpslist, crpsavg = mean(crpslist))
}
keepVariables <- c("RR1.mean", "RR1.range", "Reflectivity.mean", "Reflectivity.range", "RhoHV.mean", "RhoHV.range")
keepVariables <- c("RR1.range", "RR1.range")
data <- train[ , keepVariables]
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
data <- data[,-1]
getCRPS(data = data, dataSize = 50000, mmmax = 12, cls = TRUE)
# 5.223747 minutes, serial, 6 variables, cvk = 10, mmmax = 69, size of all data used = 10000
# 2.456471 minutes, parall, 6 variables, cvk = 10, mmmax = 69, size of all data used = 10000
# 0.597540 minutes, parall, 6 variables, cvk = 10, mmmax = 10, size of all data used = 10000
# 0.061651 minutes, parall, 6 variables, cvk = 10, mmmax = 5 , size of all data used = 100
# 2.878385 minutes, parall, 6 variables, cvk = 10, mmmax = 12, size of all data used = 50000, .007379382
# 33.47914 minutes, parall, 6 variables, cvk = 10, mmmax = 12, size of all data used = 500000, .007568024
# 2.410278 minutes, parall, RR1.mean , cvk = 10, mmmax = 12, size of all data used = 50000, .0079908
holder <- list()
listofvarnames <- names(train)[-length(names(train))]
counter = 1
for (varname in listofvarnames) {
keepVariables <- c("hydroMode", varname)
data <- train[ , keepVariables]
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
data <- data[,-1]
holder[[counter]] <- getCRPS(data = data, dataSize = 50000, mmmax = 12, cls = TRUE)
counter = counter + 1
}
save(holder, file = paste(directory, "holder1.Rda", sep=''))
holder2 <- list()
removeVars <- c("Reflectivity.mean", "hydroMode")
keepVars <- setdiff(names(train), removeVars)
listofvarnames <- keepVars
counter = 1
for (varname in listofvarnames) {
keepVariables <- c("Reflectivity.mean", varname)
data <- train[ , keepVariables]
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
holder2[[counter]] <- getCRPS(data = data, dataSize = 50000, mmmax = 12, cls = TRUE)
counter = counter + 1
}
save(holder2, file = paste(directory, "holder2.Rda", sep=''))
before = Sys.time()
holder3 <- list()
listofvarnames <- names(train)[-length(names(train))]
counter = 1
for (varname in listofvarnames) {
keepVariables <- c("hydroMode", varname)
data <- train[ , keepVariables]
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
data <- data[,-1]
holder3[[counter]] <- getCRPS(data = data, dataSize = 50000, mmmax = 12, cls = TRUE)
counter = counter + 1
## repeat with squared term
data <- train[ , keepVariables]
data[,2] <- data[,2]^2
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
data <- data[,-1]
holder3[[counter]] <- getCRPS(data = data, dataSize = 50000, mmmax = 12, cls = TRUE)
counter = counter + 1
}
after = Sys.time()
total = after - before
total
save(holder3, file = paste(directory, "holder3.Rda", sep=''))
# Start with original model, and use getCrps() + backwards stepwise procedure
before = Sys.time()
holder4 <- list()
all <- names(train)[1:8]
counter = 1
for (varnum in 0:length(all)) {
if (varnum == 0) {
data <- train[ , all]
} else {
data <- train[ , all[-varnum]]
}
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
holder4[[counter]] <- getCRPS(data = data, dataSize = 50000, mmmax = 69, cls = TRUE)
cat("Leaving out: ", all[varnum], " \n")
cat("All: ", holder4[[counter]]$crps, "\n")
cat("Average: ", holder4[[counter]]$crpsavg, " \n\n")
counter = counter + 1
}
after = Sys.time()
total = after - before
total
save(holder4, file = paste(directory, "holder4.Rda", sep=''))
## Leaving Zdr.mean and Zdr.range yield better results!
# Step 2 of backwards stepwise, original model - Zdr.range
before = Sys.time()
holder5 <- list()
all <- names(train)[1:7] # remove Zdr.range (it just happens to be last)
counter = 1
for (varnum in 0:length(all)) {
if (varnum == 0) {
data <- train[ , all]
} else {
data <- train[ , all[-varnum]]
}
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
holder5[[counter]] <- getCRPS(data = data, dataSize = 1126694, mmmax = 69, cls = TRUE)
cat("Leaving out: ", all[varnum], " \n")
cat("All: ", holder5[[counter]]$crps, "\n")
cat("Average: ", holder5[[counter]]$crpsavg, " \n\n")
counter = counter + 1
}
after = Sys.time()
total = after - before
total
save(holder5, file = paste(directory, "holder5.Rda", sep=''))
# Step 3 of backwards stepwise, original model - Zdr.range - Reflectivity.range
before = Sys.time()
holder6 <- list()
removeVars <- c("Zdr.range", "Reflectivity.range")
includeVars <- setdiff(names(train)[1:8], removeVars)
counter = 1
for (varnum in 0:length(includeVars)) {
if (varnum == 0) {
data <- train[ , includeVars]
} else {
data <- train[ , includeVars[-varnum]]
}
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
holder6[[counter]] <- getCRPS(data = data, dataSize = 50000, mmmax = 69, cls = TRUE)
cat("Leaving out: ", all[varnum], " \n")
cat("All: ", holder6[[counter]]$crps, "\n")
cat("Average: ", holder6[[counter]]$crpsavg, " \n\n")
counter = counter + 1
}
after = Sys.time()
total = after - before
total
save(holder6, file = paste(directory, "holder6.Rda", sep=''))
# Step 3 v2 of backwards stepwise, original model - Zdr.range - RhoHV.mean
before = Sys.time()
holder7 <- list()
removeVars <- c("Zdr.range", "RhoHV.mean")
includeVars <- setdiff(names(train)[1:8], removeVars)
counter = 1
for (varnum in 0:length(includeVars)) {
if (varnum == 0) {
data <- train[ , includeVars]
} else {
data <- train[ , includeVars[-varnum]]
}
data$Expected <- tr$Expected.mean
data$Id <- tr$Id.mean
holder7[[counter]] <- getCRPS(data = data, dataSize = 50000, mmmax = 20, cls = TRUE)
cat("Leaving out: ", all[varnum], " \n")
cat("All: ", holder7[[counter]]$crps, "\n")
cat("Average: ", holder7[[counter]]$crpsavg, " \n\n")
counter = counter + 1
}
after = Sys.time()
total = after - before
total
save(holder7, file = paste(directory, "holder7.Rda", sep=''))
load(file = paste(directory, "holder1.Rda", sep=''))
load(file = paste(directory, "holder2.Rda", sep=''))
sort(unlist(lapply(holder, function(x) x$crpsavg)))
sort(unlist(lapply(holder2, function(x) x$crpsavg)))