Permute rows and columns of matrices
permute_var.RdPermute rows and columns of matrices.
Usage
permute_var(mat, perm = nrow(mat):1)
permute_synch(param, perm)Details
Given a permutation, permute_var permutes the rows and columns
of a matrix in such a way that if mat is the covariance matrix
of a vector x, then the rearranged matrix is the covariance
matrix of x[perm]. If P is the permutation matrix
corresponding to perm, then the computed value is
P %*% mat %*% t(P).
permute_synch performs the above transformation on all matrices
found in param. More precisely, if param is a matrix,
then the result is the same as for permute_var. Otherwise
param should be a list and, conceptually, permute_synch is
applied recursively on each element of this list. The net result is
that each matrix, say \(M\), in param is replaced by
\(PMP'\) and each vector, say \(v\), by \(Pv\). The idea is that
param may contain specification of a VAR model, all components
of which need to be reshuffled if the components of the multivariate
vector are permuted.
All matrices in param must have the same number of rows, say
d, but this is not checked. perm should be a permutation
of 1:d.
Value
for permute_var, a matrix,
for permute_synch, a matrix or list of the same shape as
param in which each matrix is transformed as described in
Details.
Examples
Cl <- cor(longley) # from example for 'cor()'
nc <- ncol(Cl)
v <- 1:nc
names(v) <- colnames(Cl)
permute_var(Cl)
#> Employed Year Population Armed.Forces Unemployed GNP
#> Employed 1.0000000 0.9713295 0.9603906 0.4573074 0.5024981 0.9835516
#> Year 0.9713295 1.0000000 0.9939528 0.4172451 0.6682566 0.9952735
#> Population 0.9603906 0.9939528 1.0000000 0.3644163 0.6865515 0.9910901
#> Armed.Forces 0.4573074 0.4172451 0.3644163 1.0000000 -0.1774206 0.4464368
#> Unemployed 0.5024981 0.6682566 0.6865515 -0.1774206 1.0000000 0.6042609
#> GNP 0.9835516 0.9952735 0.9910901 0.4464368 0.6042609 1.0000000
#> GNP.deflator 0.9708985 0.9911492 0.9791634 0.4647442 0.6206334 0.9915892
#> GNP.deflator
#> Employed 0.9708985
#> Year 0.9911492
#> Population 0.9791634
#> Armed.Forces 0.4647442
#> Unemployed 0.6206334
#> GNP 0.9915892
#> GNP.deflator 1.0000000
all(permute_var(Cl) == Cl[ncol(Cl):1, ncol(Cl):1])
#> [1] TRUE