data(learning.test)
dag = hc(learning.test)
score(dag, learning.test, type = "bde")
## let's see score equivalence in action!
dag2 = set.arc(dag, "B", "A")
score(dag2, learning.test, type = "bde")
## K2 score on the other hand is not score equivalent.
score(dag, learning.test, type = "k2")
score(dag2, learning.test, type = "k2")
## BDe with a prior.
beta = data.frame(from = c("A", "D"), to = c("B", "F"),
prob = c(0.2, 0.5), stringsAsFactors = FALSE)
score(dag, learning.test, type = "bde", prior = "cs", beta = beta)
## equivalent to logLik(dag, learning.test)
score(dag, learning.test, type = "loglik")
## equivalent to AIC(dag, learning.test)
score(dag, learning.test, type = "aic")
## custom score, computing BIC manually.
my.bic = function(node, parents, data, args) {
n = nrow(data)
if (length(parents) == 0) {
counts = table(data[, node])
nparams = dim(counts) - 1
sum(counts * log(counts / n)) - nparams * log(n) / 2
}#THEN
else {
counts = table(data[, node], configs(data[, parents, drop = FALSE]))
nparams = ncol(counts) * (nrow(counts) - 1)
sum(counts * log(prop.table(counts, 2))) - nparams * log(n) / 2
}#ELSE
}#MY.BIC
score(dag, learning.test, type = "custom-score", fun = my.bic, by.node = TRUE)
score(dag, learning.test, type = "bic", by.node = TRUE)
Run the code above in your browser using DataLab