|
1 |
| -#' Two-mode lattice |
| 1 | +#' Two-mode chain |
2 | 2 | #'
|
3 |
| -#' Creates a two-mode lattice |
4 |
| -#' @param node1 Number of nodes in the first node set |
5 |
| -#' @param node2 Number of nodes in the second node set |
| 3 | +#' Creates a two-mode chain |
| 4 | +#' @param n1 Number of nodes in the first node set |
| 5 | +#' @param n2 Number of nodes in the second node set |
| 6 | +#' @param as What type of object to return. |
| 7 | +#' One of "matrix", "tbl_graph", "igraph". |
| 8 | +#' By default, creates a "tbl_graph" object. |
6 | 9 | #' @details Will construct a bilateral lattice,
|
7 | 10 | #' with two ties for every second-mode node.
|
8 | 11 | #' @export
|
9 | 12 | #' @examples
|
10 | 13 | #' \dontrun{
|
11 |
| -#' create_lattice(10, 12) |
| 14 | +#' create_chain(5,10) %>% ggraph() + |
| 15 | +#' geom_edge_fan(aes(alpha = stat(index)), show.legend = FALSE) + |
| 16 | +#' geom_node_point(aes(size = 5)) |
12 | 17 | #' }
|
13 |
| -create_lattice <- function(node1, node2) { |
14 |
| - mat <- matrix(0, node1, node2) |
| 18 | +create_chain <- function(n1, n2, |
| 19 | + as = c("tbl_graph", "igraph", "matrix")) { |
| 20 | + |
| 21 | + mat <- matrix(0, n1, n2) |
15 | 22 | out <- suppressWarnings(((row(mat) - col(mat)) == 0 |
|
16 |
| - (row(mat) - col(mat)) == (-seq.int(0, node2 - 1, node1)[-1]) | |
| 23 | + (row(mat) - col(mat)) == (-seq.int(0, n2 - 1, n1)[-1]) | |
17 | 24 | (row(mat) - col(mat)) == -1 |
|
18 |
| - (row(mat) - col(mat)) == -seq.int(1 + node1, node2 - 1, node1) | |
| 25 | + (row(mat) - col(mat)) == -seq.int(1 + n1, n2 - 1, n1) | |
19 | 26 | (row(mat) - col(mat)) == nrow(mat) - 1) * 1)
|
| 27 | + |
| 28 | + as <- match.arg(as) |
| 29 | + if(as == "tbl_graph") out <- tidygraph::as_tbl_graph(out) |
| 30 | + if(as == "igraph") out <- igraph::graph_from_incidence_matrix(out) |
20 | 31 | out
|
21 | 32 | }
|
22 | 33 |
|
23 |
| -#' Two-mode random graph |
| 34 | +#' Matched two-mode graph |
24 | 35 | #'
|
25 |
| -#' Creates a random two-mode network |
26 |
| -#' @param node1 Number of nodes in the first node set |
27 |
| -#' @param node2 Number of nodes in the second node set |
28 |
| -#' @param density Number of edges in the network over the number of edges possible |
| 36 | +#' Creates a matched two-mode network |
| 37 | +#' @param n1 Number of nodes in the first node set |
| 38 | +#' @param n2 Number of nodes in the second node set |
29 | 39 | #' @details Will construct an affiliation matrix,
|
30 |
| -#' with a random probability of a tie. |
| 40 | +#' with by default both n1 and n2 matched. |
| 41 | +#' TODO: Incorporate into create_chain (chordal_ring of certain breadth w). |
31 | 42 | #' @export
|
32 | 43 | #' @examples
|
33 | 44 | #' \dontrun{
|
34 |
| -#' create_random(10, 12, 0.25) |
| 45 | +#' create_match(10, 12) |
35 | 46 | #' }
|
36 |
| -create_random <- function(node1, node2, density) { |
37 |
| - mat <- matrix(rbinom(node1 * node2, 1, density), node1, node2) |
| 47 | +create_match <- function(n1, n2, |
| 48 | + as = c("tbl_graph", "igraph", "matrix")) { |
| 49 | + mat <- matrix(0, n1, n2) |
| 50 | + mat <- matrix( |
| 51 | + ((row(mat) - col(mat)) %in% unique(-seq(0, n2 - 1, n1), |
| 52 | + seq(0, n1 - 1, n2))) * 1, |
| 53 | + n1, n2 |
| 54 | + ) |
| 55 | + |
| 56 | + as <- match.arg(as) |
| 57 | + if(as == "tbl_graph") mat <- tidygraph::as_tbl_graph(mat) |
| 58 | + if(as == "igraph") mat <- igraph::graph_from_incidence_matrix(mat) |
38 | 59 | mat
|
39 | 60 | }
|
40 | 61 |
|
41 |
| -#' Two-component two-mode graph |
| 62 | +#' Two-mode random graph |
42 | 63 | #'
|
43 |
| -#' Creates a two-component two-mode network |
44 |
| -#' @param node1 Number of nodes in the first node set |
45 |
| -#' @param node2 Number of nodes in the second node set |
| 64 | +#' Creates a random two-mode network |
| 65 | +#' @param n1 Number of nodes in the first node set |
| 66 | +#' @param n2 Number of nodes in the second node set |
| 67 | +#' @param p Number of edges in the network over the number of edges possible |
| 68 | +#' @param m Number of edges in the network |
| 69 | +#' @param as What type of object to return. |
| 70 | +#' One of "matrix", "tbl_graph", "igraph". |
| 71 | +#' By default, creates a "tbl_graph" object. |
46 | 72 | #' @details Will construct an affiliation matrix,
|
47 |
| -#' with full component diagonal. |
| 73 | +#' with a random probability of a tie. |
48 | 74 | #' @export
|
49 | 75 | #' @examples
|
50 | 76 | #' \dontrun{
|
51 |
| -#' create_poles(10, 12) |
| 77 | +#' play_twomode(10, 12, 0.25) %>% ggraph() + |
| 78 | +#' geom_edge_fan(aes(alpha = stat(index)), show.legend = FALSE) + |
| 79 | +#' geom_node_point(aes(size = 5)) |
52 | 80 | #' }
|
53 |
| -create_poles <- function(node1, node2) { |
54 |
| - mat <- matrix(0, node1, node2) |
55 |
| - mat[1:round(node1 / 2), 1:round(node2 / 2)] <- 1 |
56 |
| - mat[rowSums(mat) < 1, colSums(mat) < 1] <- 1 |
57 |
| - mat |
| 81 | +play_twomode <- function(n1, n2, p, m, directed = TRUE, mode = "out", |
| 82 | + as = c("tbl_graph", "igraph", "matrix")) { |
| 83 | + |
| 84 | + g <- tidygraph::play_bipartite(n1, n2, p, m, directed, mode) |
| 85 | + |
| 86 | + as <- match.arg(as) |
| 87 | + if(as == "igraph" | as == "matrix") g <- as.igraph(g) |
| 88 | + if(as == "matrix") g <- as_adjacency_matrix(g) |
| 89 | + g |
58 | 90 | }
|
59 | 91 |
|
60 |
| -#' Matched two-mode graph |
| 92 | +#' Two-component two-mode graph |
61 | 93 | #'
|
62 |
| -#' Creates a matched two-mode network |
63 |
| -#' @param node1 Number of nodes in the first node set |
64 |
| -#' @param node2 Number of nodes in the second node set |
| 94 | +#' Creates a two-component two-mode network |
| 95 | +#' @param n1 Number of nodes in the first node set |
| 96 | +#' @param n2 Number of nodes in the second node set |
65 | 97 | #' @details Will construct an affiliation matrix,
|
66 |
| -#' with by default both node1 and node2 matched. |
67 |
| -#' TODO: Make node set matching optional. |
| 98 | +#' with full component diagonal. |
| 99 | +#' TODO: Allow specfication of how many silos/components to create |
68 | 100 | #' @export
|
69 | 101 | #' @examples
|
70 | 102 | #' \dontrun{
|
71 |
| -#' create_match(10, 12) |
| 103 | +#' create_silos(10, 12) |
72 | 104 | #' }
|
73 |
| -create_match <- function(node1, node2) { |
74 |
| - mat <- matrix(0, node1, node2) |
75 |
| - mat <- matrix( |
76 |
| - ((row(mat) - col(mat)) %in% unique(-seq(0, node2 - 1, node1), |
77 |
| - seq(0, node1 - 1, node2))) * 1, |
78 |
| - node1, node2 |
79 |
| - ) |
| 105 | +create_silos <- function(n1, n2, |
| 106 | + as = c("tbl_graph", "igraph", "matrix")) { |
| 107 | + mat <- matrix(0, n1, n2) |
| 108 | + mat[1:round(n1 / 2), 1:round(n2 / 2)] <- 1 |
| 109 | + mat[rowSums(mat) < 1, colSums(mat) < 1] <- 1 |
| 110 | + |
| 111 | + as <- match.arg(as) |
| 112 | + if(as == "tbl_graph") mat <- tidygraph::as_tbl_graph(mat) |
| 113 | + if(as == "igraph") mat <- igraph::graph_from_incidence_matrix(mat) |
80 | 114 | mat
|
81 | 115 | }
|
82 | 116 |
|
83 | 117 | #' Nested two-mode graph
|
84 | 118 | #'
|
85 | 119 | #' Creates a nested two-mode network
|
86 |
| -#' @param node1 Number of nodes in the first node set |
87 |
| -#' @param node2 Number of nodes in the second node set |
| 120 | +#' @param n1 Number of nodes in the first node set |
| 121 | +#' @param n2 Number of nodes in the second node set |
88 | 122 | #' @details Will construct an affiliation matrix,
|
89 |
| -#' with decreasing fill across node2. |
90 |
| -#' TODO: Make node set matching optional. |
| 123 | +#' with decreasing fill across n2. |
91 | 124 | #' @export
|
92 | 125 | #' @examples
|
93 | 126 | #' \dontrun{
|
94 | 127 | #' create_nest(10, 12)
|
95 | 128 | #' }
|
96 |
| -create_nest <- function(node1, node2) { |
97 |
| - mat <- matrix(0, node1, node2) |
98 |
| - mat[(row(mat) - col(mat)) >= 0] <- 1 |
99 |
| - mat |
| 129 | +create_nest <- function(n1, n2, |
| 130 | + as = c("tbl_graph", "igraph", "matrix")) { |
| 131 | + |
| 132 | + as <- match.arg(as) |
| 133 | + |
| 134 | + out <- matrix(0, n1, n2) |
| 135 | + out[(row(out) - col(out)) >= 0] <- 1 |
| 136 | + |
| 137 | + if(as == "tbl_graph") out <- tidygraph::as_tbl_graph(out) |
| 138 | + if(as == "igraph") out <- igraph::graph_from_incidence_matrix(out) |
| 139 | + out |
100 | 140 | }
|
101 | 141 |
|
102 | 142 | # mat.dist <- matrix(0,5,3)
|
|
0 commit comments