@@ -78,7 +78,7 @@ neighbour = function(x, mb, data, alpha, B = NULL, whitelist, blacklist,
78
78
# whitelisted nodes are included (arc orientation is irrelevant),
79
79
# and blacklisted nodes are removed if both directed arcs are banned
80
80
# and both are not in the whitelist.
81
- nbrhood = nbrhood [! ( nbrhood %in% blacklisted ) ]
81
+ nbrhood = nbrhood [nbrhood %! in % blacklisted ]
82
82
nbrhood = unique(c(nbrhood , whitelisted ))
83
83
84
84
# use backtracking for a further screening of the nodes to be checked.
@@ -92,7 +92,7 @@ neighbour = function(x, mb, data, alpha, B = NULL, whitelist, blacklist,
92
92
93
93
# known.bad nodes are not to be checked for inclusion and/or used in
94
94
# the subsets.
95
- nbrhood = nbrhood [! ( nbrhood %in% known.bad ) ]
95
+ nbrhood = nbrhood [nbrhood %! in % known.bad ]
96
96
97
97
}# THEN
98
98
@@ -133,7 +133,7 @@ neighbour = function(x, mb, data, alpha, B = NULL, whitelist, blacklist,
133
133
cat(" > dsep.set = '" , dsep.set , " '\n " )
134
134
135
135
a = allsubs.test(x = x , y = y , sx = dsep.set , min = ifelse(empty.dsep , 0 , 1 ),
136
- data = data , test = test , alpha = alpha , B = B , debug = debug )
136
+ data = data , test = test , alpha = alpha , B = B , debug = debug )[ 1 ]
137
137
138
138
if (a > alpha ) {
139
139
@@ -156,7 +156,7 @@ neighbour = function(x, mb, data, alpha, B = NULL, whitelist, blacklist,
156
156
157
157
# do not even try to remove whitelisted nodes; on the other hand, known.good
158
158
# nodes from backtracking should be checked to remove false positives.
159
- sapply(nbrhood [! ( nbrhood %in% whitelisted ) ], nbr , x = x , mb = mb , test = test )
159
+ sapply(nbrhood [nbrhood %! in % whitelisted ], nbr , x = x , mb = mb , test = test )
160
160
161
161
return (list (mb = mb [[x ]], nbr = nbrhood ))
162
162
@@ -177,11 +177,12 @@ vstruct.detect = function(nodes, arcs, mb, data, alpha, B = NULL, test,
177
177
178
178
tos = parents.backend(arcs , x , TRUE )
179
179
180
- if (length(tos ) < 2 ) return (NULL )
180
+ if (length(tos ) < 2 )
181
+ return (NULL )
181
182
182
183
# build a list of possibile parents for the node x, i.e. all the subsets
183
184
# of size 2 of the nodes connected to x by incoming arcs.
184
- tos.combs = subsets(length( tos ) , 2 , tos )
185
+ tos.combs = subsets(tos , 2 )
185
186
vs = NULL
186
187
187
188
for (j in 1 : nrow(tos.combs )) {
@@ -193,70 +194,28 @@ vstruct.detect = function(nodes, arcs, mb, data, alpha, B = NULL, test,
193
194
cat(" * checking" , y , " ->" , x , " <-" , z , " \n " )
194
195
195
196
# check there's no arc from y to z and vice versa.
196
- if (! is.listed(arcs , c(y , z )) &&
197
- ! is.listed(arcs , c(z , y ))) {
198
-
199
- mby = mb [[y ]][[' mb' ]]
200
- mbz = mb [[z ]][[' mb' ]]
201
-
202
- # compute mb(y) - {x,z} and mb(z) - {x,y}
203
- mby = mby [! (mby %in% c(x , z ))]
204
- mbz = mbz [! (mbz %in% c(x , y ))]
205
-
206
- # choose the smallest one to cut down the number of subsets to test.
207
- dsep.set = smaller(mby , mbz )
208
-
209
- if (debug )
210
- cat(" > chosen d-separating set: '" , dsep.set , " '\n " )
211
-
212
- k = 0
213
- max_a = a = 0
214
-
215
- repeat {
216
-
217
- dsep.subsets = subsets(length(dsep.set ), k , dsep.set )
218
-
219
- for (s in 1 : nrow(dsep.subsets )) {
197
+ if (is.listed(arcs , c(y , z ), either = TRUE ))
198
+ next
220
199
221
- a = indep.test(y , z , c(dsep.subsets [s ,], x ), data = data ,
222
- test = test , B = B , alpha = alpha )
223
- if (debug )
224
- cat(" > testing" , y , " vs" , z , " given" , c(dsep.subsets [s ,], x ), " (" , a , " )\n " )
225
- max_a = max(a , max_a )
226
- if (a > alpha ) {
200
+ # choose the smallest of mb(y) - {x,z} and mb(z) - {x,y} to cut down
201
+ # the number of subsets to test.
202
+ dsep.set = smaller(setdiff(mb [[y ]][[' mb' ]], c(x , z )),
203
+ setdiff(mb [[z ]][[' mb' ]], c(x , y )))
227
204
228
- if (debug )
229
- cat(" >" , y , " and" , z , " are independent given '" , c(dsep.subsets [s ,], x ), " ' (" , a , " )\n " )
230
- break
231
-
232
- }# THEN
233
-
234
- }# FOR
235
-
236
- if (a < = alpha ) {
237
-
238
- if (k < length(dsep.set )) {
239
-
240
- k = k + 1
241
-
242
- }# THEN
243
- else {
244
-
245
- if (debug )
246
- cat(" @ detected v-structure" , y , " ->" , x , " <-" , z , " \n " )
247
- vs = rbind(vs , data.frame (max_a , y , x , z , stringsAsFactors = FALSE ))
248
- break
249
-
250
- }# ELSE
205
+ if (debug )
206
+ cat(" > chosen d-separating set: '" , dsep.set , " '\n " )
251
207
252
- }# THEN
253
- else {
208
+ assoc = allsubs.test(x = y , y = z , fixed = x , sx = dsep.set , data = data ,
209
+ test = test , B = B , alpha = alpha , debug = debug )
210
+ a = assoc [1 ]
211
+ max_a = assoc [3 ]
254
212
255
- break
213
+ if ( a < = alpha ) {
256
214
257
- }# ELSE
215
+ if (debug )
216
+ cat(" @ detected v-structure" , y , " ->" , x , " <-" , z , " \n " )
258
217
259
- } # REPEAT
218
+ vs = rbind( vs , data.frame ( max_a , y , x , z , stringsAsFactors = FALSE ))
260
219
261
220
}# THEN
262
221
@@ -288,13 +247,13 @@ vstruct.apply = function(arcs, vs, nodes, strict, debug = FALSE) {
288
247
}# THEN
289
248
290
249
if (strict )
291
- stop(paste( " vstructure" , v [" y" ], " -> " , v [" x" ], " <- " , v [" z" ],
292
- " is not applicable, because one or both arcs are oriented" ,
293
- " in the opposite direction." ) )
250
+ stop(" vstructure " , v [" y" ], " -> " , v [" x" ], " <- " , v [" z" ],
251
+ " is not applicable, because one or both arcs are oriented" ,
252
+ " in the opposite direction." )
294
253
else
295
- warning(paste( " vstructure" , v [" y" ], " -> " , v [" x" ], " <- " , v [" z" ],
296
- " is not applicable, because one or both arcs are oriented" ,
297
- " in the opposite direction." ) )
254
+ warning(" vstructure " , v [" y" ], " -> " , v [" x" ], " <- " , v [" z" ],
255
+ " is not applicable, because one or both arcs are oriented" ,
256
+ " in the opposite direction." )
298
257
299
258
return (NULL )
300
259
@@ -314,13 +273,13 @@ vstruct.apply = function(arcs, vs, nodes, strict, debug = FALSE) {
314
273
}# THEN
315
274
316
275
if (strict )
317
- stop(paste( " vstructure" , v [" y" ], " -> " , v [" x" ], " <- " , v [" z" ],
318
- " is not applicable, because one or both arcs introduce cycles" ,
319
- " in the graph." ) )
276
+ stop(" vstructure " , v [" y" ], " -> " , v [" x" ], " <- " , v [" z" ],
277
+ " is not applicable, because one or both arcs introduce cycles" ,
278
+ " in the graph." )
320
279
else
321
- warning(paste( " vstructure" , v [" y" ], " -> " , v [" x" ], " <- " , v [" z" ],
322
- " is not applicable, because one or both arcs introduce cycles" ,
323
- " in the graph." ) )
280
+ warning(" vstructure " , v [" y" ], " -> " , v [" x" ], " <- " , v [" z" ],
281
+ " is not applicable, because one or both arcs introduce cycles" ,
282
+ " in the graph." )
324
283
325
284
return (NULL )
326
285
0 commit comments