-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
example-boot.R
62 lines (42 loc) · 1.18 KB
/
example-boot.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
library(netdiffuseR)
library(Matrix) # If you want to use sparse matrices
data("sampson", package="ergm")
samplike
# Random net
x <- as.matrix(samplike)
# Network boot1
ans <- bootnet(x, function(w, idx) {
nedges(w)/(
nvertices(w) * (1 - nvertices(w))
)
}, R=100)
hist(ans)
# Extracting the group (to include in permutation)
library(network)
grp <- samplike %v% "group"
# Multiple statistics
# Density, graph reciprocity, mean in/outdegree, triadic closure
mystats <- function(i, idx) {
# To make it easier
i <- as.matrix(i)
# Density df n1(n1-1) + n2(n2-2) - 2
n <- nnodes(i)
dens <- sum(i)/(n * (n-1))
# Outdegree
outd <- mean(rowSums(i)) # The df n1 + n2 - 2
# Reciprocity
rec <- igraph::reciprocity(igraph::graph_from_adjacency_matrix(i))
# Rebuild the network object
net <- network(
i,
vertex.attr = list(
group = grp[idx] # Notice the idx, this puts the attribute grp in the right order
)
)
triads <- ergm::summary_formula(net ~ ttriad + nodematch("group"))
c(Density = dens, "Avg Outdegree" = outd, Reciprocity = rec, "Balance" = triads)
}
ans2 <- bootnet(x, mystats, R=100)
# Actual samples
ans2$boot$t
ans2$var_t