Skip to content
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ S3method(as.rlebdm,matrix)
S3method(as.rlebdm,network)
S3method(as.rlebdm,rlebdm)
S3method(c,ergm_model)
S3method(c,network.list)
S3method(coef,ergm)
S3method(compress,rlebdm)
S3method(degreedist,network)
Expand Down
69 changes: 69 additions & 0 deletions R/network.list.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check warning on line 153 in R/network.list.R

View check run for this annotation

Codecov / codecov/patch

R/network.list.R#L152-L153

Added lines #L152 - L153 were not covered by tests
}
# 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"))),
Copy link
Member

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 then attr(., "stats") is an mcmc.list, not a matrix.

monitored = do.call("c", map(dots, ~ attr(attr(.x, "stats"), "monitored")))

Check warning on line 165 in R/network.list.R

View workflow job for this annotation

GitHub Actions / lint-changed-warnings

file=R/network.list.R,line=165,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 81 characters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "monitored" attribute has an element for each statistic element, not each realisation, so it should be checked for consistency, not concatenated.

),
formula = attr(dots[[1]], "formula"),
constraints = attr(dots[[1]], "constraints"),
reference = attr(dots[[1]], "reference")
)
}
17 changes: 17 additions & 0 deletions man/network.list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions tests/testthat/test-network.list.R
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

View workflow job for this annotation

GitHub Actions / lint-changed-warnings

file=tests/testthat/test-network.list.R,line=5,col=52,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 5 in tests/testthat/test-network.list.R

View workflow job for this annotation

GitHub Actions / lint-changed-warnings

file=tests/testthat/test-network.list.R,line=5,col=40,[infix_spaces_linter] Put spaces around all infix operators.
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

View workflow job for this annotation

GitHub Actions / lint-changed-warnings

file=tests/testthat/test-network.list.R,line=6,col=52,[infix_spaces_linter] Put spaces around all infix operators.

Check warning on line 6 in tests/testthat/test-network.list.R

View workflow job for this annotation

GitHub Actions / lint-changed-warnings

file=tests/testthat/test-network.list.R,line=6,col=40,[infix_spaces_linter] Put spaces around all infix operators.
})

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)
)
})