Skip to content

Commit a61cffe

Browse files
committed
Add permutation matrix
1 parent 0af7703 commit a61cffe

17 files changed

+240
-45
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export(mixed_census)
3838
export(multilevel_degree)
3939
export(multiplex_census)
4040
export(percolation_clique)
41+
export(perm_label)
42+
export(perm_matrix)
4143
export(posneg_index)
4244
export(power_function)
4345
export(q_analysis)

R/path_distances.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#' Relational Composition
22
#'
3-
#' This function return the relational composition of the given matrices. The compound relations define the paths along with social processes flows of the given matrices (Pattison, 1993). However, those whom they link may or may not be aware of them. The compound relations allows to identify "the possibly very long and devious chains of effects propagating withing concrete social systems through links of various kinds" (Lorrain & White, 1971: 50).
3+
#' This function returns the relational composition of the given matrices. The compound relations define the paths and the social process flows of the given matrices (Pattison, 1993). However, those whom they link may or may not be aware of them. The compound relations allow us to identify "the possibly very long and devious chains of effects propagating withing concrete social systems through links of various kinds" (Lorrain & White, 1971: 50).
44
#'
55
#' @param l A list of matrices.
66
#' @param comp A number with the length of paths to form the compound relation.
77
#' @param matrices Whether to return the resulting matrices of the compound relations.
88
#' @param equate Whether to return the semigroup equations.
99
#'
10-
#' @return This function provide the composition, or concatenation of compound relations and the primitives of the matrices.
10+
#' @return This function provides the composition or concatenation of compound relations and the primitives of the matrices.
1111
#'
1212
#' @references
1313
#'

R/utilities.R

