R/demo.R

Defines functions tiny_demo

#' Tiny Demo
#' 
#' This is a tiny visual demonstration of the Bayes net functionality.
#' @export
tiny_demo=function() {
  cat("We create a simple network of 9 binary variables and plot it.\n")
  net=bayesnet("mynet")
  net=addNode(net,"dirichlet",c("True","False"),"X1",c(),matrix(c(1,9),nrow=1))
  net=addNode(net,"dirichlet",c("True","False"),"X2",c(1),matrix(c(1,9,2,8),byrow=T,nrow=2))
  net=addNode(net,"dirichlet",c("True","False"),"X3",c(2),matrix(c(1,9,2,8),byrow=T,nrow=2))
  net=addNode(net,"dirichlet",c("True","False"),"X4",c(1,3),matrix(c(1,9,2,8,5,5,1,9),byrow=T,nrow=4))
  net=addNode(net,"dirichlet",c("True","False"),"X5",c(3,4),matrix(c(1,9,2,8,5,5,1,9),byrow=T,nrow=4))
  net=addNode(net,"dirichlet",c("True","False"),"X6",c(5),matrix(c(1,9,2,8),byrow=T,nrow=2))
  net=addNode(net,"dirichlet",c("True","False"),"X7",c(4),matrix(c(1,9,2,8),byrow=T,nrow=2))
  net=addNode(net,"dirichlet",c("True","False"),"X8",c(3,7),matrix(c(1,9,2,8,1,9,5,5),byrow=T,nrow=4))
  net=addNode(net,"dirichlet",c("True","False"),"X9",c(8),matrix(c(31,9,32,8),byrow=T,nrow=2))
  plot(net)
  readline("Press Enter to continue...")
  cat("We perform inference with the following observed information:\n")
  cat("  Variable 1 observed value: False\n")
  cat("  Variable 2 observed value: False\n")
  cat("  Variable 3 observed value: True\n")
  cat("  Variable 6 observed value: False\n")
  cat("All other variables are unobserved.\n")
  cat("Metropolis in Gibbs MCMC sampling is performed, with 1000 samples and burn of 100.\n")
  out=predict(net,c(2,2,1,NA,NA,2,NA,NA,NA),update=TRUE,algorithm="MC",algoSpecific=list(burn=100,thinner=1))
  readline("Press Enter to continue...")
  cat("Posterior probabilities are obtained. For example, for variable 8 we have:\n")
  cat(paste("  Probability True: ",out$posterior[[8]][1]))
  cat(paste("  Probability False: ",out$posterior[[8]][2]))
  readline("Press Enter to continue...")
  cat("Distributions are updated based on hard evidence.\n")
  cat("For variable 1 we originally had observation parameters:\n")
  print(net$nodes[[1]]$params)
  cat("For an expected probability distribution of:\n")
  probs=net$nodes[[1]]$params/sum(net$nodes[[1]]$params)
  cat(paste("  Probability True: ",probs[1]))
  cat(paste("  Probability False: ",probs[2]))
  cat("In the updated network, variable 1 has observation parameters of:\n")
  print(out$nodes[[1]]$params)
  cat("For an expected probability distribution of:\n")
  probs=out$nodes[[1]]$params/sum(out$nodes[[1]]$params)
  cat(paste("  Probability True: ",probs[1]))
  cat(paste("  Probability False: ",probs[2]))
  readline("Press Enter to continue...")
  cat("Distributions are updated based on *soft* evidence too.\n")
  cat("For variable 8 we originally had observation parameters:\n")
  print(net$nodes[[8]]$params)
  cat("For an expected conditional probability distribution matrix (different rows correspond to conditioned on variables taking different values) of:\n")
  print(t(apply(net$nodes[[8]]$params,1,function(row)row/sum(row))))
  cat("In the updated network, variable 8 has observation parameters of:\n")
  print(out$nodes[[8]]$params)
  cat("For an expected conditional probability distribution matrix (different rows correspond to conditioned on variables taking different values) of:\n")
  print(t(apply(out$nodes[[8]]$params,1,function(row)row/sum(row))))
  readline("Press Enter to continue...")
  cat("We perform analysis specifying variables 1-3 as defects, and variables 7-9 as root causes.\n")
  roles=analyzer(matrix(c(7,1,8,1,9,1),byrow=T,nrow=3),matrix(c(1,1,2,1,3,1),byrow=T,nrow=3))
  analysis=analyze(roles,out,out$posterior)
  print(analysis)
  
  
}
mickash/Adaptive-Bayesian-Networks documentation built on Nov. 14, 2019, 12:14 a.m.