-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaipps.main.R
78 lines (59 loc) · 1.61 KB
/
caipps.main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Description: create a Bayesian Network for synthesizing fake questionnaire data.
# Author: Haley Hunter-Zinck
# Date: April 13, 2020
# Usage: R -f caipps.main.R --args <file_node> <file_edge> <outfile_prefix>
# Dependencies: bnlearn, caipps.fxns.R
# begin -----------------------------------------
# start timer
tic = as.double(Sys.time())
# user input
args = commandArgs(trailingOnly = TRUE)
if(length(args) != 3)
{
cat("Usage: R -f caipps.main.R --args <file_node> <file_edge> <outfile_prefix>")
}
# packages
library(bnlearn)
source("caipps.fxns.R")
# files
#file_node = "caipps_node.csv"
#file_edge = "caipps_edge.csv"
#file_synth = "caipps_synth.csv"
#file_fit = "caipps_fit.rds"
file_node = args[1]
file_edge = args[2]
out_prefix = args[3]
# outfiles
file_synth = paste0(out_prefix,".csv")
file_fit = paste0(out_prefix, ".rds")
# parameters
nsample = 200
DEBUG = TRUE
if(DEBUG)
{
cat(now(), ": Starting caipps.main.R...\n", sep = "")
}
# graph --------------------------------
if(DEBUG)
{
cat(now(), ": Building graph...\n", sep="")
}
nodes = read_node(file_node)
edges = read_edge(file_edge)
g = construct_graph(nodes = names(nodes), edges = edges)
fit = fit_graph(nodes, edges)
saveRDS(fit, file_fit)
# generate --------------------------------
if(DEBUG)
{
cat(now(), ": Generating synthetic dataset...\n", sep="")
}
synth = rbn(x = fit, n = nsample)
write.csv(synth, row.names = TRUE, file = file_synth)
# close --------------------------------
if(DEBUG)
{
toc = as.double(Sys.time())
cat(now(), ": Synthetic dataset written to ", file_synth,"\n", sep="")
cat("Runtime: ", toc - tic, " s\n", sep = "")
}