Skip to content

Commit ea07b29

Browse files
committed
Simplified combined_networks(): rather than it being possible to un-combine on arbitrary vertex attribute, it is now only possible to uncombine on the combining attribute.
1 parent 89b4a0c commit ea07b29

31 files changed

Lines changed: 345 additions & 746 deletions

NAMESPACE

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ export(ERGM_LAYERS_SPEC)
2929
export(ERGM_LAYER_SPEC)
3030
export(Layer)
3131
export(Networks)
32-
export(assert_LHS_Layer)
32+
export(assert_combined_network)
3333
export(combine_ergmlhs)
3434
export(combine_networks)
3535
export(control.gofN)
3636
export(control.gofN.ergm)
3737
export(ergm.multi_combiner)
3838
export(ergm_LayerLogic)
3939
export(ergm_LayerLogics)
40-
export(ergmlhs_remove_blockdiag)
41-
export(get_combining_attr)
4240
export(gofN)
4341
export(lm.gofN)
4442
export(marg_cond_sim)
@@ -81,5 +79,6 @@ importFrom(statnet.common,ERRVL)
8179
importFrom(statnet.common,eval_lhs.formula)
8280
importFrom(statnet.common,statnetStartupMessage)
8381
importFrom(tibble,lst)
82+
importFrom(utils,head)
8483
importFrom(utils,modifyList)
8584
useDynLib(ergm.multi)

R/InitErgmTerm.dgw_sp.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515

