Calculate the posterior expected value for a feature (\(f(g)\), e.g., existence of an edge in graph \(g\)) by marginalising out the graph structure \(q\) over the graph space \(G\), thus $$E(f|D) = \sum_{g \in G} f(g) p(g|D).$$ This can be useful for calculating point estimates of quantities of interests, such as the probability that an edge exists or the probability of one node being an ancestor of another.
Arguments
- x
A chain(s) or collection object.
- p_feature
A function that takes an adjacency matrix or collection object and returns a scalar corresponding to \(f(g)\). The function must be of the form p_feature(dag).
- ...
Extra parameters sent to the methods. For a dag collection you can choose to use estimated p(g|D) in two ways which can be specified using the 'method' parameter.method='sampled' for MCMC sampled frequency (which is our recommended method) or method='score' which uses the normalised scores.
Examples
data <- bnlearn::learning.test
dag <- UniformlySampleDAG(colnames(data))
partitioned_nodes <- DAGtoPartition(dag)
scorer <- CreateScorer(
scorer = BNLearnScorer,
data = data
)
results <- SampleChains(10, partitioned_nodes, PartitionMCMC(), scorer)
dag_chains <- PartitiontoDAG(results, scorer)
# Calculate the mean edge probability per chain.
CalculateFeatureMean(dag_chains, function(x) { return(x) })
#> [[1]]
#> A B C D E F
#> A 0.0 0.6 0.3 0.0 0.6 0
#> B 0.3 0.0 0.0 0.3 0.3 0
#> C 0.7 0.0 0.0 0.7 0.0 0
#> D 1.0 0.1 0.3 0.0 0.0 0
#> E 0.4 0.4 0.0 0.4 0.0 0
#> F 0.0 0.4 0.2 0.0 0.6 0
#>
#> [[2]]
#> A B C D E F
#> A 0.0 0.0 0 0.3 0.2 0
#> B 0.6 0.0 0 0.0 1.0 0
#> C 1.0 1.0 0 0.3 0.0 0
#> D 0.7 0.7 0 0.0 0.2 0
#> E 0.0 0.0 0 0.0 0.0 0
#> F 0.0 0.0 1 0.7 1.0 0
#>
# Calculate the mean edge probability across chains.
CalculateFeatureMean(FlattenChains(dag_chains), function(x) { return(x) })
#> A B C D E F
#> A 0.00 0.3 0.15 0.15 0.40 0
#> B 0.45 0.0 0.00 0.15 0.65 0
#> C 0.85 0.5 0.00 0.50 0.00 0
#> D 0.85 0.4 0.15 0.00 0.10 0
#> E 0.20 0.2 0.00 0.20 0.00 0
#> F 0.00 0.2 0.60 0.35 0.80 0