Construct functions to generate random community matrices
community.sampler(edges, required.groups = c(0))
edges | an edge list |
---|---|
required.groups | a vector of integers specifying which groups of edges must always occur in the community matrix. |
Returns a list with elements
community()
a function to generate a random community matrix
select(p)
a function that randomly selects which uncertain edges will be retained
weights(W)
a function that returns the (non-zero) weights as a vector
edge.labels
the labels of the edges
uncertain.labels
the labels of the uncertain edges
Given an edge list that specifies a directed graph, this function constructs a list of functions that can be use to generate random community matrices corresponding to that directed graph.
Edges in the edge list that do not fall in a required group are considered uncertain, and may or may not be represented in the community matrix.
Random community matrices are generated in two stages, the first
stage determines which of the uncertain edges will be included or
excluded in subsequent simulations, while the second stage
generates random matrices corresponding to the selected. The
select
function is a function of a single argument p
that determines which of the uncertain edge pairs will be included
in matrices generated by subsequent calls to
community
. This function always selects either neither or
both edges of a pair and every uncertain pair has likelihood
p
of being selected. The community
function is a
function of no arguments that generates a random community matrix.
The weights
function is a function of a single argument
W
that returns those entries of the community matrix
W
that correspond to edges in the edge list.
set.seed(32) ## Sample model edges <- parse.digraph(c( "E *-> D", "D *-> C", "C -> E", "E *-> B", "B *-> A", "A -> E", "D --> B")) edges <- enforce.limitation(edges) s <- community.sampler(edges) ## Force D --> B edge out s$select(0) ## Generate community matrices s$community()#> [,1] [,2] [,3] [,4] [,5] #> [1,] -0.6786304 0.1519876 0.0000000 0.0000000 0.0000000 #> [2,] -0.3419975 -0.3879250 0.0000000 0.0000000 0.7288197 #> [3,] 0.0000000 0.0000000 -0.2273639 0.5948084 0.0000000 #> [4,] 0.0000000 0.0000000 -0.3265582 -0.7337878 0.5058405 #> [5,] 0.9561873 -0.6128745 0.8087471 -0.1479377 -0.2511979s$community()#> [,1] [,2] [,3] [,4] [,5] #> [1,] -0.4828351 0.68269684 0.0000000 0.0000000 0.0000000 #> [2,] -0.5374858 -0.46007894 0.0000000 0.0000000 0.9075928 #> [3,] 0.0000000 0.00000000 -0.3882794 0.4194729 0.0000000 #> [4,] 0.0000000 0.00000000 -0.4193672 -0.6057378 0.7994494 #> [5,] 0.8908419 -0.01802125 0.6222058 -0.8612446 -0.1753905## Force D --> B edge in s$select(1) ## Generate community matrices s$community()#> [,1] [,2] [,3] [,4] [,5] #> [1,] -0.4647573 0.5198455 0.0000000 0.00000000 0.00000000 #> [2,] -0.8609805 -0.1737807 0.0000000 0.79778422 0.76826156 #> [3,] 0.0000000 0.0000000 -0.7778829 0.82850912 0.00000000 #> [4,] 0.0000000 0.0000000 -0.6144326 -0.35912376 0.91085816 #> [5,] 0.1483667 -0.2406717 0.4417894 -0.01370652 -0.03663209s$community()#> [,1] [,2] [,3] [,4] [,5] #> [1,] -0.04333347 0.7014790 0.0000000 0.00000000 0.0000000 #> [2,] -0.31793984 -0.2059869 0.0000000 0.02016695 0.9521386 #> [3,] 0.00000000 0.0000000 -0.8673946 0.06280278 0.0000000 #> [4,] 0.00000000 0.0000000 -0.1630088 -0.27759930 0.7704489 #> [5,] 0.34899865 -0.1312224 0.8110850 -0.99836768 -0.8065071## Select the uncertain D --> B edge with prob 0.6 s$select(0.6) ## Generate community matrices s$community()#> [,1] [,2] [,3] [,4] [,5] #> [1,] -0.2542593 0.4102821 0.0000000 0.0000000 0.0000000 #> [2,] -0.7762236 -0.8843759 0.0000000 0.2541118 0.9397219 #> [3,] 0.0000000 0.0000000 -0.6202625 0.8461755 0.0000000 #> [4,] 0.0000000 0.0000000 -0.9714032 -0.6190249 0.9973086 #> [5,] 0.8374537 -0.8873680 0.1108565 -0.5471697 -0.4680537s$community()#> [,1] [,2] [,3] [,4] [,5] #> [1,] -0.8748754 0.5323647 0.000000000 0.0000000 0.0000000 #> [2,] -0.6817469 -0.6709742 0.000000000 0.8604564 0.1768623 #> [3,] 0.0000000 0.0000000 -0.730709196 0.5329132 0.0000000 #> [4,] 0.0000000 0.0000000 -0.139402971 -0.9388213 0.5397287 #> [5,] 0.4521534 -0.9454003 0.008040447 -0.8093323 -0.7397858