Skip to content

Instantly share code, notes, and snippets.

@rpietro
Last active August 29, 2015 14:10
Show Gist options
  • Save rpietro/93a03cddd4226c4dce90 to your computer and use it in GitHub Desktop.
Save rpietro/93a03cddd4226c4dce90 to your computer and use it in GitHub Desktop.
script for bnlearn examples
# http://www.bnlearn.com/examples/
# install.packages('bnlearn', repos='http://cran.us.r-project.org')
library(bnlearn)
e = empty.graph(LETTERS[1:6])
class(e)
e
empty.graph(LETTERS[1:6], num = 2)
arc.set = matrix(c("A", "C", "B", "F", "C", "F"),
ncol = 2, byrow = TRUE,
dimnames = list(NULL, c("from", "to")))
arc.set
arcs(e) = arc.set
e
bogus = matrix(c("X", "Y", "W", "Z"),
ncol = 2, byrow = TRUE,
dimnames = list(NULL, c("from", "to")))
bogus
cycle = matrix(c("A", "C", "C", "B", "B", "A"),
ncol = 2, byrow = TRUE,
dimnames = list(NULL, c("from", "to")))
cycle
arcs(e) = cycle
arcs(e, ignore.cycles = TRUE) = cycle
acyclic(e)
loops = matrix(c("A", "A", "B", "B", "C", "D"),
ncol = 2, byrow = TRUE,
dimnames = list(NULL, c("from", "to")))
loops
arcs(e) = loops
edges = matrix(c("A", "B", "B", "A", "C", "D"),
ncol = 2, byrow = TRUE,
dimnames = list(NULL, c("from", "to")))
edges
arcs(e) = edges
adj = matrix(0L, ncol = 6, nrow = 6,
dimnames = list(LETTERS[1:6], LETTERS[1:6]))
adj["A", "C"] = 1L
adj["B", "F"] = 1L
adj["C", "F"] = 1L
adj["D", "E"] = 1L
adj["A", "E"] = 1L
adj
amat(e) = adj
e
model2network("[A][C][B|A][D|C][F|A:B:C][E|F]")
modelstring(e) = "[A][C][B|A][D|C][F|A:B:C][E|F]"
e
random.graph(LETTERS[1:6], prob = 0.1)
random.graph(LETTERS[1:6], num = 2, method = "ic-dag")
random.graph(LETTERS[1:6], num = 2, method = "ic-dag", burn.in = 10^4, every = 50, max.degree = 3)
random.graph(LETTERS[1:6], method = "melancon")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment