-
Notifications
You must be signed in to change notification settings - Fork 39
Add c.network.list() #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add c.network.list() #604
Changes from all commits
5006f56
f36a194
eb9e241
39e9b73
e582833
072e73a
f0842b9
2b4b622
701be2a
95a81ca
31cde4c
04baa72
80fe538
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,3 +100,72 @@ | |
| } | ||
| object | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| #' @rdname network.list | ||
| #' | ||
| #' @param check_attr Logical: should the attributes of the combined network | ||
| #' lists be checked for consistency. If `TRUE` inconsistencies result in | ||
| #' errors. | ||
| #' | ||
| #' @importFrom purrr map | ||
| #' @export | ||
| #' | ||
| #' @examples | ||
| #' # Simulate some more | ||
| #' g.sim2 <- simulate(~edges+kstar(2), nsim=3, coef=c(-1.8, 0.03), | ||
| #' basis=g.use, control=control.simulate( | ||
| #' MCMC.burnin=100000, | ||
| #' MCMC.interval=1000)) | ||
| #' | ||
| #' # Merge the simulations | ||
| #' g.simall <- c(g.sim, g.sim2) | ||
| #' length(g.simall) # 6 | ||
| #' | ||
|
|
||
| c.network.list <- function(..., check_attr = TRUE) { | ||
| dots <- list(...) | ||
|
|
||
| # Merge network lists without attributes | ||
| rval <- do.call("c", map(dots, unclass)) | ||
|
|
||
| # Check attributes | ||
| if (check_attr) { | ||
| # Names of attributes to check with `all.equal()` | ||
| attr_names <- c("coefficients", "control", "response", | ||
| "formula", "constraints", "reference") | ||
| for (an in attr_names) { | ||
| al <- map(dots, ~ attr(.x, an)) | ||
| ok <- all_identical(al, all.equal) | ||
| if (!ok) stop("network lists do not have equal values on attribute ", an) | ||
| } | ||
|
|
||
| # Check if "stats" have identical columns | ||
| l_stats <- map(dots, ~ attr(.x, "stats")) | ||
| ok <- all_identical( | ||
| map(l_stats, function(x) colnames(x)), | ||
| .p = identical | ||
| ) | ||
| if (!ok) stop("network lists do not have identical columns of ", | ||
| sQuote("stats"), " attribute") | ||
| } | ||
| # Return the list of networks with attributes merged or taken from the | ||
| # first object | ||
| structure( | ||
| rval, | ||
| class = "network.list", | ||
| coefficients = attr(dots[[1]], "coefficients"), | ||
| control = attr(dots[[1]], "control"), | ||
| response = attr(dots[[1]], "response"), | ||
| stats = structure( | ||
| do.call("rbind", map(dots, ~attr(.x, "stats"))), | ||
| monitored = do.call("c", map(dots, ~ attr(attr(.x, "stats"), "monitored"))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| ), | ||
| formula = attr(dots[[1]], "formula"), | ||
| constraints = attr(dots[[1]], "constraints"), | ||
| reference = attr(dots[[1]], "reference") | ||
| ) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| suppressMessages({ | ||
| net0 <- network.initialize(36) | ||
| net0 %v% "x" <- rep(1:2, length = network.size(net0)) | ||
| k <- c(-2, -3, -3, -2) | ||
| s1 <- simulate(net0 ~ mm("x", levels2=TRUE), nsim=10, coef = k) | ||
|
Check warning on line 5 in tests/testthat/test-network.list.R
|
||
| s2 <- simulate(net0 ~ mm("x", levels2=TRUE), nsim=10, coef = k * 0.01) | ||
|
Check warning on line 6 in tests/testthat/test-network.list.R
|
||
| }) | ||
|
|
||
| test_that("there is no error if attributes match", { | ||
| expect_silent( | ||
| c(s1, s1) | ||
| ) | ||
| expect_silent( | ||
| c(s1, s1) | ||
| ) | ||
| }) | ||
|
|
||
| test_that("there is an error if attributes don't match", { | ||
| expect_error( | ||
| c(s1, s2) | ||
| ) | ||
| }) | ||
|
|
||
| test_that("there is no error if attributes are to be ignored", { | ||
| expect_silent( | ||
| c(s1, s2, check_attr = FALSE) | ||
| ) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not work for
simulate(simplify = FALSE), because thenattr(., "stats")is anmcmc.list, not a matrix.