# NOT RUN {
## A baby example to see how group() works
A <- matrix(1:12, ncol = 3)
lst <- list(1, list(2:3))
group(A, indices = lst) # split the matrix according to the grouping given by lst
## Some calls of zenpath()
zenpath(10) # integer argument
## Note that the result is of length 50 > 10 choose 2 as the underlying graph has to
## be even (and thus edges are added here)
(zp <- zenpath(c(3, 5), method = "eulerian.cross")) # integer(2) argument
## Extract the first and last three pairs of indices
extract_pairs(zp, n = 3)
## A more sophisticated example
nVars <- 5 # number of variables involved
set.seed(271)
x <- runif(nVars*(nVars-1)/2) # weights
## Construct the pairs
pairs <- expand.grid(1:nVars, 1:nVars)[,2:1]
pairs <- pairs[pairs[,1] < pairs[,2],]
pairs <- matrix(unlist(pairs), ncol = ncol(pairs))
stopifnot(length(x) == nrow(pairs)) # sanity check
## Manually compute the result of method = "strictly.weighted" and group the pairs
## 1) Sort pairs according to the weights x
(pairs. <- pairs[order(x, decreasing = TRUE),])
## 2) Now go through the rows and determine the sequence of adjacent pairs
## which can be plotted with a zenplot
res <- list(c(5,3,1),
c(3,2,5),
c(4,1,5),
c(1,2),
c(5,4,3),
c(2,4))
## Call zenpath() and check whether we get the same
(zp <- connect_pairs(zenpath(x, pairs = pairs, method = "strictly.weighted")))
stopifnot(identical(zp, lapply(res, as.integer)))
## Extract the first and last three pairs of indices
(ezp <- extract_pairs(zp, n = 3))
## Another example based on a matrix of (trivial) weights
## This also shows that an input matrix 'x' does not have to
## be symmetric. In that case, the lower triangular matrix is used.
d <- 10
x <- matrix(1, nrow = d, ncol = d)
k <- 1
for(j in 1:(d-1)) {
for(i in (j+1):d) {
x[i,j] <- k
k <- k+1
}
}
x
## Compute the 'strictly.weighted' zenpath (all pairs sorted in decreasing order)
k <- 10 # bottom and top number of pairs (k most extreme pairs)
zpath <- zenpath(x, method = "strictly.weighted") # compute path over all pairs (decreasing weights)
stopifnot(sapply(1:length(zpath), function(i) x[zpath[[i]][1], zpath[[i]][2]]) ==
45:1) # check
zpath <- connect_pairs(zpath) # connect the pairs
zp <- extract_pairs(zpath, n = c(3, 0)) # grab out the top three pairs
# }
Run the code above in your browser using DataLab