Skip to content

Commit 19949c3

Browse files
mbojankrivit
authored andcommitted
Add c.network.list()
1 parent 5d93e56 commit 19949c3

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ S3method(as.rlebdm,matrix)
3333
S3method(as.rlebdm,network)
3434
S3method(as.rlebdm,rlebdm)
3535
S3method(c,ergm_model)
36+
S3method(c,network.list)
3637
S3method(coef,ergm)
3738
S3method(compress,rlebdm)
3839
S3method(degreedist,network)

R/network.list.R

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,58 @@ summary.network.list <- function (object, stats.print=TRUE,
9191
}
9292
object
9393
}
94+
95+
#' @describeIn network.list A method for concatenating network lists preserving metadata.
96+
#'
97+
#' @param check_attr Logical: should the attributes of the combined network
98+
#' lists be checked for consistency. If `TRUE` inconsistencies result in
99+
#' errors.
100+
#'
101+
#' @importFrom purrr map
102+
#' @export
103+
c.network.list <- function(..., check_attr = TRUE) {
104+
dots <- list(...)
105+
106+
# Merge network lists without attributes
107+
lapply(dots, function(x) {
108+
attributes(x) <- NULL
109+
x
110+
}) -> l_networks
111+
rval <- do.call("c", l_networks)
112+
113+
# Check attributes
114+
if(check_attr) {
115+
# Names of attributes to check with `all.equal()`
116+
attr_names <- c("coefficients", "control", "response",
117+
"formula", "constraints", "reference")
118+
for(an in attr_names) {
119+
al <- map(dots, ~ attr(.x, an))
120+
ok <- all_identical(al, all.equal)
121+
if(!ok) stop(paste0("network lists do not have equal values on attribute ", an))
122+
}
123+
124+
# Check if "stats" have identical columns
125+
l_stats <- map(dots, ~ attr(.x, "stats"))
126+
ok <- all_identical(
127+
lapply(l_stats, function(x) colnames(x)),
128+
fun = identical
129+
)
130+
if(!ok) stop("network lists do not have identical columns of 'stats' attribute")
131+
}
132+
# Return the list of networks with attributes merged or taken from the
133+
# first object
134+
structure(
135+
rval,
136+
class = "network.list",
137+
coefficients = attr(dots[[1]], "coefficients"),
138+
control = attr(dots[[1]], "control"),
139+
response = attr(dots[[1]], "response"),
140+
stats = structure(
141+
do.call("rbind", map(dots, ~attr(.x, "stats"))),
142+
monitored = do.call("c", map(dots, ~ attr(attr(.x, "stats"), "monitored")))
143+
),
144+
formula = attr(dots[[1]], "formula"),
145+
constraints = attr(dots[[1]], "constraints"),
146+
reference = attr(dots[[1]], "reference")
147+
)
148+
}

man/network.list.Rd

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)