-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.R
72 lines (49 loc) · 1.89 KB
/
script.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
library(bnlearn, RJSONIO)
library(gtools)
nsamples = 5000
dataset_path = "cancer.rda"
csv_path = "cancer_5000_bic.csv"
score = "bic"
export_path = "cancer_5000_bic.txt"
file <- load(dataset_path)
df_arcs <- as.data.frame(arcs(bn))
write.csv(df_arcs , 'arcs_cancer.csv')
samples = rbn(bn, n = nsamples, debug = FALSE)
write.csv(samples, csv_path)
cols <- colnames(samples)
printer1<- file(export_path,"w")
for (x in cols) {
cols <- colnames(samples)
cols <- cols[! cols %in% c(x)]
comb = combinations(length(cols), 2, v = cols, set = TRUE, repeats.allowed = FALSE)
for (i in 1:(length(comb[,1]))){
# both edges
vars1 = c(comb[i, 1], x, comb[i, 2])
e = empty.graph(colnames(samples))
arc.set = matrix(c(c(comb[i, 1], x), c(comb[i, 2], x)), ncol = 2, byrow = TRUE, dimnames = list(NULL, c("from", "to")))
arcs(e) = arc.set
result1 = score(e, samples[colnames(samples)], type = score)
writeLines(paste(x, comb[i, 1], comb[i, 2], result1), con=printer1,sep="\n")
# 1 edge
vars2 = c(comb[i, 1], x)
e = empty.graph(colnames(samples))
arc.set = matrix(c(comb[i, 1], x), ncol = 2, byrow = TRUE, dimnames = list(NULL, c("from", "to")))
arcs(e) = arc.set
plot(e)
result2 = score(e, samples[colnames(samples)], type = score)
writeLines(paste(x, comb[i, 1], result2), con=printer1,sep="\n")
# 1 edge
vars3 = c(x, comb[i, 2])
e = empty.graph(colnames(samples))
arc.set = matrix(c(comb[i, 2], x), ncol = 2, byrow = TRUE, dimnames = list(NULL, c("from", "to")))
arcs(e) = arc.set
result3 = score(e, samples[colnames(samples)], type = score)
writeLines(paste(x, comb[i, 2], result3), con=printer1,sep="\n")
}
# 0 edge
vars4 = c(x)
e = empty.graph(colnames(samples))
result4 = score(e, samples[colnames(samples)], type = score)
writeLines(paste(x, result4), con=printer1,sep="\n")
}
close(printer1)