-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplforest.R
375 lines (310 loc) · 15.8 KB
/
plforest.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
library(tidyverse)
library(PlackettLuce) # for pltree function
library(rsample) # for taking samples
library(wrapr) # for sort vector
library(foreach) # for parallel computing
library(doParallel)
library(caret) # for predict function
###############################################################################
###############################################################################
# Create bagging or random forest based on pltree from package PlackettLuce
plforest <- function(rankings, covariates,
bootstrapping_n = 10, k_fold_CV = 5,
grouped_rankings = FALSE,
...) {
# bootstrapping covariate dataframe
training_boots <- bootstraps(covariates,
times = bootstrapping_n)
# create a table to store the best tree for each bootstrapping iteration
best_tree_table <- as.data.frame(matrix(nrow = 0, ncol = 4))
colnames(best_tree_table) <- c("n", "k", "accurate_rate", "tree_name")
# loop over each bootstrapping iteration
for (n in 1:bootstrapping_n) {
# subset the training dataset to n-th bootstrapping iteration
subsample <- covariates[training_boots$splits[[n]]$in_id,]
subsample_rank <- rankings[training_boots$splits[[n]]$in_id,]
# perfrom k-fold CV
fold <- vfold_cv(subsample, v = k_fold_CV)
# create a table to store the accurate rate of k-fold tree iterations
accurate_rate_table <- as.data.frame(matrix(nrow = 0, ncol = 4))
colnames(accurate_rate_table) <- c("n", "k", "accurate_rate", "tree_name")
# loop over each n-fold CV iteration
for (k in 1:k_fold_CV) {
# subset the training dataset to k-th k-fold CV iteration
subsample_fold <- subsample[fold$splits[[k]]$in_id,]
subsample_rank_fold <- subsample_rank[fold$splits[[k]]$in_id,]
# convert rankings to group rankings format if needed
if (grouped_rankings == FALSE) {
subsample_rank_fold <- group(subsample_rank_fold,
index = 1:length(subsample_rank_fold))
}
# perform Plackett-Luce trees
# ... for additional arguments pass to mob_control function
subsample_rank_fold_tree <- pltree(formula = subsample_rank_fold ~.,
data = subsample_fold, ...)
# subset the validating dataset to k-th k-fold CV iteration
subsample_fold_test <- subsample[-fold$splits[[k]]$in_id,]
# generate prediction based on the fitted tree
subsample_fold_predict <-
predict(subsample_rank_fold_tree, subsample_fold_test)
# create a table to hold predicted rankings
subsample_fold_predict_rank <- as.data.frame(
matrix(nrow = nrow(subsample_fold_test),
ncol = dim(as.rankings(rankings))[2]))
# add results to the predicted rankings table
for (i in 1:nrow(subsample_fold_predict_rank)) {
sorted_colnames <- subsample_fold_predict[i,] |>
sort(decreasing = T) |>
as.data.frame() |>
rownames()
for (c in 1:ncol(subsample_fold_predict_rank)) {
subsample_fold_predict_rank[i, c] <- sorted_colnames[c]
}
}
# convert predicted rankings table to rankings format
subsample_fold_predict_rank <-
as.rankings(subsample_fold_predict_rank, input = "orderings")
# subset to create true rankings for validating subsample
subsample_fold_true_rank <-
subsample_rank[-fold$splits[[k]]$in_id,]
# check for whether prediction match the true rankings
checker <-
subsample_fold_predict_rank == subsample_fold_true_rank
# create a counter to store the number of matching
correct_case <- 0
# generate the correct case count
for (i in 1:nrow(subsample_fold_predict_rank)) {
if (all(checker[i,])) {
correct_case <- correct_case + 1
}
}
# calculate the accurate rate for this fold iteration
accurate_rate <- correct_case/nrow(subsample_fold_predict_rank)*100
# store the iteration results and the accurate_rate
assign(paste0(n, "_", k, "_tree"), subsample_rank_fold_tree)
additional_row <- cbind(n,
k,
accurate_rate,
paste0(n, "_", k, "_tree")) |> as.data.frame()
additional_row |> colnames() <-
c("n", "k", "accurate_rate", "tree_name")
accurate_rate_table <- rbind(accurate_rate_table,
additional_row)
}
# find the tree name with the highest accuracy rate
best_tree_name <-
accurate_rate_table[which(accurate_rate_table$accurate_rate ==
max(accurate_rate_table$accurate_rate)),
"tree_name"]
# handle the case when two or more trees have tied accuracy
# return only the first one for simplicity
best_tree_name <- best_tree_name[1]
# store the iteration results and the accurate_rate
additional_row <- cbind(n, k,
max(accurate_rate_table$accurate_rate),
best_tree_name) |> as.data.frame()
additional_row |> colnames() <-
c("n", "k", "accurate_rate", "tree_name")
best_tree_table <- rbind(best_tree_table,
additional_row)
# print a counter to show percentage of completion for each bootstrapping
cat('\b\b\b\b\b\b\b\b\b\b\b\b\b',
n/bootstrapping_n*100,
"% Completed", sep = "")
}
# combine outputs into a large list for returning
output <- list(best_tree_table, mget(best_tree_table$tree_name))
return(output)
}
###############################################################################
###############################################################################
# Predict worth based on new covariates according to the plforest model
predict_plforest <- function(plforest_output, covariates) {
# record the bootstrapping number of the output
n <- dim(plforest_output[[1]])[1]
# record the number of ranked levels
model_predict_n <- predict(plforest_output[[2]][1], covariates)
model_predict_n <- as.data.frame(model_predict_n[[1]])
n_level <- ncol(model_predict_n)
# create a holder to store the bagging predicted results
bagged_predict <- as.data.frame(matrix(0,
nrow = nrow(covariates),
ncol = n_level))
# store the level names to the holder
colnames(bagged_predict) <- colnames(model_predict_n)
# convert the holder into matrix for summation
bagged_predict <- as.matrix(bagged_predict)
# loop over each bootstrapping iteration's best model prediction
# and summing the predicted worth up
for (i in 1:n) {
model_predict_n <- predict(plforest_output[[2]][i], covariates)
model_predict_n <- model_predict_n[[1]]
bagged_predict <- bagged_predict +
as.matrix(model_predict_n)
}
# create a holder to store the bagging predicted results in rank order
bagged_predict_rank <- as.data.frame(matrix(nrow = nrow(bagged_predict),
ncol = ncol(bagged_predict)))
colnames(bagged_predict_rank) <- 1:ncol(bagged_predict_rank)
# loop over each prediction to convert worth to ranking
for (i in 1:nrow(bagged_predict)) {
rank <- as.numeric(bagged_predict[i,])
names(rank) <- colnames(bagged_predict)
bagged_predict_rank[i, ] <-
names(sort(rank, decreasing = T))
}
# convert rank ordering in rankings format
bagged_predict_rank <- as.rankings(bagged_predict_rank, input = "ordering")
# standardize the predicted worth matrix (enforce each row sum to 1)
bagged_predict <- bagged_predict/n
# combine the predicted worth matrix and predicted rankings together into
# a list for return
output <- list(bagged_predict, bagged_predict_rank)
return(output)
}
###############################################################################
###############################################################################
# A parallel computing implementation of plforest function
plforest_mc <- function(rankings, covariates,
bootstrapping_n = 10, k_fold_CV = 5,
grouped_rankings = FALSE, cores = detectCores()-1,
...) {
# bootstrapping covariate dataframe
training_boots <- bootstraps(covariates,
times = bootstrapping_n)
# create a table to store the best tree for each bootstrapping iteration
best_tree_table <- as.data.frame(matrix(nrow = bootstrapping_n,
ncol = 4))
colnames(best_tree_table) <- c("n", "k", "accurate_rate", "tree_name")
# create or empty the progress txt file to denote progress
close(file("./progress.txt", open="w"))
# register cluster
cl <- makeCluster(cores) # not to overload computer
registerDoParallel(cl)
# loop over each bootstrapping iteration
# with parallel computing
best_tree_list <-
foreach(n=1:bootstrapping_n, .combine = rbind,
.packages = c("tidyverse", "PlackettLuce",
"rsample", "wrapr")) %dopar%
{
# subset the training dataset to n-th bootstrapping iteration
subsample <- covariates[training_boots$splits[[n]]$in_id,]
subsample_rank <- rankings[training_boots$splits[[n]]$in_id,]
# perfrom k-fold CV
fold <- vfold_cv(subsample, v = k_fold_CV)
# create a table to store the accurate rate of k-fold tree iterations
accurate_rate_table <- as.data.frame(matrix(nrow = 0,
ncol = 4))
colnames(accurate_rate_table) <- c("n", "k", "accurate_rate", "tree_name")
# loop over each n-fold CV iteration
for (k in 1:k_fold_CV) {
# subset the training dataset to k-th k-fold CV iteration
subsample_fold <- subsample[fold$splits[[k]]$in_id,]
subsample_rank_fold <- subsample_rank[fold$splits[[k]]$in_id,]
# convert rankings to group rankings format if needed
if (grouped_rankings == FALSE) {
subsample_rank_fold <- group(subsample_rank_fold,
index = 1:length(subsample_rank_fold))
}
# subset the validating dataset to k-th k-fold CV iteration
subsample_fold_test <- subsample[-fold$splits[[k]]$in_id,]
# loop over all the factor class variables
# add missing levels in the training dataset from the testing dataset
for (x in 1:ncol(subsample_fold)) {
if (class(subsample_fold[,x]) == "factor") {
if (all(levels(subsample_fold[,x]) ==
levels(subsample_fold_test[,x]))) {
next
} else {
all_levels <- union(levels(subsample_fold[,x]),
levels(subsample_fold_test[,x]))
levels(subsample_fold[,x]) <- all_levels
levels(subsample_fold_test[,x]) <- all_levels
}
} else {
next
}
}
# perform Plackett-Luce trees
# ... for additional arguments pass to mob_control function
subsample_rank_fold_tree <- pltree(formula = subsample_rank_fold ~.,
data = subsample_fold, ...)
# generate prediction based on the fitted tree
subsample_fold_predict <-
predict(subsample_rank_fold_tree, subsample_fold_test)
# create a table to hold predicted rankings
subsample_fold_predict_rank <- as.data.frame(
matrix(nrow = nrow(subsample_fold_test),
ncol = dim(as.rankings(rankings))[2]))
# add results to the predicted rankings table
for (i in 1:nrow(subsample_fold_predict_rank)) {
sorted_colnames <- subsample_fold_predict[i,] |>
sort(decreasing = T) |>
as.data.frame() |>
rownames()
for (c in 1:ncol(subsample_fold_predict_rank)) {
subsample_fold_predict_rank[i, c] <- sorted_colnames[c]
}
}
# convert predicted rankings table to rankings format
subsample_fold_predict_rank <-
as.rankings(subsample_fold_predict_rank, input = "orderings")
# subset to create true rankings for validating subsample
subsample_fold_true_rank <-
subsample_rank[-fold$splits[[k]]$in_id,]
# check for whether prediction match the true rankings
checker <-
subsample_fold_predict_rank == subsample_fold_true_rank
# create a counter to store the number of matching
correct_case <- 0
# generate the correct case count
for (i in 1:nrow(subsample_fold_predict_rank)) {
if (all(checker[i,])) {
correct_case <- correct_case + 1
}
}
# calculate the accurate rate for this fold iteration
accurate_rate <- correct_case/nrow(subsample_fold_predict_rank)*100
# store the iteration results and the accurate_rate
assign(paste0(n, "_", k, "_tree"), subsample_rank_fold_tree)
additional_row <- cbind(n,
k,
accurate_rate,
paste0(n, "_", k, "_tree")) |>
as.data.frame()
additional_row |> colnames() <-
c("n", "k", "accurate_rate", "tree_name")
accurate_rate_table <- rbind(accurate_rate_table,
additional_row)
}
# find the tree name with the highest accuracy rate
best_tree_name <- accurate_rate_table[
which(accurate_rate_table$accurate_rate ==
max(accurate_rate_table$accurate_rate)),
"tree_name"]
# handle the case when two or more trees have tied accuracy
# return only the first one for simplicity
best_tree_name <- best_tree_name[1]
# write a counter check progress
# add a dot to the progress txt file in the directory
write(".", file = "./progress.txt", append = T, sep = "")
# store the iteration results and the accurate_rate
additional_row <- cbind(n, k,
max(accurate_rate_table$accurate_rate),
best_tree_name) |> as.data.frame()
additional_row |> colnames() <-
c("n", "k", "accurate_rate", "tree_name")
# list together the iteration results for returning
list(additional_row, get(additional_row$tree_name))
}
# stop parallel cluster
stopCluster(cl)
# record the list results into the table for return
for (i in 1:nrow(best_tree_table)) {
best_tree_table[i,] <- best_tree_list[[i,1]]
}
# combine outputs into a large list for returning
output <- list(best_tree_table, best_tree_list[,2])
return(output)
}