Learn R Programming

zenplots (version 0.0-1)

zenpath: (Group) Indices to Subset a Matrix for a Zenplot

Description

Constructing (possibly grouped) indices which can be used to subset a data matrix for plotting via a zenplot.

Usage

zenpath(x, pairs = NULL,
        method = c("front.loaded", "back.loaded", "balanced",
                   "eulerian.cross", "eulerian.weighted", "strictly.weighted"),
        decreasing = TRUE)
extract_pairs(x, n)
connect_pairs(x, duplicate.rm = FALSE)
group(x, indices)

Arguments

x

for

zenpath:

for method

"front.loaded":

single integer.

"back.loaded":

as for method = "front.loaded".

"balanced":

as for method = "front.loaded".

"eulerian.cross":

two integers representing the group sizes.

"eulerian.weighted":

numeric vector (or matrix or distance matrix).

"strictly.weighted":

as for method = "eulerian.weighted".

extract_pairs:

the path, a vector or list of indices of the variables to be plotted.

connect_pairs:

two-column matrix or a list containing vectors of length two.

group:

matrix (or an object convertible to such via as.matrix()).

pairs

two-column matrix containing (row-wise) the pairs of variables to be sorted according to the weights. pairs is only used for methods eulerian.weighted, strictly.weighted.

method

character string indicating the sorting method to be used. Available are:

"front.loaded":

sort all pairs such that the first variables appear the most frequently early in the sequence.

"back.loaded":

sort all pairs such that the later variables appear the most frequently later in the sequence.

"balanced":

sort all pairs such that all variables appear in balanced blocks throughout the sequence (a Hamiltonian Decomposition).

"eulerian.cross":

generate a sequence of pairs such that each is formed with one variable from each group.

"eulerian.weighted":

sort all pairs according to a greedy (heuristic) Euler path visiting each edge precisely once.

"strictly.weighted":

this method strictly respects the order given by the weights.

decreasing

logical indicating whether the sorting is done according to increasing or decreasing weights.

n

vector of length two giving the number of pairs to extract from the path x (if NULL, all pairs are returned (nothing extracted); if of length one, it is replicated) The first number corresponds to the beginning of the path, the second to the end; at least one of the two numbers should be >= 1.

duplicate.rm

logical indicating whether equal pairs (up to permutation) are omitted.

indices

list of vectors of indices according to which x is grouped.

Value

zenpath() returns a sequence of variables (indices or names, possibly a list of such), which can then be used to index the data (via group()) for plotting via zenplot().

extract_pairs() returns an object of the same type as the input x but (possibly) shortened. It extracts the first/last so-many pairs of x.

connect_pairs() returns a list of (possibly connected) pairs, so a list of vectors of length at least 2.

group() returns a list of (grouped) matrices. This is then typically passed on to zenplot().

See Also

zenplot() which provides the zenplot.

Examples

Run this code
# 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