| Title: | Joint Inference of Communities and Hierarchy in Directed Networks |
|---|---|
| Description: | Fits a joint Bayesian model of community structure and ranking in directed networks of wins, losses and draws, and measures how much of the ranking the communities explain. The undirected network follows a degree-corrected stochastic block model (Peixoto 2017 <doi:10.1103/PhysRevE.95.012317>), each interaction is decided by the Bradley-Terry model, with draws as in Davidson (1970) <doi:10.1080/01621459.1970.10481082>, and a node's score is its group's score plus an individual deviation. The posterior is sampled by Markov chain Monte Carlo with merge-split moves (Peixoto 2020 <doi:10.1103/PhysRevE.102.012305>) in a C++ core shared with the 'Python' package 'community-hierarchy'. |
| Authors: | Maximilian Jerdee [aut, cre, cph] |
| Maintainer: | Maximilian Jerdee <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.5.0.9000 |
| Built: | 2026-12-02 06:15:21 UTC |
| Source: | https://github.com/maxjerdee/community-hierarchy |
Every argument has a default, so ch_config() is the full coupled model with
everything inferred. Arguments left out take the value of the
hierarchy_model and interaction presets. For the optional numeric
parameters, leaving the argument out means "as the preset says", and NULL
means "infer it".
ch_config( groups_model = "traditional_SBM", hierarchy_model, interaction = "coupled", assortative, mixing_variation, variation_in, variation_out, degree_correction = TRUE, individual_depth, group_depth, draws_parameter, num_groups, group_attribute = NULL, hierarchy_attribute = NULL, weighted_SBM, weighted_proposals = FALSE )ch_config( groups_model = "traditional_SBM", hierarchy_model, interaction = "coupled", assortative, mixing_variation, variation_in, variation_out, degree_correction = TRUE, individual_depth, group_depth, draws_parameter, num_groups, group_attribute = NULL, hierarchy_attribute = NULL, weighted_SBM, weighted_proposals = FALSE )
groups_model |
Community-structure model. |
hierarchy_model |
|
interaction |
|
assortative, mixing_variation, variation_in, variation_out
|
Experimental; leave out. |
degree_correction |
|
individual_depth |
Fixed spread of the individual scores about their
group's score (sigma_I, > 0); |
group_depth |
Fixed spread of the group scores (sigma_G, >= 0); |
draws_parameter |
Fixed draw propensity nu (>= 0); |
num_groups |
Fixed number of groups; |
group_attribute |
Name of a node attribute holding known groups. The partition is then fixed and only the hierarchy is inferred. |
hierarchy_attribute |
Name of a numeric node attribute holding known scores, which are then fixed. |
weighted_SBM |
|
weighted_proposals |
|
An object of class ch_config.
ch_config() ch_config(interaction = "independent") ch_config(degree_correction = FALSE)ch_config() ch_config(interaction = "independent") ch_config(degree_correction = FALSE)
Samples the posterior of the joint model of communities and ranking for a directed network by Markov chain Monte Carlo.
ch_fit( G, config = ch_config(), vertices = NULL, group_graph = NULL, source_high_rank = TRUE, num_chains = 4, num_samples = 1000, num_warmup = 500, sweeps_per_sample = 10, num_param_warmup = 0, beta = 1, num_tempering_chains = 1, merge_split_enabled = TRUE, initial_partition = NULL, initial_scores = NULL, initial_individual_depth = NULL, initial_group_depth = NULL, num_groups_fixed = FALSE, seed = NULL, timeout = NULL, show_progress = interactive() )ch_fit( G, config = ch_config(), vertices = NULL, group_graph = NULL, source_high_rank = TRUE, num_chains = 4, num_samples = 1000, num_warmup = 500, sweeps_per_sample = 10, num_param_warmup = 0, beta = 1, num_tempering_chains = 1, merge_split_enabled = TRUE, initial_partition = NULL, initial_scores = NULL, initial_individual_depth = NULL, initial_group_depth = NULL, num_groups_fixed = FALSE, seed = NULL, timeout = NULL, show_progress = interactive() )
G |
The interactions: a directed igraph graph, or a data
frame of edges whose first two columns are the endpoints (as for
|
config |
The model, from |
vertices |
With a data frame |
group_graph |
Two-layer mode: an undirected graph on the same vertex names
(an igraph graph or an edge data frame) whose edges drive the communities,
while |
source_high_rank |
How to read a decided edge u -> v. |
num_chains, num_samples, num_warmup
|
The run, named as in PyStan. One
iteration is |
sweeps_per_sample |
Sweeps (one proposal per node each) per iteration. |
num_param_warmup |
Iterations before warm-up with the partition and scores frozen, so that the continuous parameters settle first. |
beta |
Inverse temperature of the likelihood (0 = prior, 1 = posterior). |
num_tempering_chains |
Parallel-tempering replicas per chain; only the
|
merge_split_enabled |
Merge-split moves for the partition (Peixoto 2020). Without them, single-node moves can converge to a wrong answer without any sign of it within a chain. |
initial_partition |
Starting groups, one label per node (any labels). Default: greedy modularity on the undirected network, with groups smaller than 5\ by the C++ core, as in the 'Python' package). |
initial_scores |
Starting scores, one per node. Default: (wins - losses) / (wins + losses + draws) over distinct opponents. |
initial_individual_depth, initial_group_depth
|
Starting values of the inferred sigma_I (> 0) and sigma_G (>= 0). |
num_groups_fixed |
Hold the number of groups at that of
|
seed |
Random seed in [0, 2^30); chain c uses |
timeout |
Wall-clock limit in seconds, or |
show_progress |
Print progress to the console. |
An object of class ch_fit. See network_properties(),
node_properties(), consensus_partition(), direction_probabilities()
and samples().
# Four players: a beats b and c, b beats c, c and d draw twice. games <- data.frame(winner = c("a", "a", "b", "c"), loser = c("b", "c", "c", "d"), type = c("dominant", "dominant", "dominant", "neutral"), weight = c(1, 1, 1, 2)) fit <- ch_fit(games, num_chains = 1, num_samples = 50, num_warmup = 20, seed = 1, show_progress = FALSE) network_properties(fit) node_properties(fit)# Four players: a beats b and c, b beats c, c and d draw twice. games <- data.frame(winner = c("a", "a", "b", "c"), loser = c("b", "c", "c", "d"), type = c("dominant", "dominant", "dominant", "neutral"), weight = c(1, 1, 1, 2)) fit <- ch_fit(games, num_chains = 1, num_samples = 50, num_warmup = 20, seed = 1, show_progress = FALSE) network_properties(fit) node_properties(fit)
Co-membership matrix
coincidence_matrix(fit)coincidence_matrix(fit)
fit |
A fit from |
An n x n matrix, with rows and columns named by node: the fraction of samples in which two nodes are in the same group.
Point estimate of the partition
consensus_partition( fit, method = c("moc", "moc_modal_k", "binder"), refine = TRUE, min_samples = 1000 )consensus_partition( fit, method = c("moc", "moc_modal_k", "binder"), refine = TRUE, min_samples = 1000 )
fit |
A fit from |
method |
|
refine |
|
min_samples |
The samples are thinned, evenly spaced, to the larger of
|
An integer vector of groups, named by node, numbered from 1 with the group with the lowest mean score first.
The rank-normalised effective sample sizes and R-hat of Vehtari et al. (2021),
as ArviZ's ess(method = "bulk"), ess(method = "tail") and rhat() and the
posterior package's ess_bulk(), ess_tail() and rhat(), computed in the
C++ core and shared with the Python package.
diagnostics(fit, scores = FALSE)diagnostics(fit, scores = FALSE)
fit |
A fit from |
scores |
|
A data frame with columns ess_bulk, ess_tail and rhat, and
quantity (the rows of network_properties(), then H_A_b, the description
length of the partition, as in partition_ess()) or node. Chains are
truncated to the shortest; R-hat needs two chains (NaN otherwise), and a
fixed parameter has NaN R-hat.
Each edge of G in turn has its outcome left out (who won, or that it was a
draw); the edge itself stays in the network. The scores and the draw parameter
make the outcomes independent, so the leave-one-out posterior is the posterior
reweighted by 1 / p(outcome), with the weights Pareto-smoothed (PSIS-LOO,
Vehtari, Gelman and Gabry 2017) as in the loo package and ArviZ. The
computation is the C++ core's, shared with the Python package.
direction_loo(fit, r_eff = 1)direction_loo(fit, r_eff = 1)
fit |
A fit from |
r_eff |
Relative efficiency of the samples, which sets the length of the smoothed tail. 1 (default) treats the samples as independent. |
An edge of weight w counts w times and is left out whole: give one edge per interaction (a multigraph) for leave-one-interaction-out.
A data frame with one row per edge of G, in edge order:
winner, loser: the edge's ends, the winner first (for a draw, as in G);
draw, weight;
elpd: the leave-one-out log predictive density of the outcome
(sum(elpd) is the model's total);
pareto_k: above 0.7 that edge's estimate is unreliable;
p_win: decided edges, the leave-one-out probability that the winner beats
the loser given that the interaction is decided (plain Bradley-Terry); NA
for draws.
Two models fitted to the same graph compare edge by edge: the difference of
the elpd sums, with standard error sqrt(n) * sd(elpd1 - elpd2).
The posterior predictive probabilities of the outcome of a new interaction between two nodes, averaged over the samples.
direction_probabilities(fit, a, b, draws_allowed = TRUE)direction_probabilities(fit, a, b, draws_allowed = TRUE)
fit |
A fit from |
a, b
|
Two nodes, as named in |
draws_allowed |
|
A vector named by outcome: a, the probability that a beats b;
draw, a draw (only with draws_allowed = TRUE); b, that b beats a.
For example c(Spain = 0.62, draw = 0.24, Morocco = 0.14). The values sum
to 1. "Beats" is the win of the model, so with source_high_rank = FALSE
(for example citations) the a entry is the probability of an edge from
b to a.
The description length of a sample is H_A_b + H_Adir_G_A_s_nu + H_s_G_b_depths (see samples()): minus the log joint probability of the
network, the partition and the scores, given the continuous parameters. It is
a by-product of sampling, not a posterior summary.
mdl_partition(fit)mdl_partition(fit)
fit |
A fit from |
A list with value (in nats) and partition (an integer vector named
by node, numbered as in consensus_partition()).
Network-level posterior summaries
network_properties(fit, prob = 0.9)network_properties(fit, prob = 0.9)
fit |
A fit from |
prob |
Probability mass of the highest-density interval, in (0, 1). |
A data frame with columns parameter, mean, std, hdi_lo and
hdi_hi, one row for each of, in order, num_groups, sigma_T =
sqrt(sigma_G^2 + sigma_I^2), alignment = sigma_G^2 / sigma_T^2 (the share of the
variance in scores that the groups explain), individual_depth (sigma_I),
group_depth (sigma_G), draws_parameter (nu), density_in,
density_out, variation_in, variation_out and degree_correction. The interval is the
shortest one holding floor(prob * n_samples) samples.
Node-level posterior summaries
node_properties( fit, partition = c("consensus", "mdl"), consensus_method = c("moc", "moc_modal_k", "binder"), sort = TRUE )node_properties( fit, partition = c("consensus", "mdl"), consensus_method = c("moc", "moc_modal_k", "binder"), sort = TRUE )
fit |
A fit from |
partition |
|
consensus_method |
Passed to |
sort |
|
A data frame with one row per node (see sort): node, score_mean, score_std, group (numbered from 1, the group
with the lowest mean score first) and group_score_mean (the mean of
score_mean over the node's group).
The rank-normalised split-chain bulk effective sample size (Vehtari et al.
2021) of the description length H_A_b, pooled over chains.
partition_ess(fit)partition_ess(fit)
fit |
A fit from |
A number.
The recorded samples
samples(fit, what = c("parameters", "scores", "groups"))samples(fit, what = c("parameters", "scores", "groups"))
fit |
A fit from |
what |
|
A data frame or a matrix.