Lines changed: 111 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ extract_component <- function(A, maximum = TRUE, position = NULL) {
12391239

12401240
#' Power matrix
12411241
#'
1242-
#' Power of a matrix computed by succesive matrix multiplication.
1242+
#' Power of a matrix computed by successive matrix multiplication.
12431243
#'
12441244
#' @param A A matrix
12451245
#' @param n Positive integer
@@ -1253,20 +1253,120 @@ extract_component <- function(A, maximum = TRUE, position = NULL) {
12531253
#' @author Alejandro Espinosa-Rada
12541254
#'
12551255
#' @examples
1256-
#' A <- matrix(c(1,0,0,0,
1257-
#' 1,1,0,0,
1258-
#' 1,0,1,0,
1259-
#' 0,1,1,1), byrow = TRUE, ncol = 4, nrow = 4)
1256+
#' A <- matrix(c(
1257+
#' 1, 0, 0, 0,
1258+
#' 1, 1, 0, 0,
1259+
#' 1, 0, 1, 0,
1260+
#' 0, 1, 1, 1
1261+
#' ), byrow = TRUE, ncol = 4, nrow = 4)
12601262
#' power_function(A, 1000)
1261-
#'
1263+
#'
12621264
#' @export
12631265

1264-
power_function<- function(A, n){
1266+
power_function <- function(A, n) {
12651267
return(powA(n))
12661268
}
12671269

1268-
powA <- function(n){
1269-
if (n == 1) return(A)
1270-
if (n == 2) return(A %*% A)
1271-
if (n > 2) return(A %*% powA(n-1))
1270+
powA <- function(n) {
1271+
if (n == 1) {
1272+
return(A)
1273+
}
1274+
if (n == 2) {
1275+
return(A %*% A)
1276+
}
1277+
if (n > 2) {
1278+
return(A %*% powA(n - 1))
1279+
}
1280+
}
1281+
1282+
#' Permutation matrix
1283+
#'
1284+
#' This function create permutation matrices.
1285+
#'
1286+
#' @param n The size of the square matri
1287+
#' @param m Number of permutations
1288+
#' @param unique Whether to return unique cases
1289+
#'
1290+
#' @return This function returns a list of permutation matrices
1291+
#'
1292+
#' @author Alejandro Espinosa-Rada
1293+
#'
1294+
#' @examples
1295+
#'
1296+
#' W <- matrix(c(
1297+
#' 0, 1, 0, 0, 0,
1298+
#' 0, 0, 1, 0, 0,
1299+
#' 1, 0, 0, 0, 0,
1300+
#' 0, 0, 0, 0, 1,
1301+
#' 0, 0, 0, 1, 0
1302+
#' ), byrow = TRUE, ncol = 5)
1303+
#' rownames(W) <- c("P", "Q", "R", "S", "T")
1304+
#' colnames(W) <- rownames(W)
1305+
#' perm_matrix(5, m = 1000, unique = TRUE)
1306+
#'
1307+
#' @export
1308+
1309+
perm_matrix <- function(n, m = 1, unique = FALSE) {
1310+
matrices <- list()
1311+
for (m in seq_len(m)) {
1312+
A <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
1313+
vector <- sample(NROW(A), NROW(A))
1314+
for (i in seq_len(length(vector))) {
1315+
A[vector[i], i] <- 1
1316+
}
1317+
matrices[[m]] <- A
1318+
}
1319+
1320+
if (unique) {
1321+
matrices <- unique(matrices)
1322+
# factorial(n) == length(matrices) # should reach this limit!
1323+
}
1324+
1325+
return(matrices)
1326+
}
1327+
1328+
#' Permute labels of a matrix
1329+
#'
1330+
#' This function permutes the labels of a matrix.
1331+
#'
1332+
#' @param A A matrix
1333+
#' @param m Number of permutations
1334+
#' @param unique Whether to return unique cases
1335+
#'
1336+
#' @return This function returns the permutation of labels.
1337+
#'
1338+
#' @author Alejandro Espinosa-Rada
1339+
#'
1340+
#' @examples
1341+
#'
1342+
#' W <- matrix(c(
1343+
#' 0, 1, 0, 0, 0,
1344+
#' 0, 0, 1, 0, 0,
1345+
#' 1, 0, 0, 0, 0,
1346+
#' 0, 0, 0, 0, 1,
1347+
#' 0, 0, 0, 1, 0
1348+
#' ), byrow = TRUE, ncol = 5)
1349+
#' rownames(W) <- c("P", "Q", "R", "S", "T")
1350+
#' colnames(W) <- rownames(W)
1351+
#' perm_label(W, m = 1000, unique = TRUE)
1352+
#'
1353+
#' @export
1354+
1355+
perm_label <- function(A, m = 1, unique = FALSE) {
1356+
if (is.null(rownames(A))) {
1357+
rownames(A) <- 1:NROW(A)
1358+
colnames(A) <- rownames(A)
1359+
}
1360+
label <- rownames(A)
1361+
1362+
perm <- list()
1363+
for (i in seq_len(m)) {
1364+
perm[[i]] <- label[seq_len(NROW(A)) %*% perm_matrix(NCOL(A))[[1]]]
1365+
}
1366+
1367+
if (unique) {
1368+
perm <- unique(perm)
1369+
}
1370+
1371+
return(do.call(rbind, perm))
12721372
}

README.Rmd

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,25 @@ Utilities:
7373

7474
9. `hypergraph()`: Hypergraphs
7575

76-
10. `power_function()`: Power of a matrix
76+
10. `perm_matrix()`: Permutation matrix
7777

78-
11. `meta_matrix()`: Meta matrix for multilevel networks
78+
11. `perm_label()`: Permute labels of a matrix
7979

80-
12. `minmax_overlap()`: Minimum/maximum overlap
80+
12. `power_function()`: Power of a matrix
8181

82-
13. `mix_matrix()`: Mixing matrix
82+
13. `meta_matrix()`: Meta matrix for multilevel networks
8383

84-
14. `simplicial_complexes()`: Simplicial Complexes
84+
14. `minmax_overlap()`: Minimum/maximum overlap
8585

86-
15. `structural_na()`: Structural missing data
86+
15. `mix_matrix()`: Mixing matrix
8787

88-
16. `ego_net()`: Ego network
88+
16. `simplicial_complexes()`: Simplicial Complexes
8989

90-
17. `zone_sample()`: Zone-2 sampling from second-mode
90+
17. `structural_na()`: Structural missing data
91+
92+
18. `ego_net()`: Ego network
93+
94+
19. `zone_sample()`: Zone-2 sampling from second-mode
9195

9296
Ego and personal networks:
9397

@@ -424,6 +428,8 @@ participating in this project you agree to abide by its terms.
424428

425429
- [`{multinet}`](https://CRAN.R-project.org/package=multinet)
426430

431+
- [`{muxViz}`](https://github.com/manlius/muxViz)
432+
427433
- [`{tnet}`](https://toreopsahl.com/tnet/)
428434

429435
- [`{xUCINET}`](https://www.analyzingsocialnetworksusingr.com/xucinet)

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,25 @@ Utilities:
7272

7373
9. `hypergraph()`: Hypergraphs
7474

75-
10. `power_function()`: Power of a matrix
75+
10. `perm_matrix()`: Permutation matrix
7676

77-
11. `meta_matrix()`: Meta matrix for multilevel networks
77+
11. `perm_label()`: Permute labels of a matrix
7878

79-
12. `minmax_overlap()`: Minimum/maximum overlap
79+
12. `power_function()`: Power of a matrix
8080

81-
13. `mix_matrix()`: Mixing matrix
81+
13. `meta_matrix()`: Meta matrix for multilevel networks
8282

83-
14. `simplicial_complexes()`: Simplicial Complexes
83+
14. `minmax_overlap()`: Minimum/maximum overlap
8484

85-
15. `structural_na()`: Structural missing data
85+
15. `mix_matrix()`: Mixing matrix
8686

87-
16. `ego_net()`: Ego network
87+
16. `simplicial_complexes()`: Simplicial Complexes
8888

89-
17. `zone_sample()`: Zone-2 sampling from second-mode
89+
17. `structural_na()`: Structural missing data
90+
91+
18. `ego_net()`: Ego network
92+
93+
19. `zone_sample()`: Zone-2 sampling from second-mode
9094

9195
Ego and personal networks:
9296

@@ -560,6 +564,8 @@ participating in this project you agree to abide by its terms.
560564

561565
- [`{multinet}`](https://CRAN.R-project.org/package=multinet)
562566

567+
- [`{muxViz}`](https://github.com/manlius/muxViz)
568+
563569
- [`{tnet}`](https://toreopsahl.com/tnet/)
564570

565571
- [`{xUCINET}`](https://www.analyzingsocialnetworksusingr.com/xucinet)
Loading
Loading
Loading
Loading
Loading

docs/index.html

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ pkgdown: 2.0.7
33
pkgdown_sha: ~
44
articles:
55
multilayer: multilayer.html
6-
last_built: 2023-06-02T22:04Z
6+
last_built: 2023-06-04T16:18Z
77

docs/reference/zone_sample.html

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/compound_relation.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/perm_label.Rd

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)