Blame view

bnlearn.R 7.24 KB
8375af764   Jingan Qu   add all code 11-7...
1
2
  
  setwd("/home/qja0428/Dropbox/research/Yoo")
94c6dd68c   Jingan Qu   11-29-2016
3
  #setwd("/Users/jinganqu/Dropbox/research/Yoo/code")
8375af764   Jingan Qu   add all code 11-7...
4
5
6
7
  
  library(bnlearn)
  
  data(learning.test)
8375af764   Jingan Qu   add all code 11-7...
8

94c6dd68c   Jingan Qu   11-29-2016
9
  dataset = learning.test
8375af764   Jingan Qu   add all code 11-7...
10
11
12
  
  a = hc(dataset, score='bde')
  plot(a)
94c6dd68c   Jingan Qu   11-29-2016
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  from <- a$arcs[,1]
  to <- a$arcs[,2]
  m <- ncol(dataset)
  n <- nrow(dataset)
  #a function that return the variable position
  position <- function(x, data) {
    result <- which(x == names(data))
    return(result)
  }
  
  from <- sapply(from,position,data=dataset)
  to <- sapply(to,position,data=dataset)
  
  score(a,data=dataset,type="bde")
  
  
  
  
  
  
  
  
  
  #############simple test sample############
  test <- learning.test[1:5,1:3]
  test[2,3] <- "b"
  test <- data.frame(apply(test,2,as.character))
  
  dag <- hc(test, score = "bic")
  
  score(dag,data=test,type="bde")
  #-10.37759
  #bde score by hand
  score <- log(gamma(.5)/gamma(3.5)) + log(gamma(3.25)/gamma(.25)) + log(gamma(.25)/gamma(.25)) +
    log(gamma(.5)/gamma(2.5)) + log(gamma(1.25)/gamma(.25)) + log(gamma(1.25)/gamma(.25)) + 
    log(gamma(.5)/gamma(3.5)) + log(gamma(2.25)/gamma(.25)) + log(gamma(1.25)/gamma(.25)) +
    log(gamma(.5)/gamma(2.5)) + log(gamma(.25)/gamma(.25)) + log(gamma(2.25)/gamma(.25))
  
  write.csv(test,"./code/test.csv",row.names = F)
  
  
  ################################################
  ######check all the graph of 3 variables########
  ################################################
  
  ######generate data
  set.seed(123)
  A <- sample(1:2,5000,replace = T)
  set.seed(428)
  B <- sample(1:2,5000,replace = T)
  set.seed(403)
  C <- sample(1:2,5000,replace = T)
  
  dataset <- data.frame(A,B,C)
  #save the dataset
  write.csv(dataset,"test_three_variables.csv",row.names = F)
  
  dataset <- read.csv("test_three_variables.csv")
  #make the variable as factor
  dataset <- apply(dataset,2,as.factor)
  dataset <- as.data.frame(dataset)
  
  score_bde <- numeric(25)
  
  #graph 1  A B C
  g1 <- empty.graph(names(dataset))
  score_bde[1] <- score(g1,data = dataset, type="bde") #-10405.17
  
  #graph 2 A->B C
  g2 <- set.arc(g1,"A","B")
  score_bde[2] <- score(g2,data = dataset, type="bde") #-10408.04
  
  #graph 3  C->A->B
  g3 <- set.arc(g2,"C","A")
  score_bde[3] <- score(g3,data = dataset, type="bde") #-10411.15
  
  
  #graph 4 B->A<-C
  g4 <- empty.graph(names(dataset))
  modelstring(g4) <- "[B][C][A|B:C]"
  score_bde[4] <- score(g4,data = dataset, type="bde") #-10414.47
  
  #graph 5 B->C->A
  g5 <- empty.graph(names(dataset))
  modelstring(g5) <- "[B][C|B][A|C]"
  score_bde[5] <- score(g5,data = dataset, type="bde") #-10411.13
  
  #graph 6 
  g6 <- empty.graph(names(dataset))
  modelstring(g6) <- "[B][C|B][A|C:B]"
  score_bde[6] <- score(g6,data = dataset, type="bde") #-10417.33
  
  #graph 7
  g7 <- empty.graph(names(dataset))
  modelstring(g7) <- "[C][A|C][B|C]"
  score_bde[7] <- score(g7,data = dataset, type="bde") #-10411.13
  
  #graph 8
  g8 <- empty.graph(names(dataset))
  modelstring(g8) <- "[C][A|C][B|C:A]"
  score_bde[8] <- score(g8,data = dataset, type="bde") #-10417.33
  
  #graph 9
  g9 <- empty.graph(names(dataset))
  modelstring(g9) <- "[C][A|C:B][B|C]"
  score_bde[9] <- score(g9,data = dataset, type="bde") #-10417.33
  
  #graph 10
  g10 <- empty.graph(names(dataset))
  modelstring(g10) <- "[C|A][A][B]"
  score_bde[10] <- score(g10,data = dataset, type="bde") #-10408.27
  
  #graph 11
  g11 <- empty.graph(names(dataset))
  modelstring(g11) <- "[C|A][A][B|A]"
  score_bde[11] <- score(g11,data = dataset, type="bde") #-10411.15
  
  #graph 12
  g12 <- empty.graph(names(dataset))
  modelstring(g12) <- "[A|B][B][C|A]"
  score_bde[12] <- score(g12,data = dataset, type="bde") #-10411.15
  
  #graph 13
  g13 <- empty.graph(names(dataset))
  modelstring(g13) <- "[A|B][B][C]"
  score_bde[13] <- score(g13,data = dataset, type="bde") #-10408.04
  
  
  #graph 14
  g14 <- empty.graph(names(dataset))
  modelstring(g14) <- "[A][B][C|A:B]"
  score_bde[14] <- score(g14,data = dataset, type="bde") #-10414.45
  
  
  #graph 15
  g15 <- empty.graph(names(dataset))
  modelstring(g15) <- "[A][B|A][C|A:B]"
  score_bde[15] <- score(g15,data = dataset, type="bde") #-10417.33
  
  
  #graph 16
  g16 <- empty.graph(names(dataset))
  modelstring(g16) <- "[A|B][B][C|A:B]"
  score_bde[16] <- score(g16,data = dataset, type="bde") #-10417.33
  
  
  #graph 17
  g17 <- empty.graph(names(dataset))
  modelstring(g17) <- "[A][B|C][C|A]"
  score_bde[17] <- score(g17,data = dataset, type="bde") #-10411.13
  
  
  #graph 18
  g18 <- empty.graph(names(dataset))
  modelstring(g18) <- "[A][B|C:A][C|A]"
  score_bde[18] <- score(g18,data = dataset, type="bde") #-10417.33
  
  
  #graph 19
  g19 <- empty.graph(names(dataset))
  modelstring(g19) <- "[A][B][C|B]"
  score_bde[19] <- score(g19,data = dataset, type="bde") #-10408.03
  
  
  #graph 20
  g20 <- empty.graph(names(dataset))
  modelstring(g20) <- "[A][B|A][C|B]"
  score_bde[20] <- score(g20,data = dataset, type="bde") #-10410.9
  
  
  #graph 21
  g21 <- empty.graph(names(dataset))
  modelstring(g21) <- "[A|B][B][C|B]"
  score_bde[21] <- score(g21,data = dataset, type="bde") #-10410.9
  
  #graph 22
  g22 <- empty.graph(names(dataset))
  modelstring(g22) <- "[A][B|C][C]"
  score_bde[22] <- score(g22,data = dataset, type="bde") #-10408.03
  
  
  #graph 23
  g23 <- empty.graph(names(dataset))
  modelstring(g23) <- "[A][B|C:A][C]"
  score_bde[23] <- score(g23,data = dataset, type="bde") #-10414.22
  
  
  #graph 24
  g24 <- empty.graph(names(dataset))
  modelstring(g24) <- "[A|B][B|C][C]"
  score_bde[24] <- score(g24,data = dataset, type="bde") #-10410.9
  
  
  #graph 25
  g25 <- empty.graph(names(dataset))
  modelstring(g25) <- "[A|C][B][C]"
  score_bde[25] <- score(g25,data = dataset, type="bde") #-10408.27
  
  m <- ncol(dataset)
  n <- nrow(dataset)
  #a function that return the variable position
  position <- function(x, data) {
    result <- which(x == names(data))
    return(result)
  }
  #bde score from python
  score_bde_python <- c(-10409.216534618594,
                        -10413.72828956955,
                        -10418.467521622148,
                        -10423.856345541608,
                        -10418.448531487986,
                        -10418.44853148799,
                        -10418.44853148799,
                        -10428.34911035841,
                        -10428.34911035841,
                        -10413.95576667119,
                        -10418.467521622146,
                        -10418.467521622146,
                        -10413.72828956955,
                        -10423.837355407451,
                        -10428.349110358407,
                        -10428.349110358407,
                        -10418.448531487988,
                        -10428.349110358407,
                        -10413.709299435392,
                        -10418.221054386348,
                        -10418.221054386348,
                        -10413.709299435392,
                        -10423.609878305811,
                        -10418.221054386351,
                        -10413.955766671188)
  
  arc <- data.frame()
  
  #output a pdf file
  pdf("plot.pdf")
  for (i in 1:25) {
    s <- paste0("g",i)
    tmp <- eval(parse(text=s))
    #plot
    plot(tmp,main=paste("BDe score (python): ", score_bde_python[i], 
         "
  BDe score (r): ", score_bde[i]))
  }
  dev.off()
  
  
  score_bde - score_bde_python
  
  
  for (i in 1:25) {
    s <- paste0("g",i)
    tmp <- eval(parse(text=s))
    #plot
    plot(tmp,main=paste("BDe score: ", score_bde_python[i]))
    from <- tmp$arcs[,1]
    to <- tmp$arcs[,2]
    if (length(from) == 0) next
    else {
      from <- sapply(from,position,data=dataset) - 1
      to <- sapply(to,position,data=dataset) - 1
      l <- rep(i,length(from))
      t <- data.frame(from, to, l)
      arc <- rbind(arc, t)
    }
  }
  names(arc) <- c("from","to","graph")
  write.csv(arc,"arc.csv",row.names = F)
8375af764   Jingan Qu   add all code 11-7...
281

8375af764   Jingan Qu   add all code 11-7...
282