|
| 1 | +#!/usr/bin/env Rscript |
| 2 | + |
| 3 | +## |
| 4 | +# @file simulate_bn.R |
| 5 | +# @brief Script for simulating a Bayesian network using pcalg |
| 6 | +# @author Ankit Srivastava <asrivast@gatech.edu> |
| 7 | +# |
| 8 | +# Copyright 2020 Georgia Institute of Technology |
| 9 | +# |
| 10 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | +# you may not use this file except in compliance with the License. |
| 12 | +# You may obtain a copy of the License at |
| 13 | +# |
| 14 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +# |
| 16 | +# Unless required by applicable law or agreed to in writing, software |
| 17 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +# See the License for the specific language governing permissions and |
| 20 | +# limitations under the License. |
| 21 | + |
| 22 | +library('pcalg') |
| 23 | +library('optparse') |
| 24 | + |
| 25 | + |
| 26 | +if (!exists('argv')) { |
| 27 | + argv = commandArgs(trailing=TRUE) |
| 28 | +} |
| 29 | + |
| 30 | +parser <- OptionParser() |
| 31 | +parser <- add_option(parser, c('--seed'), type='integer', help='PRNG seed.') |
| 32 | +parser <- add_option(parser, c('--nvars', '-n'), type='integer', help='Number of variables in the simulated network.') |
| 33 | +parser <- add_option(parser, c('--prob', '-p'), type='double', default=0.05, help='Threshold p-value used for generating the graph.') |
| 34 | +parser <- add_option(parser, c('--mbsize'), action='store_true', default=FALSE, help='Print out the average MB size.') |
| 35 | +parser <- add_option(parser, c('--bn', '-b'), type='character', help='Name of the dot file to which the network is written.') |
| 36 | +parser <- add_option(parser, c('--nobs', '-m'), type='integer', help='Number of observations in the simulated dataset.') |
| 37 | +parser <- add_option(parser, c('--datafile', '-d'), type='character', help='Name of the file to which dataset is written.') |
| 38 | +parser <- add_option(parser, c('--colobs', '-c'), action='store_true', default=FALSE, help='The file contains observations in columns.') |
| 39 | +parser <- add_option(parser, c('--sep', '-s'), type='character', default=' ', help='Delimiting character in the dataset file.') |
| 40 | +args <- parse_args(parser, args=argv) |
| 41 | + |
| 42 | + |
| 43 | +if (!is.null(args$seed)) { |
| 44 | + set.seed(args$seed) |
| 45 | +} |
| 46 | + |
| 47 | +tGenerate <- proc.time() |
| 48 | +nodes <- c() |
| 49 | +for (v in seq(1, args$nvars)) { |
| 50 | + nodes <- c(nodes, paste('V', v, sep='')) |
| 51 | +} |
| 52 | +# Graph of type graphNEL |
| 53 | +dag <- randomDAG(args$nvars, prob=args$prob, V=nodes) |
| 54 | +show(dag) |
| 55 | +tGenerate <- proc.time() - tGenerate |
| 56 | +cat('Time taken in generating the network:', tGenerate['elapsed'], 'sec\n') |
| 57 | + |
| 58 | +if (args$mbsize) { |
| 59 | + cat('Using bnlearn from', find.package('bnlearn'), '\n') |
| 60 | + library('bnlearn') |
| 61 | + tMBSize <- proc.time() |
| 62 | + # Convert to BN |
| 63 | + bn <- as.bn(dag) |
| 64 | + avgmb <- mean(sapply(nodes, function(n) { length(bn$nodes[[n]]$mb) })) |
| 65 | + cat('Average MB size is', avgmb, '\n') |
| 66 | + tMBSize <- proc.time() - tMBSize |
| 67 | + cat('Time taken in getting the MB sizes:', tMBSize['elapsed'], 'sec\n') |
| 68 | +} |
| 69 | + |
| 70 | +if (!is.null(args$bn)) { |
| 71 | + tWrite <- proc.time() |
| 72 | + write.dot(args$bn, bn) |
| 73 | + tWrite <- proc.time() - tWrite |
| 74 | + cat('Time taken in writing the network:', tWrite['elapsed'], 'sec\n') |
| 75 | +} |
| 76 | + |
| 77 | +if (!is.null(args$nobs)) { |
| 78 | + tData <- proc.time() |
| 79 | + data <- rmvDAG(args$nobs, dag, use.node.names=TRUE) |
| 80 | + if (!args$colobs) { |
| 81 | + data <- t(data) |
| 82 | + } |
| 83 | + tData <- proc.time() - tData |
| 84 | + cat('Time taken in getting the data:', tData['elapsed'], 'sec\n') |
| 85 | + tWrite <- proc.time() |
| 86 | + write.table(data, file=args$datafile, sep=args$sep, row.names=!args$colobs, col.names=args$colobs) |
| 87 | + tWrite <- proc.time() - tWrite |
| 88 | + cat('Time taken in writing the dataset:', tWrite['elapsed'], 'sec\n') |
| 89 | +} |
0 commit comments