Skip to content

Commit a645fdc

Browse files
Fixed bug where ccdr.run was truncating solution paths that did not meet edge threshold (set by alpha)
1 parent 7959a65 commit a645fdc

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

R/ccdrAlgorithm-main.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,12 @@ ccdr_gridR <- function(cors,
290290
# 7-16-14: Added code below to check edge threshold via alpha parameter
291291
if(ccdr.out[[i]]$nedge > alpha * pp){
292292
if(verbose) message("Edge threshold met, terminating algorithm with ", ccdr.out[[i-1]]$nedge, " edges.")
293+
ccdr.out <- ccdr.out[1:(i-1)] # only return up to i - 1 since the last (ith) model did not finish
293294
break
294295
}
295296
}
296297

297-
ccdr.out[1:(i-1)] # only return up to i - 1 since the last (ith) model would not have finished running anyway
298+
ccdr.out
298299
} # END CCDR_GRIDR
299300

300301
# ccdr_singleR

tests/testthat/test-default_run.R

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ test_that("Testing default behaviour of ccdr.run", {
1414
expect_is(final, "list")
1515

1616
### Check output types
17-
for(i in seq_along(final)){
18-
expect_is(final[[i]], "sparsebnFit")
19-
}
17+
check_sbf <- check_list_class(final, "sparsebnFit")
18+
expect_true(check_sbf)
19+
# for(i in seq_along(final)){
20+
# expect_is(final[[i]], "sparsebnFit")
21+
# }
2022

2123
### Check consistency of nedge
2224
for(i in seq_along(final)){
@@ -33,9 +35,11 @@ test_that("Testing ccdr.run with manual settings", {
3335
expect_is(final, "list")
3436

3537
### Check output types
36-
for(i in seq_along(final)){
37-
expect_is(final[[i]], "sparsebnFit")
38-
}
38+
check_sbf <- check_list_class(final, "sparsebnFit")
39+
expect_true(check_sbf)
40+
# for(i in seq_along(final)){
41+
# expect_is(final[[i]], "sparsebnFit")
42+
# }
3943

4044
### Check consistency of nedge
4145
for(i in seq_along(final)){
@@ -45,38 +49,12 @@ test_that("Testing ccdr.run with manual settings", {
4549
}
4650
})
4751

48-
### OLD TEST CODE
49-
# source('~/Dropbox/PhD Research/Programming Projects/bncompare_dev/bncompare/R/bncompare-generate.R')
50-
# # depends on
51-
# source('~/Dropbox/PhD Research/Programming Projects/bncompare_dev/bncompare/R/s3-trueGraph.R')
52-
# # depends on
53-
# source('~/Dropbox/PhD Research/Programming Projects/bncompare_dev/bncompare/R/bncompare-utils.R')
54-
# # depends on
55-
# source('~/Dropbox/PhD Research/Programming Projects/bncompare_dev/bncompare/R/bncompare-functions.R')
56-
#
57-
# R_DEBUG_ON <<- FALSE # This is just a hack since we aren't using load_bncompare.R to load the package
58-
#
59-
# ### Generate some random data
60-
# pp <- 10
61-
# nn <- 100
62-
# ss <- 2
63-
#
64-
# g <- generate_ordered_dag(pp, ss)
65-
# d <- generate_data(g, nn)
66-
# final <- ccdr.run(data = d$dat, lambdas.length = 20, alpha = 3, verbose = FALSE)
67-
#
68-
# test_that("Testing default behaviour of ccdr.gridR", {
69-
# expect_is(final, "list")
70-
#
71-
# ### Check output types
72-
# for(i in seq_along(final)){
73-
# expect_is(final[[i]], "sparsebnFit")
74-
# }
75-
#
76-
# ### Check consistency of nedge
77-
# for(i in seq_along(final)){
78-
# matrix.nedge <- sum(as.matrix(final[[i]]$sbm) != 0)
79-
# sbm.nedge <- .num_edges(final[[i]]$sbm)
80-
# expect_equal(final[[i]]$nedge, sbm.nedge, matrix.nedge)
81-
# }
82-
# })
52+
test_that("Bugfix: ccdr.run returns the correct number of solutions", {
53+
### Edge threshold not met, return all solutions
54+
final <- ccdr.run(data = data, lambdas.length = 5)
55+
expect_equal(length(final), 5)
56+
57+
### Edge threshold met, only return subpath of complete solutions
58+
final <- ccdr.run(data = data, lambdas.length = 5, alpha = 0.1)
59+
expect_equal(length(final), 1)
60+
})

0 commit comments

Comments
 (0)