-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpermutations.R
43 lines (30 loc) · 1.54 KB
/
permutations.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
# This function computes the permutations needed by aggregation.R to restore the dependences between the variables of interest.
make_permutations <- function(my_bights, residuals){
itree <- my_bights$itree
mat_residuals <- residuals
n_resid <- nrow(mat_residuals)
# compute the parsing order of the aggregate nodes
leaves <- V(itree)[degree(itree, mode="out") == 0]
agg_nodes <- V(itree)[degree(itree, mode="out") != 0]
all_nodes <- V(itree)
stopifnot(all(names(all_nodes) == colnames(residuals)))
list_matpermutations <- list_vecties <- vector("list", length(agg_nodes))
for(inode in seq_along(agg_nodes)){
agg_node <- agg_nodes[inode]
idseries_agg <- names(agg_node)
children_nodes <- ego(itree, order = 1, nodes = agg_node, mode = "out")[[1]][-1]
idchildren <- match(children_nodes, all_nodes)
mat_residuals <- residuals[, idchildren]
vec_ties <- sapply(seq(ncol(mat_residuals)), function(j){
(nrow(mat_residuals) - length(unique(mat_residuals[, j]))) / nrow(mat_residuals)
}) * 100
mat_residuals <- tail(mat_residuals, M)
mat_permutations <- apply(mat_residuals, 2, rank, ties.method = "random")
colnames(mat_permutations) <- names(children_nodes)
list_matpermutations[[inode]] <- mat_permutations
list_vecties[[inode]] <- vec_ties
}
list_matpermutations <- setNames(list_matpermutations, names(agg_nodes))
list_vecties <- setNames(list_vecties, names(agg_nodes))
list(list_matpermutations = list_matpermutations, list_vecties = list_vecties)
}