1616
.sp.handle_layers <- function(nw, a, type, has_base, cache.sp=FALSE){
17-
assert_LHS_Layer(nw)
17+
assert_combined_network(nw, "Layer")
1818

1919
out <- list()
2020

@@ -67,7 +67,7 @@ no_layer_err <- function(instead){
6767

6868
wrap_ergm_sp_call <- function(ergm_name, nw, a, has_base, d0 = FALSE, cache.sp = TRUE, ...) {
6969
# A "representative" layer network.
70-
nw1 <- subnetwork_templates(nw, ".LayerID", ".LayerName")[[1]]
70+
nw1 <- subnetwork_templates(nw)[[1]]
7171

7272
# Construct and call the ergm term with only the arguments it
7373
# supports.

R/InitErgmTerm.multilayer.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ InitErgmTerm..layer.net <- function(nw, arglist, ...){
2525
defaultvalues = list(NULL),
2626
required = c(TRUE))
2727

28-
assert_LHS_Layer(nw)
28+
assert_combined_network(nw, "Layer")
2929

30-
nwl <- subnetwork_templates(nw, ".LayerID", ".LayerName", copy.ergmlhs = c())
30+
nwl <- subnetwork_templates(nw, copy.ergmlhs = c())
3131

3232
L <- ergm_LayerLogic(a$L, nw)
3333
# Terms on this logical layer will induce dyadic independence if its
@@ -40,7 +40,7 @@ InitErgmTerm..layer.net <- function(nw, arglist, ...){
4040
" This is not supported at this time.", call. = FALSE)
4141

4242
list(name = "_layer_net", coef.names = c(), dependence = dependence,
43-
iinputs = c(unlist(.block_vertexmap(nw, ".LayerID", TRUE)),
43+
iinputs = c(unlist(.block_vertexmap(nw)),
4444
if (is.directed(nw)) sapply(nwl, function(nw) (nw%v%".undirected")[1]),
4545
L%@%"C"))
4646
}
@@ -77,9 +77,9 @@ InitErgmTerm.L <- function(nw, arglist, ...){
7777
defaultvalues = list(NULL, empty_env(~.)),
7878
required = c(TRUE, FALSE))
7979

80-
assert_LHS_Layer(nw)
80+
assert_combined_network(nw, "Layer")
8181

82-
nwl <- subnetwork_templates(nw, ".LayerID", ".LayerName", copy.ergmlhs = c())
82+
nwl <- subnetwork_templates(nw, copy.ergmlhs = c())
8383

8484
Ls <- ergm_LayerLogics(a$Ls, nw)
8585

@@ -133,7 +133,7 @@ InitErgmTerm.CMBL <- function(nw, arglist, ...){
133133
defaultvalues = list(empty_env(~.)),
134134
required = c(FALSE))
135135

136-
assert_LHS_Layer(nw)
136+
assert_combined_network(nw, "Layer")
137137

138138
Ls <- ergm_LayerLogics(a$Ls, nw)
139139

@@ -192,7 +192,7 @@ InitErgmTerm.twostarL<-function(nw, arglist, ...) {
192192
defaultvalues = list(NULL, NULL, TRUE),
193193
required = c(TRUE, FALSE, FALSE))
194194

195-
assert_LHS_Layer(nw)
195+
assert_combined_network(nw, "Layer")
196196

197197
TYPES <- c("any", "out", "in", "path")
198198
TYPEREP <- setNames(c("--", "<>", "><", ">>"), TYPES)
@@ -259,7 +259,7 @@ InitErgmTerm.mutualL<-function (nw, arglist, ...) {
259259
defaultvalues = list(NULL, NULL, FALSE, NULL, NULL),
260260
required = c(FALSE, FALSE, FALSE, FALSE, FALSE))
261261

262-
assert_LHS_Layer(nw)
262+
assert_combined_network(nw, "Layer")
263263

264264
## Process the arguments
265265
if (!is.null(a$same) || !is.null(a$by)) {
@@ -361,7 +361,7 @@ InitErgmTerm.hammingL <- function(nw, arglist, ...){
361361
defaultvalues = list(empty_env(~.)),
362362
required = c(FALSE))
363363

364-
assert_LHS_Layer(nw)
364+
assert_combined_network(nw, "Layer")
365365

366366
Ls <- ergm_LayerLogics(a$Ls, nw)
367367
if (length(Ls) < 2L) ergm_Init_stop("multiple layers are required")

R/InitErgmTerm.multinet.R

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
################################################################################
1010

1111
InitErgmTerm..subnets <- function(nw, arglist, ...){
12-
a <- check.ErgmTerm(nw, arglist,
13-
varnames = c("attrname"),
14-
vartypes = c("character"),
15-
defaultvalues = list(NULL),
16-
required = c(TRUE))
12+
a <- check.ErgmTerm(nw, arglist)
1713

18-
list(name="_subnets", coef.names=c(), iinputs=c(unlist(.block_vertexmap(nw, a$attrname))), dependence=FALSE)
14+
list(name="_subnets", coef.names=c(), iinputs=c(unlist(.block_vertexmap(nw))), dependence=FALSE)
1915
}
2016

2117

@@ -55,13 +51,6 @@ get_lminfo <- function(nattrs, lm=~1, subset=TRUE, contrasts=NULL, offset=NULL,
5551
list(xf=xf, xm=xm, subset=subset, offset=offset)
5652
}
5753

58-
assert_LHS_Networks <- function(nw, nid, term_trace = TRUE, call = if(term_trace) NULL else rlang::caller_env()){
59-
if(anyNA(get_combining_attr(nw, nid))){
60-
msg <- paste0("The LHS of the model is not a multi-network ", sQuote("Networks()"), " construct.")
61-
if (term_trace) ergm_Init_abort(msg, call=call)
62-
else abort(msg, call=call)
63-
}
64-
}
6554

6655
#' @import purrr
6756
#' @import tibble
@@ -80,9 +69,10 @@ assert_LHS_Networks <- function(nw, nid, term_trace = TRUE, call = if(term_trace
8069
#' error. This can be avoided by pre-filtering with `subset`, which
8170
#' controls which networks are affected by the term.
8271
#'
83-
#' The formula may also reference `.NetworkID` and `.NetworkName`. In
84-
#' particular, `~0+factor(.NetworkID)` will evaluate `formula` on each
85-
#' network individually.
72+
#' The formula may also reference `.NetworkID` and `.NetworkName` (or
73+
#' the corresponding IDs set by `.combiner`). In particular,
74+
#' `~0+factor(.NetworkID)` will evaluate `formula` on each network
75+
#' individually.
8676
#'
8777
#' @note Care should be taken to avoid multicollinearity when using
8878
#' this operator. As with the [lm()] function, `lm` formulas have an
@@ -100,7 +90,7 @@ assert_LHS_Networks <- function(nw, nid, term_trace = TRUE, call = if(term_trace
10090
#'
10191
#' @usage
10292
#' # binary: N(formula, lm=~1, subset=TRUE, weights=1, contrasts=NULL, offset=0, label=NULL,
103-
#' # .NetworkID=".NetworkID", .NetworkName=".NetworkName")
93+
#' # .combiner = "Networks")
10494
#' @template ergmTerm-formula
10595
#' @param lm a one-sided [lm()]-style formula whose RHS specifies the network-level predictors for the terms in the [ergm()] formula `formula`.
10696
#' @param subset,contrasts see [lm()].
@@ -112,7 +102,10 @@ assert_LHS_Networks <- function(nw, nid, term_trace = TRUE, call = if(term_trace
112102
#' parameters to help identify the term (which may have similar
113103
#' predictors but, say, a different network subset) in the output
114104
#' *or* a function that wraps the names.
115-
#' @template ergmTerm-NetworkIDName
105+
#'
106+
#' @param .combiner A character vector specifying which combiner, such
107+
#' as [`Layer`] or [`Networks`] is expected to have created the LHS
108+
#' network.
116109
#'
117110
#' @section Offsets and fixing parameters:
118111
#'
@@ -158,21 +151,21 @@ assert_LHS_Networks <- function(nw, nid, term_trace = TRUE, call = if(term_trace
158151
#' @concept operator
159152
#' @concept directed
160153
#' @concept undirected
161-
InitErgmTerm.N <- function(nw, arglist, ..., N.compact_stats=TRUE, .NetworkID=".NetworkID", .NetworkName=".NetworkName"){
154+
InitErgmTerm.N <- function(nw, arglist, ..., N.compact_stats=TRUE, .combiner = "Networks"){
162155
a <- check.ErgmTerm(nw, arglist,
163-
varnames = c("formula", "lm", "subset", "weights", "contrasts", "offset", "label", ".NetworkID", ".NetworkName"),
164-
vartypes = c("formula", "formula", "formula,logical,numeric,expression,call", "formula,logical,numeric,expression,call", "list", "formula,logical,numeric,expression,call", "character,function", "character", "character"),
165-
defaultvalues = list(NULL, ~1, TRUE, 1, NULL, NULL, NULL, .NetworkID, .NetworkName),
166-
required = c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
156+
varnames = c("formula", "lm", "subset", "weights", "contrasts", "offset", "label", ".combiner"),
157+
vartypes = c("formula", "formula", "formula,logical,numeric,expression,call", "formula,logical,numeric,expression,call", "list", "formula,logical,numeric,expression,call", "character,function", "character"),
158+
defaultvalues = list(NULL, ~1, TRUE, 1, NULL, NULL, NULL, .combiner),
159+
required = c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
167160

168-
assert_LHS_Networks(nw, a$.NetworkID)
161+
assert_combined_network(nw, a$.combiner)
169162

170-
auxiliaries <- eval(substitute(~.subnets(.NetworkID), list(.NetworkID=a$.NetworkID)), baseenv())
163+
auxiliaries <- base_env(~.subnets)
171164

172-
nwl <- subnetwork_templates(nw, a$.NetworkID, a$.NetworkName)
165+
nwl <- subnetwork_templates(nw)
173166
nwnames <- names(nwl)
174167
nn <- length(nwl)
175-
nattrs <- as_tibble(nw, unit="networks", .NetworkID=a$.NetworkID, .NetworkName=a$.NetworkName, store.nid=TRUE)
168+
nattrs <- as_tibble(nw, unit="networks")
176169

177170
lmi <- get_lminfo(nattrs, lm=a$lm, subset=a$subset, contrasts=a$contrasts, offset=a$offset, weights=a$weights)
178171

@@ -356,16 +349,16 @@ InitErgmTerm.N <- function(nw, arglist, ..., N.compact_stats=TRUE, .NetworkID=".
356349
#' @concept directed
357350
#' @concept undirected
358351
#' @noRd
359-
InitErgmTerm.ByNetDStats <- function(nw, arglist, ..., .NetworkID=".NetworkID"){
352+
InitErgmTerm.ByNetDStats <- function(nw, arglist, ...){
360353
a <- check.ErgmTerm(nw, arglist,
361-
varnames = c("formula", "subset", ".NetworkID"),
354+
varnames = c("formula", "subset", ".combiner"),
362355
vartypes = c("formula", "formula,logical,numeric,expression,call", "character"),
363-
defaultvalues = list(NULL, TRUE, .NetworkID),
356+
defaultvalues = list(NULL, TRUE, "Networks"),
364357
required = c(TRUE, FALSE, FALSE))
365358

366-
assert_LHS_Networks(nw, a$.NetworkID)
359+
assert_combined_network(nw, a$.combiner)
367360

368-
auxiliaries <- eval(substitute(~.subnets(.NetworkID), list(.NetworkID=a$.NetworkID)), baseenv())
361+
auxiliaries <- base_env(~.subnets)
369362
nattrs <- as_tibble(nw, unit="networks")
370363

371364
lmi <- get_lminfo(nattrs, subset=a$subset)

R/InitErgmTerm.spcache.multilayer.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ InitErgmTerm..spcache.netL<-function(nw, arglist, ...){
1515
defaultvalues = list(NULL,NULL,NULL),
1616
required = c(TRUE, TRUE, TRUE))
1717

18-
assert_LHS_Layer(nw)
18+
assert_combined_network(nw, "Layer")
1919

2020
type <- match.arg(tolower(a$type), c("otp","osp","isp","utp")) # ITP not included, because it's just OTP with direction reversed.
2121

R/InitWtErgmTerm.multinet.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ InitWtErgmTerm..subnets <- function(...){
2323
#' @template ergmTerm-rdname
2424
#' @usage
2525
#' # valued: N(formula, lm=~1, subset=TRUE, weights=1, contrasts=NULL, offset=0, label=NULL,
26-
#' # .NetworkID=".NetworkID", .NetworkName=".NetworkName")
26+
#' # .combiner = "Networks")
2727
InitWtErgmTerm.N <- function(...){
2828
# Rename the function to avoid the extra nesting level in the
2929
# diagnostic messages.

R/block_blacklist.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ flatten_guid_pairs <- function(guidll){
116116

117117
get_all_bl_subnetattr <- function(l){
118118
lapply(l, function(subl)
119-
c(subl$.block_blacklist, lapply(subl$.subnetattr, get_all_bl_subnetattr), recursive=FALSE)
119+
c(subl$.block_blacklist, lapply(subl$.snattr, get_all_bl_subnetattr), recursive=FALSE)
120120
) %>% flatten_guid_pairs
121121
}
122122

123123
#' Obtain and concatenate unique block blacklists from the specified
124124
#' networks, which may be combined networks.
125125
#' @noRd
126126
get_all_bl <- function(nw, blacklist_nattr=".block_blacklist"){
127-
c(nw%n%blacklist_nattr, get_all_bl_subnetattr(nw%n%".subnetattr")) %>% unique
127+
c(nw%n%blacklist_nattr, get_all_bl_subnetattr(list(nw%n%".snattr"))) %>% unique
128128
}
129129

0 commit comments

Comments
 (0)