Title: | Inference for a Generalised SBM with a Split Merge Sampler |
---|---|
Description: | Inference in a Bayesian framework for a generalised stochastic block model. The generalised stochastic block model (SBM) can capture group structure in network data without requiring conjugate priors on the edge-states. Two sampling methods are provided to perform inference on edge parameters and block structure: a split-merge Markov chain Monte Carlo algorithm and a Dirichlet process sampler. Green, Richardson (2001) <doi:10.1111/1467-9469.00242>; Neal (2000) <doi:10.1080/10618600.2000.10474879>; Ludkin (2019) <arXiv:1909.09421>. |
Authors: | Matthew Ludkin [aut, cre, cph] |
Maintainer: | Matthew Ludkin <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.1 |
Built: | 2025-03-13 03:46:57 UTC |
Source: | https://github.com/cran/SBMSplitMerge |
propsbm
with the acceptance probability alphaaccept propsbm
with the acceptance probability alpha
accept(currsbm, propsbm, edges, sbmmod, logjac = 0, logu = 0, ...)
accept(currsbm, propsbm, edges, sbmmod, logjac = 0, logu = 0, ...)
currsbm |
current |
propsbm |
proposed |
edges |
an |
sbmmod |
an |
logjac |
log Jacobian of transformation of variables |
logu |
log density for auxiliary variables |
... |
additional arguments to pass to |
updated sbm
object
proposes adding an empty block labelled kappa+
1 to sbm
addblock(sbm, edges, sbmmod, rho = 1)
addblock(sbm, edges, sbmmod, rho = 1)
sbm |
the current state of the sampler |
edges |
an |
sbmmod |
an |
rho |
probability of choosing to add a block |
an updated sbm
object
Calculate the Adjusted Rand Index between two clusterings
ARI(z, truez)
ARI(z, truez)
z |
input vector |
truez |
reference vector |
Adjusted Rand Index of z
against truez
ARI(c(1,1,2,2,3,3), c(2,2,1,1,3,3)) ## 1 - doesn't care for labels ARI(c(1,1,2,2,3,3), c(1,1,1,1,2,2)) ## 0.444
ARI(c(1,1,2,2,3,3), c(2,2,1,1,3,3)) ## 1 - doesn't care for labels ARI(c(1,1,2,2,3,3), c(1,1,1,1,2,2)) ## 0.444
converts x
to a matrix of block assignments
blockmat(x, ...)
blockmat(x, ...)
x |
object for dispatch |
... |
additional arguments for method |
matrix of block assignment indicators
blockmat.sbm
blockmat.blocks
blockmat.numeric
converts block assignments of a blocks
object to a matrix of block assignments
## S3 method for class 'blocks' blockmat(blocks, kappa)
## S3 method for class 'blocks' blockmat(blocks, kappa)
blocks |
a |
kappa |
number of blocks in matrix |
matrix with kappa
rows and a 1 at (k,i)
if node i
is in block k
under blocks
converts a vector of block assignments to a matrix of block assignments
## S3 method for class 'numeric' blockmat(x, kappa) ## S3 method for class 'factor' blockmat(x, kappa)
## S3 method for class 'numeric' blockmat(x, kappa) ## S3 method for class 'factor' blockmat(x, kappa)
x |
a numeric-vector of node-to-block assignments |
kappa |
number of blocks |
matrix with kappa
rows and a 1 at (k,i)
if node i
is in block k
under x
converts block assignments of an sbm
object to a matrix of block assignments
## S3 method for class 'sbm' blockmat(SBM, kappa)
## S3 method for class 'sbm' blockmat(SBM, kappa)
SBM |
an |
kappa |
number of blocks in matrix |
matrix with kappa
rows and a 1 at (k,i)
if node i
is in block k
under SBM
create a blockmod
object
blockmod(fixkappa, logd, dcond, r, ...)
blockmod(fixkappa, logd, dcond, r, ...)
fixkappa |
Logical - is kappa fixed or can it vary under the model? |
logd |
|
dcond |
|
r |
|
... |
parameters of the model for use in |
A block model is a probability model for a blocks
object.
This class creates a closure with three functions:
- a random method for sampling block a structure from the model with n
nodes; a
- a log-density method for computing the log-density of a given block structure in a blocks
object
- a conditional density function that takes a blocks
object and a node i
a blockmod
object
create a blocks object
blocks(z, kappa)
blocks(z, kappa)
z |
vector of block labels for each node |
kappa |
maximum number of blocks |
stores the block allocations and total number of blocks for a stochastic block model
a blocks
object
## Assign six nodes to four blocks: b <- blocks(c(1,1,2,3,4,4), 4) print(b) plot(b) ## shows id two nodes are members of the same block
## Assign six nodes to four blocks: b <- blocks(c(1,1,2,3,4,4), 4) print(b) plot(b) ## shows id two nodes are members of the same block
plot a trace of the blocks from MCMC samples
blocktrace(postz, burnin)
blocktrace(postz, burnin)
postz |
output from sampler |
burnin |
which iterations to plot? defaults to all. |
'ggplot2' object
A blockmod
for the Chinese restaurant process (CRP)
crp(gamma)
crp(gamma)
gamma |
concentration parameter |
The CRP posits that each node arrives in turn. The first node joins the first block. Each subsequent node starts a new block with probability 'gamma' or joins an existing block proportional to the block size.
a block model representing a CRP(gamma)
distribution
## simulate from a CRP(5) prior m <- crp(5) print(m) m$r(10)
## simulate from a CRP(5) prior m <- crp(5) print(m) m$r(10)
Density of Dirichlet distribution
ddirichlet(x, gam, log = FALSE)
ddirichlet(x, gam, log = FALSE)
x |
random variable in the d-dimensional simplex |
gam |
a length K concentration parameter |
log |
return the log-probability instead? |
the density
g <- rep(2,5) p <- rdirichlet(1, g) ## a length-5 probability vector ddirichlet(p, g)
g <- rep(2,5) p <- rdirichlet(1, g) ## a length-5 probability vector ddirichlet(p, g)
Compute the probability density for an edges
object
dedges(x, edges, edgemod, na.rm = TRUE, ...)
dedges(x, edges, edgemod, na.rm = TRUE, ...)
x |
an R object for dispatch |
edges |
an |
edgemod |
an |
na.rm |
remove NAs when calculating? |
... |
additional arguments |
matrix same size as edges$E
with density of each edge
likelihood of edges
## S3 method for class 'numeric' dedges(x, edges, edgemod, na.rm = na.rm, ...)
## S3 method for class 'numeric' dedges(x, edges, edgemod, na.rm = na.rm, ...)
x |
a matrix of parameters (with same size as |
edges |
an |
edgemod |
an |
na.rm |
remove NAs when calculating? |
... |
additional arguments passed to |
likelihood of edges under the edgemod
using parameters in matrix pmat
Compute the probability density for an edges
object under an sbm
object
## S3 method for class 'sbm' dedges(x, edges, edgemod, na.rm = TRUE, ...)
## S3 method for class 'sbm' dedges(x, edges, edgemod, na.rm = TRUE, ...)
x |
an |
edges |
an |
edgemod |
an |
na.rm |
remove NAs when calculating? |
... |
additional arguments for |
matrix same size as edges$E
with density of each edge
## make an sbm model, sample data then plot and print: model <- sbmmod(dma(2,5), param_beta(1,1,1,1), edges_bern()) s <- model$r(100) e <- redges(s, model$edge) dedges(s, e, model$edge)
## make an sbm model, sample data then plot and print: model <- sbmmod(dma(2,5), param_beta(1,1,1,1), edges_bern()) s <- model$r(100) e <- redges(s, model$edge) dedges(s, e, model$edge)
proposes deleting an empty block (chosen at random among empty Blocks)
delblock(sbm, edges, sbmmod, rho = 1)
delblock(sbm, edges, sbmmod, rho = 1)
sbm |
the current state of the sampler |
edges |
an |
sbmmod |
an |
rho |
probability of choosing to add a block |
an updated sbm
object
A blockmod
for Dirichlet Multinomial Allocation (DMA)
dma(gamma, delta)
dma(gamma, delta)
gamma |
parameter for Dirichlet component |
delta |
parameter for Poison component |
This model posits:
a block model representing a dma(gamma, delta)
distribution
## simulate from a DMA(2, 5) prior ## This models the `number of blocks-1` as Poisson(5) ## and block assignments as Dirichlet-Multinomial(2, 2, ...) m <- dma(2, 5) print(m) m$r(10)
## simulate from a DMA(2, 5) prior ## This models the `number of blocks-1` as Poisson(5) ## and block assignments as Dirichlet-Multinomial(2, 2, ...) m <- dma(2, 5) print(m) m$r(10)
Draw block membership in a Dirichlet process sampler
drawblock.dp(i, currsbm, edges, sbmmod)
drawblock.dp(i, currsbm, edges, sbmmod)
i |
node to update |
currsbm |
current |
edges |
an |
sbmmod |
an |
sample a new block assignment for i under a Dirichlet process.
Care needs to be taken with singleton blocks to update the parameter model in currsbm
.
updated sbm
object
For full algorithm details see http://doi.org/10.17635/lancaster/thesis/296
Reassign node 'i' to the current set of blocks given the current number of blocks and the other block assignments
drawblock.gibbs(i, currsbm, edges, sbmmod)
drawblock.gibbs(i, currsbm, edges, sbmmod)
i |
the node to reassign |
currsbm |
an |
edges |
an |
sbmmod |
an |
updated sbm
object with new block assignment for i
Draw block memberships in a Dirichlet process sampler
drawblocks.dp(currsbm, edges, sbmmod)
drawblocks.dp(currsbm, edges, sbmmod)
currsbm |
current |
edges |
an |
sbmmod |
an |
iteratively updates the block assignment of each node using a Dirichlet process update move
updated sbm
object
Sweep through the set of nodes and reassign to the current set of blocks given the current number of blocks
drawblocks.gibbs(currsbm, edges, sbmmod)
drawblocks.gibbs(currsbm, edges, sbmmod)
currsbm |
an |
edges |
an |
sbmmod |
an |
updated sbm
object with new block assignments
Simulate parameters for the given model with a Metropolis-Hastings step
drawparams(sbm, edges, sbmmod, sigma = 0.1)
drawparams(sbm, edges, sbmmod, sigma = 0.1)
sbm |
current |
edges |
an |
sbmmod |
an |
sigma |
parameter for |
iterate through the parameters in currsbm
and update.
updated sbm
object
A class with a random and density method for edges
objects
edgemod(logd, r, ...)
edgemod(logd, r, ...)
logd |
function(e, p) to calculate likelihood of edge an edge e given parameter array p |
r |
function(p) - simulate an edge given a parameter p (optional) |
... |
additional arguments to append to |
an edgemod
object
the parameter for logd
is an array of c(dimension of theta, dim(E)) e.g. from parammat
edges_bern
edges_pois
edges_norm
A class to hold edge data
edges(e, sym, loops, ...)
edges(e, sym, loops, ...)
e |
a matrix or array representing the raw edge-state data |
sym |
is the network symmetric? ( |
loops |
does the network contain self-loops? (edges from node i to i) |
... |
additional arguments to append to edges internal list |
an edges object
## make an sbm model, sample data then plot and print: model <- sbmmod(dma(2,5), param_beta(1,1,1,1), edges_bern()) s <- model$r(100) e <- redges(s, model$edge) plot(e) plot(e, s) print(e)
## make an sbm model, sample data then plot and print: model <- sbmmod(dma(2,5), param_beta(1,1,1,1), edges_bern()) s <- model$r(100) e <- redges(s, model$edge) plot(e) plot(e, s) print(e)
Make an edgemod
model with Bernoulli edge-states
edges_bern(...)
edges_bern(...)
... |
additional parameters to pass to |
an edgemod
eb <- edges_bern() ## makes `eb` an edgemod for Bernoulli edge-states
eb <- edges_bern() ## makes `eb` an edgemod for Bernoulli edge-states
Make an edgemod
model with Negative-Binomial edge-states
edges_nbin(...)
edges_nbin(...)
... |
additional parameters to pass to |
an edgemod
enb <- edges_nbin() ## makes `enb` an edgemod for Negative-Binomial edge-states
enb <- edges_nbin() ## makes `enb` an edgemod for Negative-Binomial edge-states
Make an edgemod
model with Normal edge-states
edges_norm(...)
edges_norm(...)
... |
additional parameters to pass to |
an edgemod
en <- edges_norm() ## makes `en` an edgemod for Normal edge-states
en <- edges_norm() ## makes `en` an edgemod for Normal edge-states
Make an edgemod
model with Poisson edge-states
edges_pois(...)
edges_pois(...)
... |
additional parameters to pass to |
an edgemod
ep <- edges_pois() ## makes `ep` an edgemod for Poisson edge-states
ep <- edges_pois() ## makes `ep` an edgemod for Poisson edge-states
igraph
using the script in data-rawA data set of counts of emails between email addresses This is a non-symmetric network. Nodes represent email address. The edge-state ij between two email addresses i and j is the number of emails sent from i to j The Groups vector is the node label from the igraph attribute "notes"
Enron
Enron
A list containing
an edges object with each edge-state representing the number of emails between two email addresses
A vector giving a group name to which the email address belong. The order matches the edges such that Edges[i,j] is the edge-state between the nodes i and nodes j who are members of Groups[i] and Groups[j] respectively
https://cran.r-project.org/package=igraphdata
get a set of evaluation plots from MCMC samples
eval_plots(output, burnin, theta_index)
eval_plots(output, burnin, theta_index)
output |
from sampler |
burnin |
burn-in period (a vector of iteration numbers to subset outputs) |
theta_index |
which set of thetas to plot? |
list of ggplot objects (with descriptive names)
igraph
using the script in data-rawThe Macaque data set as extracted from igraph
using the script in data-raw
Macaque
Macaque
An edges
object of activation counts between brain regions in a Macaque
igraph
calculate the marginal likelihood for a node for samplers using conjugate models
marglike_bern(znoi, ei, parammod)
marglike_bern(znoi, ei, parammod)
znoi |
a matrix of block assignments without node i |
ei |
edge-states incident to i |
parammod |
a |
log-probability of node i belonging to each block
calculate the marginal likelihood for a node for samplers using conjugate models
marglike_norm(znoi, ei, parammod)
marglike_norm(znoi, ei, parammod)
znoi |
a matrix of block assignments without node i |
ei |
edge-states incident to i |
parammod |
a |
log-probability of node i belonging to each block
calculate the marginal likelihood for a node for samplers using conjugate models
marglike_pois(znoi, ei, parammod)
marglike_pois(znoi, ei, parammod)
znoi |
a matrix of block assignments without node i |
ei |
edge-states incident to i |
parammod |
a |
log-probability of node i belonging to each block
Merge-move using an average to merge parameters
mergeavg(sbm, edges, sbmmod, ...)
mergeavg(sbm, edges, sbmmod, ...)
sbm |
the current state of the sampler |
edges |
an |
sbmmod |
an |
... |
additional parameter to 'accept' |
the blocks are chosen at random, the nodes reassigned to the block with the smallest index, then the parameters are combined using the average on the transformed scale
an updated sbm
object
merge move block merging
mergeblocks(currblocks, propparams, edges, sbmmod, k, l)
mergeblocks(currblocks, propparams, edges, sbmmod, k, l)
currblocks |
current blocks |
propparams |
proposed parameters |
edges |
an |
sbmmod |
an |
k |
Blocks to merge |
l |
Blocks to merge |
list(proposed block structure, log-acceptance-prob)
merge parameters
mergeparams(x, ...)
mergeparams(x, ...)
x |
an object to dispatch on |
... |
additional arguments for methods |
merged parameters from x
mergeparams.default
mergeparams.numeric
Merge step: parameters
## Default S3 method: mergeparams(params, k, l, parammod)
## Default S3 method: mergeparams(params, k, l, parammod)
params |
a |
k |
Blocks to merge |
l |
Blocks to merge |
parammod |
a |
list(proposed_params, log-acceptance-prob)
Merge step - parameter merging
## S3 method for class 'numeric' mergeparams(thetak, thetal, x, parammod)
## S3 method for class 'numeric' mergeparams(thetak, thetal, x, parammod)
thetak , thetal
|
parameters to merge |
x |
auxiliary parameter |
parammod |
a |
list(proposed_params, log-acceptance-prob)
modal block assignments from MCMC samples
modeblocks(postz)
modeblocks(postz)
postz |
output from sampler |
a blocks object with the modal block assignments under postz
A blockmod
for Multinomial allocation
multinom(gamma, kappa)
multinom(gamma, kappa)
gamma |
parameter for Dirichlet component |
kappa |
the number of blocks |
This model posits that: for i=1:n
where
a block model representing a Multinomial(gamma)
distribution
## A fixed number of blocks with multinomial assignment of nodes m <- multinom(1, 4) print(m) m$r(10) ## simulate a blocks object with 10 nodes
## A fixed number of blocks with multinomial assignment of nodes m <- multinom(1, 4) print(m) m$r(10) ## simulate a blocks object with 10 nodes
Calculate the likelihood of a nod belonging to each of block
nodelike(blocks, params, edges, i, sbmmod, ...)
nodelike(blocks, params, edges, i, sbmmod, ...)
blocks |
an |
params |
an |
edges |
an |
i |
the node of interest |
sbmmod |
an |
... |
additional arguments for |
the number of blocks considered is either the number of blocks in sbm (kappa)
or kappa+1
when sbmmod
has a variable number of blocks.
care is taken for data which is directed and with loops.
likelihood of edges emanating from node i
plot a trace of the number of blocks from MCMC samples
numblockstrace(postk, burnin)
numblockstrace(postk, burnin)
postk |
output from sampler |
burnin |
which iterations to plot? defaults to all. |
'ggplot2' object
A parammod
with beta-distributed parameters
param_beta(a0, a1, b0, b1)
param_beta(a0, a1, b0, b1)
a0 |
|
a1 |
|
b0 |
|
b1 |
|
This model represents a prior on theta with:
for k = 1 ... kappa
a parammod
## theta0 ~ Beta(1,9); thetak ~ Beta(9,1) pb <- param_beta(1,9,9,1) pb$r(5) ## a draw with 5 within-block parameters
## theta0 ~ Beta(1,9); thetak ~ Beta(9,1) pb <- param_beta(1,9,9,1) pb$r(5) ## a draw with 5 within-block parameters
A parammod
with gamma-distributed parameters
param_gamma(a0, a1, b0, b1)
param_gamma(a0, a1, b0, b1)
a0 |
|
a1 |
|
b0 |
|
b1 |
|
This model represents a prior on theta with:
for k = 1 ... kappa
a parammod
## theta0 ~ Gamma(1,1); thetak ~ Gamma(5,5) pg <- param_gamma(1,1,5,5) pg$r(5) ## a draw with 5 within-block parameters
## theta0 ~ Gamma(1,1); thetak ~ Gamma(5,5) pg <- param_gamma(1,1,5,5) pg$r(5) ## a draw with 5 within-block parameters
Negative Binomial parameter model:
theta_0 = (mu0, sigma0)
theta_k = (muk, sigmak)
param_nbin(a0, a1, b0, b1, c0, c1, d0, d1)
param_nbin(a0, a1, b0, b1, c0, c1, d0, d1)
a0 , a1
|
|
b0 , b1
|
|
c0 , c1
|
|
d0 , d1
|
|
parammod
representing Negative-Binomial distributed parameters
## theta0 = (r0, p0); r0~Gamma(1,1); p0 ~ Beta(1,1); ## thetak = (rk, pk); rk~Gamma(3,3); pk ~ Beta(5,5); pn <- param_nbin(1,1,1,1,3,3,5,5) pn$r(5) ## a draw with 5 within-block parameters
## theta0 = (r0, p0); r0~Gamma(1,1); p0 ~ Beta(1,1); ## thetak = (rk, pk); rk~Gamma(3,3); pk ~ Beta(5,5); pn <- param_nbin(1,1,1,1,3,3,5,5) pn$r(5) ## a draw with 5 within-block parameters
Normal parameter model:
theta_0 = (mu0, sigma0)
theta_k = (muk, sigmak)
param_norm(a0, a1, b0, b1, c0, c1, d0, d1)
param_norm(a0, a1, b0, b1, c0, c1, d0, d1)
a0 , a1
|
|
b0 , b1
|
|
c0 , c1
|
|
d0 , d1
|
|
parammod
representing Normal distributed parameters
## theta0 = (mu0, sigma0); mu0~Normal(0,5); sigma0 ~ Gamma(1,1); ## thetak = (muk, sigmak); muk~Normal(0,3); sigmak ~ Gamma(5,2); pn <- param_norm(0,5,1,1,0,3,5,2) pn$r(5) ## a draw with 5 within-block parameters
## theta0 = (mu0, sigma0); mu0~Normal(0,5); sigma0 ~ Gamma(1,1); ## thetak = (muk, sigmak); muk~Normal(0,3); sigmak ~ Gamma(5,2); pn <- param_norm(0,5,1,1,0,3,5,2) pn$r(5) ## a draw with 5 within-block parameters
Make a matrix of parameters
parammat(x, ...)
parammat(x, ...)
x |
object for dispatch |
... |
additional arguments for method |
a parameter matrix object
Make a matrix of parameters from a blocks
and params
object
## S3 method for class 'blocks' parammat(x, params, ...)
## S3 method for class 'blocks' parammat(x, params, ...)
x |
a |
params |
a |
... |
(unused) |
an NxN
matrix P
, with P[i,j] =
the parameter governing edge ij
Make a matrix of parameters from a matrix of block assignments
## S3 method for class 'matrix' parammat(zleft, zright, params, ...)
## S3 method for class 'matrix' parammat(zleft, zright, params, ...)
zleft |
block assignment matrix on the left |
zright |
block assignment matrix on the right |
params |
the parameters object |
... |
(unused) |
a matrix of parameters of size |zleft
| x |zright
|
Make a matrix of parameters from a params
object
## S3 method for class 'params' parammat(x, kappa, ...)
## S3 method for class 'params' parammat(x, kappa, ...)
x |
a |
kappa |
- number of blocks to compute for matrix (optional) |
... |
(unused) |
a matrix of parameters
Make a matrix of parameters from an sbm
object
## S3 method for class 'sbm' parammat(x, ...)
## S3 method for class 'sbm' parammat(x, ...)
x |
an |
... |
(unused) |
a matrix of parameters
create a parammod
object
parammod(logd, r, t, invt, loggradt, ...)
parammod(logd, r, t, invt, loggradt, ...)
logd |
|
r |
|
t |
mapping parameter space to real line |
invt |
mapping real line to parameter space |
loggradt |
log of the gradient of mapping |
... |
additional arguments to store in the |
A parameter model is a probability model for a params
object.
This class creates a closure with five functions:
- a random method for sampling a params
object
- a log-density method for computing the log-density of a given params
object
- a transformation function t
that maps a parameter value to the real line
- the inverse of t
- the log-gradient of t
a parammod
object
param_beta
param_gamma
param_nbin
param_norm
params
S3 objectmake a params
object from the between-block parameter theta0
and a vector of within block parameters thetak
params(theta0, thetak)
params(theta0, thetak)
theta0 |
between block parameters - a vector of length 'dimension of theta' |
thetak |
within block parameters - a matrix with |
a params
object
p <- params(0.1, c(0.2,0.4,0.5)) p
p <- params(0.1, c(0.2,0.4,0.5)) p
plot a trace of parameter values from MCMC samples
paramtrace(theta, range, burnin)
paramtrace(theta, range, burnin)
theta |
output from sampler |
range |
which thetas to plot? defaults to all. |
burnin |
which iterations to plot? defaults to all. |
'ggplot2' object
plots a block object
## S3 method for class 'blocks' plot(x, col, xaxt = "n", yaxt = "n", xlab = "Nodes", ylab = "Nodes", ...) ## S3 method for class 'blocks' image(x, col, xaxt = "n", yaxt = "n", xlab = "Nodes", ylab = "Nodes", ...)
## S3 method for class 'blocks' plot(x, col, xaxt = "n", yaxt = "n", xlab = "Nodes", ylab = "Nodes", ...) ## S3 method for class 'blocks' image(x, col, xaxt = "n", yaxt = "n", xlab = "Nodes", ylab = "Nodes", ...)
x |
a blocks object to plot |
col |
colours for the plot |
xaxt |
override |
yaxt |
override |
xlab |
override |
ylab |
override |
... |
additional parameters for |
plot the block assignments in a blocks
object as a matrix, color-coded by block membership
## Assign six nodes to four blocks: b <- blocks(c(1,1,2,3,4,4), 4) plot(b) ## note that the lower left corner has one 2x2 red square ## indicating node 1 and 2 belong to the same block
## Assign six nodes to four blocks: b <- blocks(c(1,1,2,3,4,4), 4) plot(b) ## note that the lower left corner has one 2x2 red square ## indicating node 1 and 2 belong to the same block
plots an edges
objects
## S3 method for class 'edges' plot(x, Blocks, sorted = TRUE, xlab = "Node", ylab = "Node", ...) ## S3 method for class 'edges' image(x, Blocks, sorted = TRUE, xlab = "Node", ylab = "Node", ...)
## S3 method for class 'edges' plot(x, Blocks, sorted = TRUE, xlab = "Node", ylab = "Node", ...) ## S3 method for class 'edges' image(x, Blocks, sorted = TRUE, xlab = "Node", ylab = "Node", ...)
x |
an |
Blocks |
a blocks object or |
sorted |
sort by block membership in |
xlab |
label for x-axis |
ylab |
label for y-axis |
... |
parameters for |
ggplot2
plot of edges in a raster
## make an sbm model, sample data then plot and print: model <- sbmmod(dma(2,5), param_beta(1,1,1,1), edges_bern()) s <- model$r(100) e <- redges(s, model$edge) plot(e) plot(e, s) print(e)
## make an sbm model, sample data then plot and print: model <- sbmmod(dma(2,5), param_beta(1,1,1,1), edges_bern()) s <- model$r(100) e <- redges(s, model$edge) plot(e) plot(e, s) print(e)
sbm
objectplot an sbm
object as an image
## S3 method for class 'sbm' plot(x, col, ...) ## S3 method for class 'sbm' image(x, col, ...)
## S3 method for class 'sbm' plot(x, col, ...) ## S3 method for class 'sbm' image(x, col, ...)
x |
an |
col |
colours for each block - if missing, |
... |
additional arguments for plot |
plot.default
helper function for trace plots
plotpostpairs(mat)
plotpostpairs(mat)
mat |
matrix to plot as an image using ggplot2 |
'ggplot2' plot objecy
mean proportion of times two nodes were in the same block under MCMC samples
postpairs(postz)
postpairs(postz)
postz |
output from sampler |
matrix P with P[i,j] = proportion of times i and j are in the same block under postz
Draw draw Categorical distribution
rcat(n, p, replace = TRUE)
rcat(n, p, replace = TRUE)
n |
number of draws |
p |
a length-d probability vector |
replace |
should the categories be replaced? If so n < p required |
a draw from Categorical(p)
rcat(1, 1) ## returns 1 with probability 1 rcat(1, rep(1/6,6)) ## a dice roll
rcat(1, 1) ## returns 1 with probability 1 rcat(1, rep(1/6,6)) ## a dice roll
Draw from Dirichlet distribution
rdirichlet(n, gam)
rdirichlet(n, gam)
n |
number of variates to draw |
gam |
a vector of concentration parameters of length |
matrix dimension n*k
of samples
rdirichlet(1, rep(2,5)) ## a length-5 probability vector
rdirichlet(1, rep(2,5)) ## a length-5 probability vector
Simulate edges from an sbm
object with a given edgemod
redges(SBM, edgemod, sym = TRUE, loops = FALSE, ...)
redges(SBM, edgemod, sym = TRUE, loops = FALSE, ...)
SBM |
an |
edgemod |
an |
sym |
should the network be symmetric? |
loops |
should the network have self-loops? |
... |
additional arguments passed to |
None
an edges
object
## make an sbm model, sample data then plot and print: model <- sbmmod(dma(2,5), param_beta(1,1,1,1), edges_bern()) s <- model$r(100) e <- redges(s, model$edge) plot(e) plot(e, s) print(e)
## make an sbm model, sample data then plot and print: model <- sbmmod(dma(2,5), param_beta(1,1,1,1), edges_bern()) s <- model$r(100) e <- redges(s, model$edge) plot(e) plot(e, s) print(e)
performs a random walk on a parameter value with a given parameter model
rw(p, pm, sigma)
rw(p, pm, sigma)
p |
a parameter |
pm |
a |
sigma |
- scale of random walk |
ist(proposed parameter, locjacobian)
top level sampler function
sampler( edges, sbmmod, nSteps = 1000, algorithm = "rj", sigma = 0.5, statusfreq, currsbm, ... )
sampler( edges, sbmmod, nSteps = 1000, algorithm = "rj", sigma = 0.5, statusfreq, currsbm, ... )
edges |
an |
sbmmod |
an |
nSteps |
number of steps to run sampler |
algorithm |
choice of algorithm options are: |
sigma |
random walk parameter for |
statusfreq |
print the elapsed number of iterations every |
currsbm |
initial state for |
... |
additional parameters to pass to step |
postz
traces for block assignments z
postt
traces for theta
postk
traces for number of blocks kappa
postn
traces for number of occupied blocks
nsteps
number of iterations of chain
algorithm
choice
## see vignette("Weibull-edges")
## see vignette("Weibull-edges")
Conjugate model sampler
sampler.conj(currsbm, edges, sbmmod, sigma = NULL, ...)
sampler.conj(currsbm, edges, sbmmod, sigma = NULL, ...)
currsbm |
the current state of the sampler |
edges |
an |
sbmmod |
an |
sigma |
unused |
... |
additional arguments for |
next state of currsbm
object
If using the CRP as the block model, then this is the IRM sampler of Schmidt or Morup (Schmidt, M.N. and Morup, M., 2013. Nonparametric Bayesian modeling of complex networks: An introduction. IEEE Signal Processing Magazine, 30(3), pp.110-128.)
model <- sbmmod(crp(3), param_beta(1,1,1,1), edges_bern(), marglike=marglike_bern) trueSBM <- model$r(100) Edges <- redges(trueSBM, model$edge) out <- sampler(Edges, model, 10, "conjugate")
model <- sbmmod(crp(3), param_beta(1,1,1,1), edges_bern(), marglike=marglike_bern) trueSBM <- model$r(100) Edges <- redges(trueSBM, model$edge) out <- sampler(Edges, model, 10, "conjugate")
Dirichlet process sampler
sampler.dp(currsbm, edges, sbmmod, sigma)
sampler.dp(currsbm, edges, sbmmod, sigma)
currsbm |
the current state of the sampler |
edges |
an |
sbmmod |
an |
sigma |
random walk parameter for theta |
next state of currsbm
object
For full algorithm details see http://doi.org/10.17635/lancaster/thesis/296
model <- sbmmod(crp(4), param_norm(0,0,1,1,3,3,1,1), edges_norm()) trueSBM <- model$r(100) Edges <- redges(trueSBM, model$edge) dp_out <- sampler(Edges, model, 25, "dp", sigma=0.1)
model <- sbmmod(crp(4), param_norm(0,0,1,1,3,3,1,1), edges_norm()) trueSBM <- model$r(100) Edges <- redges(trueSBM, model$edge) dp_out <- sampler(Edges, model, 25, "dp", sigma=0.1)
Gibbs sampling for node assignments
sampler.gibbs(currsbm, edges, sbmmod, sigma)
sampler.gibbs(currsbm, edges, sbmmod, sigma)
currsbm |
the current state of the sampler |
edges |
an |
sbmmod |
an |
sigma |
random walk parameter for theta |
next state of currsbm
object
This requires a block model with a fixed kappa
model <- sbmmod(multinom(1, 3), param_gamma(1,1,1,1), edges_pois()) trueSBM <- model$r(10) Edges <- redges(trueSBM, model$edge) gibbs_out <- sampler(Edges, model, algorithm="gibbs", 10, sigma=0.1) eval_plots(gibbs_out)
model <- sbmmod(multinom(1, 3), param_gamma(1,1,1,1), edges_pois()) trueSBM <- model$r(10) Edges <- redges(trueSBM, model$edge) gibbs_out <- sampler(Edges, model, algorithm="gibbs", 10, sigma=0.1) eval_plots(gibbs_out)
reversible jump Markov chain Monte Carlo split-merge sampler
sampler.rj(currsbm, edges, sbmmod, sigma, rho = 10)
sampler.rj(currsbm, edges, sbmmod, sigma, rho = 10)
currsbm |
the current state of the sampler |
edges |
an |
sbmmod |
an |
sigma |
random walk parameter for |
rho |
propensity to add a block |
next state of currsbm
object
For full algorithm details see http://doi.org/10.17635/lancaster/thesis/296
model <- sbmmod(dma(1,10), param_nbin(1,1,4,4,0.5,0.5,0.5,0.5), edges_nbin()) trueSBM <- model$r(100) Edges <- redges(trueSBM, model$edge) rj_out <- sampler(Edges, model, 10, "rj", sigma=0.1)
model <- sbmmod(dma(1,10), param_nbin(1,1,4,4,0.5,0.5,0.5,0.5), edges_nbin()) trueSBM <- model$r(100) Edges <- redges(trueSBM, model$edge) rj_out <- sampler(Edges, model, 10, "rj", sigma=0.1)
sbm
Class sbm
sbm(blocks, params)
sbm(blocks, params)
blocks |
a |
params |
a |
an sbm
object
sbm(blocks(c(1,1,2,2,3,3)), params(0.1, c(0.4,0.5,0.6)))
sbm(blocks(c(1,1,2,2,3,3)), params(0.1, c(0.4,0.5,0.6)))
A wrapper for a block and parameter model
sbmmod(blockmod, parammod, edgemod, ...)
sbmmod(blockmod, parammod, edgemod, ...)
blockmod |
a |
parammod |
a |
edgemod |
an |
... |
additional arguments to store in the |
Simple wrapper for the block and parameter model for an sbm
object
an sbmmod
object with a method 'r(n)' sampling an sbm
object with n
nodes from the model and a method logd(sbm)
computing the log-density of sbm
under the model
Matthew Ludkin
split move using average to merge parameters
splitavg(sbm, edges, sbmmod, ...)
splitavg(sbm, edges, sbmmod, ...)
sbm |
the current state of the sampler |
edges |
an |
sbmmod |
an |
... |
additional parameter to 'accept' |
an updated sbm
object
split move: blocks
splitblocks(currblocks, propparams, edges, sbmmod, k)
splitblocks(currblocks, propparams, edges, sbmmod, k)
currblocks |
current blocks |
propparams |
proposed parameters |
edges |
an |
sbmmod |
a model list |
k |
block to split |
list(proposed block structure, log-acceptance-prob)
split move: parameters
splitparams(x, ...)
splitparams(x, ...)
x |
object for dispatch |
... |
additional arguments for method |
list(proposed_params, log-acceptance-prob)
params
split move: params
## S3 method for class 'numeric' splitparams(theta, u, x, parammod)
## S3 method for class 'numeric' splitparams(theta, u, x, parammod)
theta |
a parameter to split |
u |
auxiliary variable |
x |
auxiliary variable |
parammod |
|
list(proposed_params, log-acceptance-prob)
params
split move: params
## S3 method for class 'params' splitparams(params, k, parammod)
## S3 method for class 'params' splitparams(params, k, parammod)
params |
a |
k |
block to split |
parammod |
|
list(proposed_params, log-acceptance-prob)
igraph
using the script in data-raw
Extracted on 27/8/2019 from Kaggle (login required) using:
library(rvest)
read_html("https://www.kaggle.com/stackoverflow/stack-overflow-tag-network/downloads/stack_network_links.csv/1")
The Stack-Overflow data set as extracted from igraph
using the script in data-raw
Extracted on 27/8/2019 from Kaggle (login required) using:
library(rvest)
read_html("https://www.kaggle.com/stackoverflow/stack-overflow-tag-network/downloads/stack_network_links.csv/1")
StackOverflow
StackOverflow
An edges
object of activation counts between brain regions in a Macaque
https://www.kaggle.com/stackoverflow/stack-overflow-tag-network/
igraph
change the block assignment in x
of a node to a new block
updateblock(x, ...)
updateblock(x, ...)
x |
object for dispatch |
... |
additional arguments for method |
object like 'x' with updated block structure
updateblock.blocks
updateblock.sbm
change the block assignment in an blocks
object to a new block
## S3 method for class 'blocks' updateblock(blocks, i, newblock)
## S3 method for class 'blocks' updateblock(blocks, i, newblock)
blocks |
a |
i |
the node to update |
newblock |
the new block for node i |
new blocks
object
change the block assignment in an sbm
object to a new block
## S3 method for class 'sbm' updateblock(currsbm, i, newblock, model)
## S3 method for class 'sbm' updateblock(currsbm, i, newblock, model)
currsbm |
an |
i |
the node to update |
newblock |
the new block for node i |
model |
an |
new sbm
object
If adding a new block, this draws from the prior
Calculate the V-measure of two clusterings
vmeasure(z, truez, beta = 1)
vmeasure(z, truez, beta = 1)
z |
input vector |
truez |
reference vector |
beta |
parameter |
An information based measure of similarity between two clusterings
v-measure of z against truez
Rosenberg, A., & Hirschberg, J. (2007, June). V-measure: A conditional entropy-based external cluster evaluation measure. In Proceedings of the 2007 joint conference on empirical methods in natural language processing and computational natural language learning (EMNLP-CoNLL) (pp. 410-420).
vmeasure(c(1,1,2,2,3,3), c(2,2,1,1,3,3)) ## 1 - doesn't care for labels vmeasure(c(1,1,2,2,3,3), c(1,1,2,2,2,2)) ## 0.7333 vmeasure(c(1,1,2,2,3,3), c(1,1,2,2,3,4)) ## 0.904
vmeasure(c(1,1,2,2,3,3), c(2,2,1,1,3,3)) ## 1 - doesn't care for labels vmeasure(c(1,1,2,2,3,3), c(1,1,2,2,2,2)) ## 0.7333 vmeasure(c(1,1,2,2,3,3), c(1,1,2,2,3,4)) ## 0.904