-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_networks.R
285 lines (265 loc) · 9.22 KB
/
random_networks.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#' Independent random matrix
#'
#' The function creates random matrices following a uniform probability over the space of networks having exactly m fixed number of edges (Moreno and Jennings, 1938; Rapoport, 1948; Solomonoff and Rapoport, 1951; Erdos and Renyi, 1959) or following a probability of the formation of the ties (Gilbert, 1959) assuming ties independency.
#'
#' The fixed model is often called the G(n,m) graph with 'n' nodes and 'm' edges, and the 'm' edges are chosen uniformly randomly from the set of all possible ties.
#'
#' The probability model is known as the G(n,p) graph, in which the matrix has 'n' nodes, and for each tie, the probability that it is present in the matrix is 'p'.
#'
#' These are the simplest models that follow a conditional uniform distribution that place nonnull probability on a subset of networks with distinctive characteristics corresponding to the observed networks - for example, simulating a matrix based on the number of ties observed in the network.
#'
#' @param n The number of nodes of the first set
#' @param m The number of nodes of a second set
#' @param type The model assumes a fixed number of \code{edges} model (a.k.a. G(n,m)) (default) or a \code{probability} model (a.k.a. G(n,p))
#' @param digraph Whether the matrix is symmetric or not
#' @param loops Whether to expect nonzero elements in the diagonal of the matrix
#' @param l The number of ties expected for the \code{edges} (a.k.a. G(n,m)) model
#' @param p The probability of the ties expected for the \code{probability} (a.k.a. G(n,p)) model. If no parameter `p` is specified, a uniform distribution is considered (p=0.5).
#' @param trials Whether to add counting numbers to the \code{probability} (a.k.a. G(n,p)) model
#' @param multilevel Whether to return a meta-matrix to represent a multilevel network
#'
#' @return This function return the counts of the dyad census.
#'
#' @references
#'
#' Erdos, P. and Renyi, A. (1959). On random graphs. Publicationes Mathematicae 6, 290–297.
#'
#' Gilbert, N. (1959). Random Graphs. The Annals of Mathematical Statistics, 30(4): 1141-1144.
#'
#' Moreno, J. and Jennings, H. (1938). Statistics of social configurations. Sociometry, 1(3/4):342–374.
#'
#' Rapoport, A. (1948). Cycle distributions in random nets. Bulletin of Mathematical Biology, 10(3):145–157.
#'
#' Solomonoff, R. and Rapoport, A. (1951). Connectivity of random nets. Bulletin of Mathematical Biology, 13:107–117.
#'
#' @author Alejandro Espinosa-Rada
#'
#' @examples
#'
#' set.seed(18051889)
#' ind_rand_matrix(5, type = "edges", l = 3, digraph = TRUE, loops = TRUE)
#' ind_rand_matrix(5, type = "probability")
#' ind_rand_matrix(n = 5, m = 2, p = 0.20, type = "probability", multilevel = TRUE)
#' @importFrom stats rbinom
#'
#' @export
ind_rand_matrix <- function(n, m = NULL,
type = c("edges", "probability"),
digraph = TRUE, loops = FALSE,
l = NULL, p = NULL, trials = 1, multilevel = FALSE) {
type <- switch(graph_type(type),
"edges" = 1,
"probability" = 2
)
if (trials > 1) {
trials <- as.integer(trials)
}
if (type == 1) {
if (is.null(l)) {
stop("The number of fixed ties is not specified")
}
}
if (type == 2) {
if (is.null(p)) {
p <- 0.5
}
}
if (!is.null(m)) {
# TWO-MODE
if (type == 1) {
A <- matrix(0, ncol = n, nrow = m, byrow = TRUE)
while (l) {
i <- sample(m, 1)
j <- sample(n, 1)
A[i, j] <- 1
if (sum(A) == l) break
}
}
if (type == 2) {
ties <- n * m
A <- matrix(c(rbinom(ties, trials, p)),
ncol = n, nrow = m, byrow = TRUE
)
}
if (multilevel) {
# MULTILEVEL
if (digraph) {
if (!loops) {
# DIRECTED AND NO LOOPS
if (type == 1) {
B <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
while (l) {
i <- sample(n, 1)
j <- sample(n, 1)
if (i == j) next
B[i, j] <- 1
if (sum(B) == l) break
}
}
if (type == 2) {
ties <- n * (n - 1)
B <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
edges <- c(rbinom(ties, trials, p))
B[upper.tri(B, diag = FALSE)] <- edges[1:(ties / 2)]
B[lower.tri(B, diag = FALSE)] <- edges[((ties / 2) + 1):ties]
}
} else {
# DIRECTED AND LOOPS
if (type == 1) {
B <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
while (l) {
i <- sample(n, 1)
j <- sample(n, 1)
B[i, j] <- 1
if (sum(B) == l) break
}
}
if (type == 2) {
ties <- n * n
B <- matrix(c(rbinom(ties, trials, p)),
ncol = n, nrow = n, byrow = TRUE
)
}
}
} else {
if (!loops) {
# UNDIRECTED AND NO LOOPS
if (type == 1) {
B <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
while (l) {
i <- sample(n, 1)
B[upper.tri(B, diag = FALSE)][i] <- 1
print(sum(B))
if (sum(B) == l) break
}
B[lower.tri(B)] <- t(B)[lower.tri(B)]
}
if (type == 2) {
ties <- (n * (n - 1)) / 2
B <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
edges <- c(rbinom(ties, trials, p))
B[upper.tri(B, diag = FALSE)] <- edges[1:ties]
B[lower.tri(B)] <- t(B)[lower.tri(B)]
}
} else {
# UNDIRECTED AND LOOPS
if (type == 1) {
B <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
while (l) {
i <- sample(n, 1)
B[upper.tri(B, diag = TRUE)][i] <- 1
print(sum(B))
if (sum(B) == l) break
}
B[lower.tri(B)] <- t(B)[lower.tri(B)]
}
if (type == 2) {
ties <- (n * (n - 1)) / 2 + n
B <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
edges <- c(rbinom(ties, trials, p))
B[upper.tri(B, diag = TRUE)] <- edges[1:ties]
B[lower.tri(B)] <- t(B)[lower.tri(B)]
}
}
}
M <- meta_matrix(B, t(A))
rownames(M) <- c(paste("n", 1:ncol(B), sep = ""), paste("m", 1:nrow(A), sep = ""))
colnames(M) <- rownames(M)
return(M)
} else {
# ONLY TWO-MODE
return(A)
}
} else {
# ONE-MODE NETWORK
if (digraph) {
if (!loops) {
# DIRECTED AND NO LOOPS
if (type == 1) {
A <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
while (l) {
i <- sample(n, 1)
j <- sample(n, 1)
if (i == j) next
A[i, j] <- 1
if (sum(A) == l) break
}
}
if (type == 2) {
ties <- n * (n - 1)
A <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
edges <- c(rbinom(ties, trials, p))
A[upper.tri(A, diag = FALSE)] <- edges[1:(ties / 2)]
A[lower.tri(A, diag = FALSE)] <- edges[((ties / 2) + 1):ties]
}
} else {
# DIRECTED AND LOOPS
if (type == 1) {
A <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
while (l) {
i <- sample(n, 1)
j <- sample(n, 1)
A[i, j] <- 1
if (sum(A) == l) break
}
}
if (type == 2) {
ties <- n * n
A <- matrix(c(rbinom(ties, trials, p)),
ncol = n, nrow = n, byrow = TRUE
)
}
}
} else {
if (!loops) {
# UNDIRECTED AND NO LOOPS
if (type == 1) {
A <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
while (l) {
i <- sample(n, 1)
A[upper.tri(A, diag = FALSE)][i] <- 1
print(sum(A))
if (sum(A) == l) break
}
A[lower.tri(A)] <- t(A)[lower.tri(A)]
}
if (type == 2) {
ties <- (n * (n - 1)) / 2
A <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
edges <- c(rbinom(ties, trials, p))
A[upper.tri(A, diag = FALSE)] <- edges[1:ties]
A[lower.tri(A)] <- t(A)[lower.tri(A)]
}
} else {
# UNDIRECTED AND LOOPS
if (type == 1) {
A <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
while (l) {
i <- sample(n, 1)
A[upper.tri(A, diag = TRUE)][i] <- 1
print(sum(A))
if (sum(A) == l) break
}
A[lower.tri(A)] <- t(A)[lower.tri(A)]
}
if (type == 2) {
ties <- (n * (n - 1)) / 2 + n
A <- matrix(0, ncol = n, nrow = n, byrow = TRUE)
edges <- c(rbinom(ties, trials, p))
A[upper.tri(A, diag = TRUE)] <- edges[1:ties]
A[lower.tri(A)] <- t(A)[lower.tri(A)]
}
}
}
}
return(A)
}
graph_type <- function(arg, choices, several.ok = FALSE) {
if (missing(choices)) {
formal.args <- formals(sys.function(sys.parent()))
choices <- eval(formal.args[[deparse(substitute(arg))]])
}
arg <- tolower(arg)
choices <- tolower(choices)
match.arg(arg = arg, choices = choices, several.ok = several.ok)
}